NicolaDelPrete

POSIX find

Jul 14th, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.82 KB | None | 0 0
  1. #include <semaphore.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <pthread.h>
  5. #include <unistd.h>
  6.  
  7.  
  8. int n=5;
  9. int *matrice;
  10. int num,pos=-1;
  11. sem_t semaforo;
  12.  
  13. void *numero( void *i )
  14. {
  15.     sem_wait(&semaforo);   
  16.     int ind,j;
  17.     ind=*(int*)i;
  18.     for(j=0;j<n;j++)
  19.     {   if(num==*(matrice+ind*n+j) && pos==-1)
  20.             pos=ind;
  21.     }
  22.     sem_post(&semaforo);   
  23. }
  24.  
  25. int main()
  26. {
  27.     int i,j;   
  28.     pthread_t riga[n];
  29.     matrice=(int*)calloc(n*n,sizeof(int));
  30.     sem_init(&semaforo,0,1);
  31.     for(i=0;i<n;i++)
  32.     {   for(j=0;j<n;j++)
  33.         *(matrice+i*n+j)=rand()%5+1;
  34.     }
  35.     num=rand()%5+1;
  36.     for(i=0;i<n;i++)
  37.         pthread_create(&riga[i],NULL,numero,(void*)&i);
  38.     sleep(n);
  39.     if(num!=-1)
  40.         printf("Ho trovato l'elemento nella posizione %d \n",pos);
  41.     for(i=0;i<n;i++)
  42.         pthread_join(riga[i],NULL);
  43.     sem_destroy(&semaforo);
  44.     free(matrice);
  45.     return 0;
  46. }
Add Comment
Please, Sign In to add comment