Advertisement
Guest User

tu wbijaj jak cos to lab 7 zad3

a guest
Jun 23rd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. #include <sys/types.h>
  2.  
  3. #include <sys/stat.h>
  4.  
  5. #include <fcntl.h>
  6.  
  7. #include <pthread.h>
  8.  
  9. #include <string.h>
  10.  
  11. #include <sys/file.h>
  12.  
  13. #include <stdlib.h>
  14.  
  15. #include <stdio.h>
  16.  
  17. #include <unistd.h>
  18.  
  19. #include <time.h>
  20.  
  21.  
  22.  
  23.  
  24.  
  25. void *fun_watek1(void * p);
  26.  
  27. void *fun_watek2(void * p);
  28.  
  29. struct liczby{
  30.  
  31. int x;
  32.  
  33. int y;
  34.  
  35. };
  36.  
  37.  
  38.  
  39. int retval;
  40.  
  41.  
  42.  
  43. struct liczby message;
  44.  
  45.  
  46.  
  47. int main() {
  48.  
  49. message.x = 4;
  50.  
  51. message.y = 2;
  52.  
  53.  
  54.  
  55. pthread_t watek1;
  56.  
  57. pthread_t watek2;
  58.  
  59.  
  60.  
  61. pthread_create(&watek1, NULL, fun_watek1, (void *)&message);
  62.  
  63. pthread_create(&watek2, NULL, fun_watek2, (void *)&message);
  64.  
  65.  
  66.  
  67. pthread_join(watek1, NULL);
  68.  
  69. printf("%d + %d = %d\n",message.x, message.y, retval);
  70.  
  71.  
  72.  
  73. pthread_join(watek2, NULL);
  74.  
  75. printf("%d - %d = %d\n",message.x, message.y, retval);
  76.  
  77.  
  78.  
  79.  
  80.  
  81. return0;
  82.  
  83. }
  84.  
  85.  
  86.  
  87. void *fun_watek1(void * p) {
  88.  
  89. printf("Watek 1: %lu, PID: %d\n", pthread_self(), getpid());
  90.  
  91.  
  92.  
  93. struct liczby * l = (struct liczby*) p;
  94.  
  95. retval = l->x+l->y;
  96.  
  97. return0;
  98.  
  99. }
  100.  
  101.  
  102.  
  103. void *fun_watek2(void * p) {
  104.  
  105. printf("Watek 2: %lu, PID: %d\n", pthread_self(), getpid());
  106.  
  107.  
  108.  
  109. struct liczby * l = (struct liczby*) p;
  110.  
  111. retval = l->x-l->y;
  112.  
  113. return0;
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement