Created
July 31, 2016 23:40
-
-
Save pnalvarez/349491c03baba172826d142b44ad0ece to your computer and use it in GitHub Desktop.
codigo a ser verificado
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <sys/wait.h> | |
#include <sys/stat.h> | |
#include <sys/shm.h> | |
int main(void){ | |
int *m1,*m2,*m3,seg[3],i,j,pid = getpid(); | |
/*gero 3 areas de memoria compartilhada e faco attach de cada um a uma matriz 3 por 3*/ | |
seg[0] = shmget(IPC_PRIVATE, 9 * sizeof(int),IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR); | |
m1 = (int*) shmat(seg[0],0,0); | |
seg[1]=shmget(IPC_PRIVATE, 9 * sizeof(int),IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR); | |
m2=(int*) shmat(seg[1],0,0); | |
seg[2]=shmget(IPC_PRIVATE, 9 * sizeof(int),IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR); | |
m3= (int*) shmat(seg[2],0,0); | |
/*preencho as duas primeiras matrizes com valores entre 0 e 9 de acordo com o contador do loop */ | |
for(i=0;i<3;i++){ | |
for(j=0;j<3;j++) | |
*(m1+3*i+j)=3*i+j; | |
} | |
for(i=0;i<3;i++){ | |
for(j=0;j<3;j++) | |
*(m2+3*i+j)=3*i+j; | |
} | |
/*Para cada linha da matriz m3, gero um novo processo e cada coluna é a soma do valor da matriz 1 com a matriz 2* */ | |
for(i=0;i<3;i++){ | |
if(fork()==0){ | |
for(j=0;j<3;j++) | |
*(m3+3*i+j)= *(m1+3*i+j) + *(m2+3*i+j); | |
} | |
else | |
break; | |
} | |
/* Imprimo todos os valores da matriz soma*/ | |
for(i=0;i<3;i++){ | |
for(j=0;j<3;j++) | |
printf("%d ",*(m3+3*i+j)); | |
printf("%d\n",getpid()); | |
} | |
/*libero as areas de memoria compartihada*/ | |
shmdt(m1); | |
shmctl(seg[0],IPC_RMID,0); | |
shmdt(m2); | |
shmctl(seg[1],IPC_RMID,0); | |
shmdt(m3); | |
shmctl(seg[2],IPC_RMID,0); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Acredito que você deveria trocar o break; pelo return 0; (ou return inteiro qualquer)