Slmaan

Untitled

Apr 27th, 2022
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. /* t2.c
  2. synchronize threads through mutex and conditional variable
  3. To compile use: gcc -o t2 t2.c -lpthread
  4. SLMAAN ALBEADY
  5. 3.2.2 Conditional Variable
  6. */
  7. #include <pthread.h>
  8. #include <unistd.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11.  
  12. #define RED "\x1B[31m" //just color for a new word "again"
  13. #define RESET "\x1B[0m"
  14.  
  15. void hello(); // define two routines called by threads
  16. void world();
  17. void again(); //new*/ theard
  18. /* global variable shared by threads */
  19. pthread_mutex_t mutex; // mutex
  20. pthread_cond_t done_hello , done_world ; // conditional variable //new*/ done for second word
  21. int done = 0; // testing variable
  22.  
  23. int main (int argc, char *argv[]){
  24. pthread_t tid_hello, // thread id
  25. tid_world,
  26. tid_again; //new*/
  27. /* initialization on mutex and cond variable */
  28. pthread_mutex_init(&mutex, NULL);
  29. pthread_cond_init(&done_hello, NULL);
  30. pthread_cond_init(&done_world, NULL); //new*/ to enter a new thread
  31.  
  32.  
  33. pthread_create(&tid_hello, NULL, (void*)&hello, NULL); //thread creation
  34. pthread_create(&tid_world, NULL, (void*)&world, NULL); //thread creation
  35. pthread_create(&tid_again, NULL, (void*)&again, NULL); //new*/ thread creation again
  36.  
  37. /* main waits for the two threads to finish */
  38. pthread_join(tid_hello, NULL);
  39. pthread_join(tid_world, NULL);
  40. pthread_join(tid_again, NULL); //new*/
  41.  
  42. printf("\n");
  43. return 0;
  44. }
  45.  
  46. void hello() {
  47. pthread_mutex_lock (&mutex);
  48. printf ("Hello ");
  49. fflush (stdout); // flush buffer to allow instant print out
  50. done = 1;
  51. pthread_cond_signal (&done_hello); // signal world() thread
  52. pthread_mutex_unlock(&mutex); // unlocks mutex to allow world to print
  53. return ;
  54. }
  55.  
  56.  
  57. void world() {
  58. pthread_mutex_lock(&mutex);
  59.  
  60. /* world thread waits until done == 1. */
  61. while(done == 0) {
  62. pthread_cond_wait (&done_hello, &mutex);
  63. }
  64.  
  65. printf ("World");
  66. fflush (stdout);
  67. usleep(1);
  68. done=2; //new*/ to wait a world finish
  69. pthread_cond_signal (&done_world); //new*/
  70. pthread_cond_broadcast (&done_hello); //new*/ unblock all threads currently blocked on the specified condition variable cond.
  71.  
  72. pthread_mutex_unlock (&mutex); // unlocks mutex
  73.  
  74. return ;
  75. }
  76. //| ALL NEW : new thread for print therd word "Again" |*/
  77. void again(){
  78. pthread_mutex_lock(&mutex);
  79. /* again thread waits until done == 2. */
  80. while(done == 0) {
  81. pthread_cond_wait(&done_hello, &mutex);
  82. }
  83. while(done == 1) {
  84. pthread_cond_wait(&done_world, &mutex);
  85. usleep(1);
  86. }
  87.  
  88. printf (RED" Again!"RESET);
  89. fflush (stdout);
  90. pthread_mutex_unlock(&mutex); // unlocks mutex
  91. return ; //NULL
  92. }
  93. /*
  94. salbeady@penguin:~/os$ gcc -o t2 t2.c -lpthread
  95. salbeady@penguin:~/os$ ./t2
  96. Hello World Again!
  97. salbeady@penguin:~/os$ ./t2
  98. Hello World Again!
  99. */
Advertisement
Add Comment
Please, Sign In to add comment