Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <semaphore.h>
- #include <unistd.h>
- #include <pthread.h>
- int N=0;
- int *matrice;
- sem_t sem,sem2;
- void *fibonacci ( void *i)
- {
- sem_wait(&sem);
- int ind,j;
- ind=*(int*)i;
- for(j=1;j<N;j++)
- {
- if(j-2<0)
- {
- *(matrice+ind*N+j)=*(matrice+ind*N+j-1)+*(matrice+ind*N+j-1);
- }else
- {
- *(matrice+ind*N+j)=*(matrice+ind*N+j-1)+*(matrice+ind*N+j-2);
- }
- }
- sem_post(&sem2);
- sem_post(&sem);
- }
- void *matrices (void*i)
- {
- sem_wait(&sem2);
- int ind,j;
- ind=*(int*)i;
- for(j=0;j<N;j++)
- printf("%d\t",*(matrice+ind*N+j));
- printf("\n");
- }
- int main()
- {
- int i,sec=5000,sec2=5;
- while (N>15 || N<10)
- {
- printf("Inserire il valore di N: ");
- scanf("%d",&N);
- }
- matrice=(int*)calloc(N*N,sizeof(int));
- sem_init(&sem,0,1);
- sem_init(&sem2,0,0);
- pthread_t riga,leggi;
- for(i=0;i<N;i++)
- *(matrice+i*N)=i+1;
- for(i=0;i<N;i++)
- {pthread_create(&riga,NULL,fibonacci,(void*)&i);usleep(sec);
- pthread_create(&leggi,NULL,matrices,(void*)&i);usleep(sec);}
- sleep(sec2);
- pthread_join(riga,NULL);
- pthread_join(leggi,NULL);
- sem_destroy(&sem);
- free(matrice);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment