Advertisement
HmHimu

lab7.6

Nov 9th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. #include<pthread.h>
  4.  
  5.  
  6.  
  7. void *increment(void*);
  8.  
  9. void *watch(void*);
  10.  
  11. pthread_mutex_t my_mutex;
  12.  
  13. pthread_cond_t w_cond;
  14.  
  15. int count=0;
  16.  
  17.  
  18.  
  19. main()
  20.  
  21. {
  22.  
  23. pthread_t tid1, tid2, tid3;
  24.  
  25. pthread_mutex_init (&my_mutex, NULL);
  26.  
  27. pthread_cond_init(&w_cond, NULL);
  28.  
  29.  
  30.  
  31. pthread_create(&tid1,NULL,watch, NULL);
  32.  
  33. pthread_create(&tid2,NULL,increment, NULL);
  34.  
  35. pthread_create(&tid3,NULL,increment, NULL);
  36.  
  37.  
  38.  
  39. pthread_join(tid1,NULL);
  40.  
  41. pthread_mutex_destroy(&my_mutex);
  42.  
  43. pthread_cond_destroy(&w_cond);
  44.  
  45. printf("Parent is DONE ....\n");
  46.  
  47. exit(0);
  48.  
  49. }
  50.  
  51.  
  52.  
  53.  
  54.  
  55. void* increment(void *dummy)
  56.  
  57. {
  58.  
  59. while(count<10)
  60.  
  61. {
  62.  
  63. pthread_mutex_lock(&my_mutex);
  64.  
  65. printf("Cound =%d by thread %d\n",++count,pthread_self());
  66.  
  67. if(count==10)
  68.  
  69. pthread_cond_signal(&w_cond);
  70.  
  71. pthread_mutex_unlock(&my_mutex);
  72.  
  73. sleep(1);
  74.  
  75. }
  76.  
  77. return;
  78.  
  79. }
  80.  
  81. void* watch(void *dummy)
  82.  
  83. {
  84.  
  85. pthread_mutex_lock(&my_mutex);
  86.  
  87. pthread_cond_wait(&w_cond,&my_mutex);
  88.  
  89. pthread_mutex_unlock(&my_mutex);
  90.  
  91. printf("Watch is DONE...\n");
  92.  
  93. return;
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement