Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <semaphore.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <unistd.h>
- int n=5;
- int *matrice;
- int num,pos=-1;
- sem_t semaforo;
- void *numero( void *i )
- {
- sem_wait(&semaforo);
- int ind,j;
- ind=*(int*)i;
- for(j=0;j<n;j++)
- { if(num==*(matrice+ind*n+j) && pos==-1)
- pos=ind;
- }
- sem_post(&semaforo);
- }
- int main()
- {
- int i,j;
- pthread_t riga[n];
- matrice=(int*)calloc(n*n,sizeof(int));
- sem_init(&semaforo,0,1);
- for(i=0;i<n;i++)
- { for(j=0;j<n;j++)
- *(matrice+i*n+j)=rand()%5+1;
- }
- num=rand()%5+1;
- for(i=0;i<n;i++)
- pthread_create(&riga[i],NULL,numero,(void*)&i);
- sleep(n);
- if(num!=-1)
- printf("Ho trovato l'elemento nella posizione %d \n",pos);
- for(i=0;i<n;i++)
- pthread_join(riga[i],NULL);
- sem_destroy(&semaforo);
- free(matrice);
- return 0;
- }
Add Comment
Please, Sign In to add comment