Advertisement
nicb

Array circolare Staiano (Ese Esame 1)

Jul 19th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.76 KB | None | 0 0
  1. /*
  2.  
  3. Si realizzi un programma in C e Posix sotto Linux che , con l ausilio della libreria pthread , lancia n thread passato da riga di comando , in modo che il thread i-esimo prelevi casualmente un elemento da una matrice m * n di cartteri allocata dinamicamente e lo insersca in un buffer circolare di grandezza N.
  4. Concorrentemente agli n thread , un thread n+1 esimo , preleva continuamente un elemento disponibile , e lo stampa.
  5.  
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <pthread.h>
  11. #include <time.h>
  12. #include <semaphore.h>
  13.  
  14.  
  15.  
  16. char ** matrix, *array ;
  17. int m,N,index_produttore = 0;
  18. long int n;
  19. sem_t mutex,size_N,event;
  20.  
  21. void print_matrix(int row, int col){
  22.    
  23.     int i, j;
  24.    
  25.     for(i=0;i<row;i++){
  26.        
  27.         printf("\n");
  28.  
  29.         for(j=0;j<col;j++){
  30.  
  31.             printf("%c ",matrix[i][j]);
  32.         }
  33.     }
  34. }
  35.  
  36. void set_matrix(int row, int col){
  37.    
  38.     int i, j;
  39.    
  40.     for(i=0;i<row;i++){
  41.    
  42.         for(j=0;j<col;j++){
  43.  
  44.             matrix[i][j] = 65 + rand() %(90-65);
  45.         }
  46.     }
  47. }
  48.  
  49. void * insert(void * args ){
  50.    
  51.     int index = (* (int *) args);
  52.     int index_row_rand,index_col_rand;
  53.    
  54.    
  55.     index_row_rand = rand()%m;
  56.     index_col_rand = rand()%n;
  57.        
  58.     sem_wait(&size_N);
  59.        
  60.         sem_wait(&mutex);
  61.            
  62.             array[index_produttore % N] = matrix[index_row_rand][index_col_rand];
  63.             index_produttore ++;
  64.             printf("\ni m thread %lu value of index_row rand : %d ,value of index_col_rand %d, index array %d , value array[%c]\n ",pthread_self(),index_row_rand, index_col_rand,index_produttore,matrix[index_row_rand][index_col_rand]);
  65.  
  66.         sem_post(&mutex);
  67.    
  68.     sem_post(&event);
  69.     return (NULL);
  70.    
  71. }
  72.  
  73. void * cons(void * args){
  74.  
  75.     int i;
  76.  
  77.     for(i=0;i<n;i++){  
  78.  
  79.         sem_wait(&event);
  80.            
  81.             sem_wait(&mutex);
  82.  
  83.             printf("consumer this is value of buffer[%d] %c\n",i,array[i % N]);
  84.            
  85.             sem_post(&mutex);
  86.        
  87.         sem_post(&size_N);
  88.     }
  89.  
  90.     return(NULL);
  91. }
  92.  
  93. void main(int argc, char *argv[]){
  94.  
  95.  
  96.     if ( argc != 4 ){
  97.    
  98.         fprintf(stderr,"error pass two argument \n");  
  99.         fprintf(stderr,"pass size row , size of column, size of array\n");
  100.         exit(-1);
  101.     }
  102.    
  103.    
  104.     srand(time(NULL));
  105.  
  106.     m = atoi(argv[1]);
  107.     n = atoi(argv[2]);
  108.     N = atoi(argv[3]);
  109.  
  110.     int task[N],i;
  111.     pthread_t * tid, consumer;
  112.  
  113.     matrix = malloc( sizeof(int *) * m );
  114.     array = malloc( sizeof(int) * N);
  115.     tid = malloc( sizeof(pthread_t) * n );
  116.  
  117.    
  118.     for(i=0;i<m;i++)
  119.         matrix[i] = malloc( sizeof (int) * n);
  120.  
  121.     set_matrix(m,n);
  122.     print_matrix(m,n);
  123.    
  124.     sem_init(&mutex,0,1);
  125.     sem_init(&event,0,0);
  126.     sem_init(&size_N,0,N);
  127.    
  128.     for(i=0;i<n;i++){
  129.        
  130.         task[i] = i;
  131.         pthread_create(&tid[i],NULL,insert,&task[i]);
  132.     }
  133.  
  134.     pthread_create(&consumer,NULL,cons,NULL);
  135.  
  136.     for(i=0;i<n;i++)
  137.         pthread_join(tid[i],NULL);
  138.  
  139.     pthread_join(consumer,NULL);
  140.  
  141.     sem_destroy(&mutex);
  142.     sem_destroy(&event);
  143.     sem_destroy(&size_N);
  144.  
  145.     exit(0);
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement