Advertisement
Guest User

e

a guest
May 27th, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.70 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <time.h>
  4. #include <stdlib.h>
  5. #include <sys/types.h>
  6. #include <sys/mman.h>
  7. #include <fcntl.h>
  8. #include <unistd.h>
  9. #include <sys/shm.h>
  10. #include <sys/stat.h>
  11. #include <sys/wait.h>
  12. #include <pthread.h>
  13.  
  14. #define NUM_THREADS 5
  15. #define MAX_ELE_VEC 10000
  16.  
  17.  
  18.  
  19. int vec[MAX_ELE_VEC];
  20.  
  21. int count_ele_thread1=0;
  22. int count_ele_thread2=0;
  23. int count_ele_thread_3_4=0;
  24. int count_ele_total=0;
  25.  
  26. pthread_mutex_t mut;
  27. pthread_cond_t cond_thread3;
  28. pthread_cond_t cond_thread4;               
  29. pthread_cond_t cond_thread5;                       
  30.  
  31.  
  32. void* thread_func_1_2(void *arg){
  33.  
  34.     int *num_thread = (int*)arg;
  35.    
  36.     printf("Incrementacao do vec começou na thread %d\n",num_thread);
  37.     do{
  38.                 pthread_mutex_lock(&mut);
  39.                 vec[count_ele_total]=rand()%99;
  40.                 count_ele_total++;
  41.                 if(num_thread==1){
  42.                     count_ele_thread1++;
  43.                     if(count_ele_thread1%50==0){
  44.                         pthread_cond_signal(&cond_thread3);
  45.                     }
  46.                 }else{
  47.                     count_ele_thread2++;
  48.                     if(count_ele_thread2%50==0){
  49.                         pthread_cond_signal(&cond_thread4);
  50.                     }
  51.                 }
  52.                
  53.                 if(count_ele_total==5000){
  54.                     pthread_cond_signal(&cond_thread5);
  55.                 }
  56.                 pthread_mutex_unlock(&mut);
  57.            
  58.            
  59.     }while(count_ele_total<MAX_ELE_VEC);
  60.        
  61.     pthread_exit(NULL);
  62.        
  63. }
  64.  
  65.  
  66. void* thread_func_3_4(void *arg){
  67.         int *num_thread = (int*)arg;
  68.         do{
  69.             pthread_mutex_lock(&mut);
  70.             if(num_thread==3){
  71.                 while(count_ele_total==count_ele_thread_3_4){
  72.                     pthread_cond_wait(&cond_thread3,&mut);
  73.                 }
  74.                 while(count_ele_total>count_ele_thread_3_4){
  75.                     printf("Thread 1 preencheu mais 50 posições\n");
  76.                 count_ele_thread_3_4=count_ele_thread_3_4+50;
  77.                 }
  78.             }else{
  79.                 while(count_ele_total==count_ele_thread_3_4){
  80.                     pthread_cond_wait(&cond_thread4,&mut);
  81.                 }
  82.                 while(count_ele_total>count_ele_thread_3_4){
  83.                     printf("Thread 2 preencheu mais 50 posições\n");
  84.                 count_ele_thread_3_4=count_ele_thread_3_4+50;
  85.                 }
  86.             }
  87.            
  88.            
  89.             pthread_mutex_unlock(&mut);
  90.            
  91.         }while(count_ele_thread_3_4<MAX_ELE_VEC);
  92.  
  93.         pthread_exit((void*)NULL);  //nao retorna nada
  94.  
  95. }
  96.  
  97. void* thread_func5(void *arg){
  98.            
  99.            
  100.             pthread_mutex_lock(&mut);
  101.            
  102.             pthread_cond_wait(&cond_thread5,&mut);
  103.                    
  104.             printf("Thread 1 preencheu: %d posicoes\nThread2 preencheu: %d posicoes\nTotal: %d\n",count_ele_thread1,count_ele_thread2,count_ele_total);
  105.    
  106.             pthread_mutex_unlock(&mut);
  107.            
  108.         pthread_exit((void*)NULL);  //nao retorna nada
  109.  
  110. }
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117. int main(){
  118.  
  119.     int i, ret;
  120.     pthread_t threads[NUM_THREADS];            
  121.  
  122.    
  123.     //criacao do mutex para acesso exclusivo
  124.     ret = pthread_mutex_init(&mut, NULL);   //NULL -> usa valores por omissao
  125.     if (ret!=0){
  126.         perror("ERRO NO MUTEX\n");
  127.     }
  128.    
  129.     //CRIA VARIAVEIS CONDICAO
  130.     ret = pthread_cond_init( &cond_thread3,NULL);
  131.     if (ret!=0){
  132.         perror("ERRO NA VARIAVEL CONDICAO\n");
  133.     }
  134.    
  135.     ret = pthread_cond_init( &cond_thread4,NULL);
  136.     if (ret!=0){
  137.         perror("ERRO NA VARIAVEL CONDICAO\n");
  138.     }
  139.    
  140.     ret = pthread_cond_init( &cond_thread5,NULL);
  141.     if (ret!=0){
  142.         perror("ERRO NA VARIAVEL CONDICAO\n");
  143.     }
  144.    
  145.    
  146.    
  147.    
  148.     //CRIACAO DAS THREADS
  149.  
  150.     for(i=0;i<NUM_THREADS;i++){
  151.         int ret = pthread_create(&threads[i], NULL, posto,(void*)&(i+1));   //cast (void*) do indice +1
  152.         if (ret!=0){
  153.             perror("ERRO NA THREAD\n");
  154.         }
  155.     }
  156.  
  157.  
  158.    
  159.  
  160.     /***************************THREADS A FUNCIONAR***********************/
  161.  
  162.     printf("Todas as threads foram criadas\n");
  163.  
  164.  
  165.     //a thread MAIN vai esperar por todas as threads
  166.     for(i=0;i<NUM_THREADS;i++){
  167.         pthread_join(threads[i], NULL);
  168.     }
  169.  
  170.     pthread_mutex_destroy(&mut);
  171.     pthread_cond_destroy(&cond_thread3);
  172.     pthread_cond_destroy(&cond_thread4);
  173.     pthread_cond_destroy(&cond_thread5);
  174.     printf("Todas as threads terminaram\n");
  175.  
  176.     return 0;
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement