SHARE
TWEET

Untitled

a guest May 25th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <pthread.h>
  5. #include <time.h>
  6. #include <math.h>
  7.  
  8. #define N 3
  9. #define K 3
  10.  
  11. /* run this program using the console pauser or add your own getch, system("pause") or input loop */
  12.  
  13. pthread_mutex_t mutex;
  14. int mt[K][N];
  15.  
  16.  
  17. void * trfun(void *arg){
  18. int pers= (intptr_t) arg;
  19. printf("Persona numero: %d \n", pers+1);
  20. pthread_mutex_lock(&mutex);
  21. int i = 0;
  22. int j = 0;
  23. int z = 0;
  24. int m[K];
  25.  
  26.  
  27.     for(i=0; i<K; i++) {
  28.         mt[pers][i] = rand()%10;
  29.     }  
  30.     for(j=0; j<K; j++) {
  31.         for(i=0;i<pers;i++){
  32.             m[z] = m[z]+mt[j][i];
  33.         }
  34.     }
  35.    
  36.     for(i=0; i<K; i++) {
  37.     m[z] /= pers+1;
  38.     }
  39.    
  40.     z++;
  41.     sleep(3);
  42.     pthread_mutex_unlock(&mutex);
  43.    
  44.     printf("media parziale dopo compilazione utente %d\n", pers+1);
  45.    
  46.     for(i=0; i<K; i++){
  47.         printf("%d\n", m[i]);
  48.     }
  49.    
  50.     printf("\n");
  51.     pthread_exit(0);
  52. return NULL;
  53. }
  54. int main() {
  55.     pthread_t tid[N];
  56.     int i = 0;
  57.     pthread_mutex_init(&mutex, NULL);
  58.     for(i=0; i<N;i++)
  59.     {
  60.         pthread_create(&tid[i], NULL , trfun , (void *)(intptr_t)i);
  61.     }
  62.    
  63.     for(i=0; i<N;i++)
  64.     {
  65.     pthread_join(tid[i], NULL);
  66.     }
  67.    
  68.     return 0;
  69. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top