Advertisement
Guest User

Settembre 2010

a guest
Feb 10th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.52 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. #include <unistd.h>
  5.  
  6. /*
  7. Appello 3 del 14.09.2010 - A.A. 2009/2010
  8. Realizzate un programma multi thread in C, completo di commento, che svolga quanto segue: il thread iniziale che
  9. esegue il main crea due thread TH1 e TH2. I due thread condividono due variabili intere S1 e S2 sulle quali
  10. operano in mutua esclusione. TH1 genera periodicamente, ogni secondo, un numero casuale compreso tra 1 e 50 e
  11. lo sottrae a S1 se il numero estratto è dispari, mentre lo sottrae a S2 se il numero estratto è pari. TH2 genera
  12. periodicamente, ogni due secondi, un numero casuale compreso tra 1 e 100 e lo aggiunge ad S1 ed incrementa di 1
  13. il valore di S2. Il programma termina quando TH1 verifica che il valore della somma S1+S2 ha superato il valore
  14. 1000. Prima della terminazione del programma il thread main stampa su schermo il valore finale della somma S1+S2.
  15. */
  16.  
  17. int S1 = 0;
  18. int S2 = 0;
  19. int somma = 0;
  20. int fine = 0;
  21. pthread_mutex_t M;
  22.  
  23. int get_random(int min, int max) {
  24.   return (rand() % (max - min + 1)) + min;
  25. }
  26.  
  27. void *codiceTh1(void *arg){
  28.     if(somma > 1000){
  29.         fine = 1;
  30.     } else {
  31.         int x = get_random(1, 50);
  32.         pthread_mutex_lock(&M);
  33.         if (x%2 != 0){
  34.             S1 = S1 - x;
  35.             fprintf(stdout, "Ho sottratto %d a S1\n", x);
  36.         } else {
  37.             S2 = S2 - x;
  38.             fprintf(stdout, "Ho sottratto %d a S2\n", x);
  39.         }
  40.         pthread_mutex_unlock(&M);
  41.         sleep(1);
  42.     }
  43. }
  44.  
  45. void *codiceTh2(void *arg){
  46.     if(fine == 0){
  47.         int x = get_random(1, 100);
  48.         pthread_mutex_lock(&M);
  49.         S1 = S1 + x;
  50.         S2++;
  51.         fprintf(stdout, "Ho aggiunto %d a S1\n", x);
  52.         pthread_mutex_unlock(&M);
  53.         sleep(2);
  54.     }
  55. }
  56.  
  57. int main(){
  58.     pthread_t th1, th2;
  59.     pthread_mutex_init(&M, NULL);
  60.  
  61.     while(fine == 0){
  62.         if(pthread_create(&th1, NULL, codiceTh1, (int*)0) != 0){
  63.             fprintf(stderr, "Errore creazione thread 1\n");
  64.             exit(1);
  65.         }
  66.         if(pthread_create(&th2, NULL, codiceTh2, (int*)0) != 0){
  67.             fprintf(stderr, "Errore creazione thread 2\n");
  68.             exit(1);
  69.         }
  70.  
  71.         somma = S1 + S2;
  72.         fprintf(stdout, "La somma è %d\n", somma);
  73.  
  74.         if(pthread_join(th1, NULL) != 0){
  75.             fprintf(stderr, "Errore join\n");
  76.         }
  77.         if(pthread_join(th2, NULL) != 0){
  78.             fprintf(stderr, "Errore join\n");
  79.         }
  80.     }
  81.     fprintf(stdout, "Ho finito, ciao!\n");
  82.     return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement