Advertisement
Guest User

LAB3

a guest
Oct 14th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. Zadanie 1
  2. Kod:
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <pthread.h>
  6. #include <unistd.h>
  7.  
  8. void* Funkcja(void* arg) //watek 2
  9. {
  10. while(1)
  11. {
  12. printf("Watek 1 ma id: %d\n",pthread_self());
  13. fflush(stdout);
  14. sleep(1);
  15. sleep(10);
  16. }
  17. return(0);
  18. }
  19.  
  20. int main(void) //watek 1
  21. {
  22. pthread_t watek_2;
  23. //getchar();
  24. pthread_create(&watek_2,NULL,Funkcja,NULL);
  25. //while(1)
  26. //{
  27. printf("Watek 2 ma id: %d\n",pthread_self());
  28. fflush(stdout);
  29. sleep(1);
  30. //}
  31. return(0);
  32. }
  33.  
  34. Zadanie 2
  35. Kod:
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <pthread.h>
  39. #include <unistd.h>
  40.  
  41. void* Funkcja(void* arg) //watek 2
  42. {
  43. printf("Watek 1 ma id: %d\n",pthread_self());
  44. fflush(stdout);
  45. sleep(1);
  46. //sleep(4);
  47. pthread_exit(0);
  48. }
  49.  
  50. int main(void) //watek 1
  51. {
  52.  
  53. pthread_t watek_2;
  54. pthread_create(&watek_2,NULL,Funkcja,NULL);
  55. printf("Watek 2 ma id: %d\n",pthread_self());
  56. fflush(stdout);
  57. sleep(5);
  58. pthread_join(watek_2,NULL);
  59. return(0);
  60. }
  61.  
  62. Zadanie 3
  63. Kod:
  64.  
  65. #include <stdio.h>
  66. #include <stdlib.h>
  67. #include <pthread.h>
  68. #include <unistd.h>
  69.  
  70. void* Funkcja(int* arg) //watek 2
  71. {
  72. int cyfra;
  73. /
  74. printf("Watek 1 ma id: %d\n",pthread_self());
  75. fflush(stdout);
  76.  
  77. cyfra=*arg;
  78. cyfra=cyfra*cyfra;
  79.  
  80. pthread_exit(cyfra);
  81. }
  82.  
  83. int main(void) //watek 1
  84. {
  85. int cyfra, wynik, wynik_2;
  86. pthread_t watek_2;
  87. printf("Watek 2 ma id: %d\n",pthread_self());
  88. fflush(stdout);
  89. scanf("%d",&cyfra);
  90. pthread_create(&watek_2,NULL,&Funkcja,&cyfra);
  91. pthread_join(watek_2,&wynik_2);
  92. wynik = wynik_2;
  93. printf("Wynik: %d\n",wynik);
  94. fflush(stdout);
  95.  
  96. return(0);
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement