andrelievable

Untitled

Nov 21st, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <pthread.h>
  5. #include <time.h>
  6. #define REENTRAN
  7.  
  8. int status;
  9. int tab[2][10];
  10. int sum1=0;
  11. int sum2=0;
  12.  
  13. void * suma1()
  14. {
  15.     for(int i=0;i<10;i++)
  16.     {
  17.         sum1+=tab[0][i];
  18.        
  19.     }
  20.    
  21.    
  22.     pthread_exit((void *)15);
  23.    
  24. }
  25.  
  26. void * suma2()
  27. {
  28.     for(int j=0;j<10;j++)
  29.     {
  30.         sum2+=tab[1][j];
  31.     }
  32.    
  33.    
  34.         pthread_exit((void *)51);
  35.    
  36. }
  37.  
  38. int main()
  39. {
  40.     int tmp, status;
  41.     pthread_t one, two;
  42.     srand(time(NULL));
  43.    
  44.     printf("\n");
  45.     for(int i=0; i<2; i++)
  46.     {
  47.         for(int j=0;j<10;j++)
  48.         {
  49.             tab[i][j]=rand()%50;
  50.             printf("%d ",tab[i][j]);
  51.         }
  52.         printf("\n");
  53.     }
  54.     tmp=pthread_create(&one, NULL, suma1, NULL);
  55.     if(tmp==-1)
  56.     {
  57.         printf("blad tworzenia watku one");
  58.         exit(1);
  59.     }
  60.    
  61.    
  62.    
  63.     tmp=pthread_create(&two, NULL, suma2, NULL);
  64.     if(tmp==-1)
  65.     {
  66.         printf("blad tworzenia watku two");
  67.         exit(1);
  68.     }
  69.    
  70.    
  71.    
  72.     tmp=pthread_join(one, (void **)&status);
  73.     if(tmp==-1)
  74.     {
  75.         printf("blad podlaczenia watku one");
  76.         exit(1);
  77.     }
  78.     else if(tmp==0)
  79.     printf("status = %d\n",status);
  80.    
  81.     tmp=pthread_join(two, (void **)&status);
  82.     if(tmp==-1)
  83.     {
  84.         printf("blad podlaczenia watku two");
  85.         exit(1);
  86.     }
  87.         else if(tmp==0)
  88.     printf("status = %d\n",status);
  89.    
  90.    
  91.     printf("\ntid watku 1: %ld\n", one);
  92.     printf("tid watku 2: %ld\n", two);
  93.     printf("\nwatek pierwszy: %d\n", sum1);
  94.     printf("\nwatek drugi: %d\n", sum2);
  95.    
  96.     printf("suma: %d\n", sum1+sum2);
  97.  
  98.     tmp=pthread_detach(one);
  99.     if(tmp==-1)
  100.     {
  101.         printf("blad odlaczenia watku one");
  102.         exit(1);
  103.     }
  104.    
  105.     tmp=pthread_detach(two);
  106.     if(tmp==-1)
  107.     {
  108.    
  109.         printf("blad odlaczenia watku two");
  110.         exit(1);
  111.     }
  112.    
  113.    
  114. return 0;
  115.    
  116. }
Advertisement
Add Comment
Please, Sign In to add comment