Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5.  
  6. #define MAX 10
  7. int buffer[MAX];
  8. int count = 0;
  9. pthread_mutex_t mutex;
  10. pthread_cond_t dataAvailable;
  11. pthread_cond_t spaceAvailable;
  12.  
  13. void *consumer(void *arg)
  14. {
  15. while(1)
  16. {
  17. pthread_mutex_lock(&mutex);
  18.  
  19. while(count == 0)
  20. {
  21. pthread_cond_signal(&spaceAvailable);
  22. printf("Signaling spaceAvailablen");
  23. pthread_cond_wait(&dataAvailable,&mutex);
  24. printf("Consumer: resume after the waitn");
  25. }
  26.  
  27. sleep(1);
  28.  
  29. printf("starting to empty the buffer...n");
  30. int j;
  31. for(j = 0; j < MAX; j++)
  32. {
  33. buffer[j] = 0;
  34. printf("Consumer: buffer[%d] = %dn",j,buffer[j]);
  35. --count;
  36. }
  37.  
  38. pthread_cond_signal(&spaceAvailable);
  39. printf("The value of count is: %dn",count);
  40. pthread_cond_wait(&dataAvailable,&mutex);
  41.  
  42. }
  43. }
  44.  
  45.  
  46. void *producer(void *arg)
  47. {
  48. while(1)
  49. {
  50. pthread_mutex_lock(&mutex);
  51.  
  52. while(count == 9)
  53. {
  54. pthread_cond_signal(&dataAvailable);
  55. printf("Signaling dataAvailablen");
  56. pthread_cond_wait(&spaceAvailable,&mutex);
  57. printf("Producer: resume after waitn");
  58. }
  59.  
  60. sleep(1);
  61.  
  62.  
  63. printf("starting to load the buffer...n");
  64. int j;
  65. for(j = 0; j < MAX; j++)
  66. {
  67. buffer[j] = j;
  68. printf("Producer: buffer[%d] = %dn",j,buffer[j]);
  69. ++count;
  70. }
  71.  
  72. pthread_cond_signal(&dataAvailable);
  73.  
  74. pthread_cond_wait(&spaceAvailable,&mutex);
  75. }
  76. }
  77.  
  78.  
  79. int main(int argc, char *argv[])
  80. {
  81. pthread_t thread;
  82.  
  83. pthread_create(&thread,0,producer,NULL);
  84. pthread_create(&thread,0,consumer,NULL);
  85. pthread_exit(0);
  86.  
  87. }
  88.  
  89. starting to load the buffer...
  90. Producer: buffer[0] = 0
  91. Producer: buffer[1] = 1
  92. Producer: buffer[2] = 2
  93. Producer: buffer[3] = 3
  94. Producer: buffer[4] = 4
  95. Producer: buffer[5] = 5
  96. Producer: buffer[6] = 6
  97. Producer: buffer[7] = 7
  98. Producer: buffer[8] = 8
  99. Producer: buffer[9] = 9
  100. starting to empty the buffer...
  101. Consumer: buffer[0] = 0
  102. Consumer: buffer[1] = 0
  103. Consumer: buffer[2] = 0
  104. Consumer: buffer[3] = 0
  105. Consumer: buffer[4] = 0
  106. Consumer: buffer[5] = 0
  107. Consumer: buffer[6] = 0
  108. Consumer: buffer[7] = 0
  109. Consumer: buffer[8] = 0
  110. Consumer: buffer[9] = 0
  111. The value of count is: 0
  112.  
  113. Semaphore s1, s2;
  114.  
  115. Producer:
  116. P(&s1)
  117. // do your operations
  118. print()
  119. V(&s2) //unlock the consumer thread
  120.  
  121. Consumer:
  122. P(&s2)
  123. // do your operations
  124. print()
  125. V(&s1) //unlock the producer thread
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement