Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 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.  
  7. #define REENTRANT // informacja dla procesora ze aplikacja jest wielowatkowa
  8.  
  9. int create_th1;
  10. int create_th2;
  11. int create_th3;
  12.  
  13. int join_th1;
  14. int join_th2;
  15. int join_th3;
  16.  
  17. int detach_th1;
  18. int detach_th2;
  19. int detach_th3;
  20.  
  21. typedef struct _th1 {
  22. int tab[10];
  23. int sum;
  24. }_TH1;
  25.  
  26. typedef struct _th2 {
  27. int tab[2][10];
  28. int sum;
  29. int n;
  30. }_TH2;
  31.  
  32. void* _sum1(void* _th1) {
  33. _TH1 * th1 = (_TH1*)_th1;
  34. int i;
  35.  
  36. for(i = 0; i < 10; i++)
  37. th1->sum += th1->tab[i];
  38. pthread_exit(0);
  39. }
  40. void* _sum2(void* _th2) {
  41. _TH2* th2 = (_TH2*)_th2;
  42. int i,j,count = th2->n;
  43. for(i = 0;i < 2; i++)
  44. for(j = 0;j < count;j++)
  45. th2->sum += th2->tab[i][j];
  46. pthread_exit(0);
  47. }
  48.  
  49. int main() {
  50. id_self = pthread_self();
  51. srand(time(NULL));
  52.  
  53. pthread_t id_th1;
  54. pthread_t id_th2;
  55. pthread_t id_th3;
  56.  
  57. _TH1 th1,th2;
  58. _TH2 th3;
  59.  
  60. th1.sum = 0; th2.sum = 0;
  61. th3.sum = 0;
  62.  
  63. th3.n=3;
  64.  
  65. int _tab[2][10];
  66. int i,j;
  67. printf("* id main thread: %d\n", id_self);
  68.  
  69. for(i=0; i<2; i++){
  70. for(j=0; j<10; j++){
  71. _tab[i][j] = rand() % 10;//generowanie liczb losowych do macierzy
  72. printf("%d ",_tab[i][j]);
  73. th3.tab[i][j]=_tab[i][j];
  74. th1.tab[i]=_tab[0][j];//przypisywanie do struktur
  75. th2.tab[j]=_tab[1][j];// -||-
  76. }
  77. /*printf("[s]: wykonuje funkcje pthread_create oraz pthread_join... x2\n");*/
  78. create_th1=pthread_create(&id_th1, NULL, _sum1, &th1);
  79. if(create_th1==-1) perror("create1-error");
  80. join_th1=pthread_join(id_th1, NULL);
  81. if(join_th1==-1) perror("join1-error");
  82.  
  83. create_th2=pthread_create(&id_th2, NULL, _sum1, &th2);
  84. if(create_th2 == -1) perror("create2-error");
  85. join_th2=pthread_join(id_th2, NULL);
  86. if(join_th2==-1) perror("join2-error");
  87.  
  88. create_th3=pthread_create(&id_th3, NULL, _sum2, &th3);
  89. if(create_th3 == -1) perror("create3-error");
  90. join_th1=pthread_join(id_th3, NULL);
  91. if(join_th3==-1) perror("join3-error");
  92. /*printf("[s]: zakonczylem tworzenie i przylaczanie watkow... x2\n");*/
  93.  
  94. printf("Sum of first row: %d\n",th1.sum);
  95. printf("Suma drugiego wiersza: %d\n",th2.sum);
  96. printf("Suma calkowita: %d\n",th2.sum+th1.sum);
  97. printf("Suma n-pierwszych elementow danej tablicy: %d\n",th3.sum);
  98.  
  99. detach_th1=pthread_detach(id_th1);
  100. if(detach_th1==-1) perror("detach1-error");
  101. detach_th2=pthread_detach(id_th2);
  102. if(detach_th2==-1) perror("detach2-error");
  103. detach_th3=pthread_detach(id_th3);
  104. if(detach_th3==-1) perror("detach3-error");
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement