Advertisement
nicb

Prodotto RigaColonna Esame Staiano

Jul 19th, 2018
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <semaphore.h>
  4. #include <pthread.h>
  5. #include <unistd.h>
  6. #include <fcntl.h>
  7.  
  8. int **matriceA, **matriceB, **matriceC;
  9. int n,m,p;
  10.  
  11. pthread_mutex_t mutex= PTHREAD_MUTEX_INITIALIZER;
  12. pthread_cond_t cond= PTHREAD_COND_INITIALIZER;
  13. int controllo=0;
  14. sem_t semaforo;
  15.  
  16. void *prodotto( void* arg)
  17. {
  18.     int indice;
  19.     int i,j;
  20.     //int appoggio[p];
  21.     indice=*(int*)arg;
  22.     for(i=0;i<p;i++)//scorre le colonne
  23.     {
  24.         for(j=0;j<n;j++)//scorre grandezza comune comune
  25.         {
  26.             matriceC[indice][i]+=matriceA[indice][j]*matriceB[j][i];
  27.         }
  28.     }
  29.     sem_wait(&semaforo);
  30.     controllo++;
  31.     sem_post(&semaforo);
  32.     pthread_mutex_lock(&mutex);
  33.     if(controllo==m)
  34.         pthread_cond_signal(&cond);
  35.     pthread_mutex_unlock(&mutex);
  36.     pthread_exit(0);
  37.     //for(i=0;i<p;i++)
  38.     //printf("\t%d",appoggio[i]);
  39.    
  40. }
  41. void *stampa ()
  42. {
  43.     int i,j;
  44.     pthread_mutex_lock(&mutex);
  45.     if(controllo<m)
  46.         pthread_cond_wait(&cond,&mutex);
  47.     pthread_mutex_unlock(&mutex);
  48.     printf("\n\n\n\n\n Risultato:\n");
  49.     for(i=0;i<m;i++)
  50.     {
  51.         for(j=0;j<p;j++)
  52.         {
  53.             printf("\t%d",matriceC[i][j]);
  54.         }
  55.         printf("\n");
  56.     }
  57.     pthread_exit(0);
  58. }
  59.    
  60.  
  61. int main(int argc, char **argv)
  62. {
  63.     int i,j;
  64.     n=atoi(argv[1]);
  65.     m=atoi(argv[2]);
  66.     p=atoi(argv[3]);
  67.  
  68.     matriceA= (int**)calloc(m,sizeof(int*));
  69.         for(i=0;i<m;i++)
  70.             matriceA[i]=(int*)calloc(n,sizeof(int));
  71.     matriceB= (int**)calloc(n,sizeof(int*));
  72.         for(i=0;i<n;i++)
  73.             matriceB[i]=(int*)calloc(p,sizeof(int));
  74.     matriceC= (int**)calloc(m,sizeof(int*));
  75.         for(i=0;i<m;i++)
  76.             matriceC[i]=(int*)calloc(p,sizeof(int));
  77.  
  78.     for(i=0;i<m;i++)
  79.     {
  80.         for(j=0;j<n;j++)
  81.         {
  82.             matriceA[i][j]=rand()%10;
  83.         }
  84.     }
  85.     for(i=0;i<n;i++)
  86.     {
  87.         for(j=0;j<p;j++)
  88.         {
  89.             matriceB[i][j]=rand()%10;
  90.         }
  91.     }  
  92.    
  93.     for(i=0;i<m;i++)
  94.     {
  95.         for(j=0;j<n;j++)
  96.         {
  97.             printf("\t%d",matriceA[i][j]);
  98.         }
  99.         printf("\n");
  100.     }
  101.  
  102.     printf("\n\n\n");
  103.  
  104.            
  105.     for(i=0;i<n;i++)
  106.     {
  107.         for(j=0;j<p;j++)
  108.         {
  109.             printf("\t%d",matriceB[i][j]);
  110.         }
  111.         printf("\n");
  112.     }
  113.    
  114.     sem_init(&semaforo,0,1);
  115.     //semaforo=sem_open("sema", O_CREAT | O_EXCL, 0777, 1);
  116.    
  117.     pthread_t id[m], controllore;
  118.     pthread_create(&controllore, NULL, stampa, NULL);
  119.     for(i=0;i<m; i++)
  120.         pthread_create(&id[i], NULL, prodotto, (void*)&i);
  121.  
  122.    
  123.     for(i=0;i<m;i++)
  124.         pthread_join(id[i],NULL);
  125.     pthread_join(controllore, NULL);
  126.     sem_destroy(&semaforo);
  127.     //sem_unlink("sema");
  128.  
  129.  
  130.     return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement