Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pthread.h>
- #include <semaphore.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #define SEM_VALORE "/valore"
- int m=5;
- int min=90000;
- int *matrice;
- sem_t *valore;
- void *somma (void *i)
- {
- sem_wait(valore);
- int j,indice,somma=0;
- indice=*(int*)i;
- if(indice%2==0)
- {
- j=0;
- while(j<m)
- {
- somma=somma + (*matrice+indice*m+j);
- j=j+2;
- }
- }else
- {
- j=1;
- while(j<m)
- {
- somma= somma + (*matrice+indice*m+j);
- j=j+2;
- }
- }
- if(somma<min)
- min=somma;
- sem_post(valore);
- }
- int main ()
- {
- int i,j;
- pthread_t riga[m];
- valore=sem_open(SEM_VALORE,O_CREAT|O_EXCL,077,1);
- matrice=(int*)calloc(m*m,sizeof(int));
- for(i=0;i<m;i++)
- { for(j=0;j<m;j++)
- *(matrice+i*m+j)=rand()%256;
- }
- for(i=0;i<m;i++)
- { pthread_create(&riga[i],NULL,somma,(void*)&i);}
- sleep(5);
- printf("%d\n",min);
- for(i=0;i<m;i++)
- pthread_join(riga[i],NULL);
- sem_unlink(SEM_VALORE);
- free(matrice);
- return 0;
- }
Add Comment
Please, Sign In to add comment