Advertisement
CodenameDuchess

pthread

Apr 12th, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <semaphore.h>
  5. #include <unistd.h>
  6.  
  7. #define NUM_THREADS 4
  8. #define BUFFER_SIZE 5
  9.  
  10. sem_t Empty;
  11. sem_t Full;
  12. pthread_mutex_t mutex;
  13.  
  14. /* Threads */
  15. pthread_t tid[NUM_THREADS]; /* Thread ID */
  16. pthread_attr_t attr; /* Thread attributes */
  17.  
  18. void *producer(void *param);
  19. void *consumer(void *param);
  20. int insert_item(int threadid);
  21. int remove_item(int threadid);
  22.  
  23. /* Progress Counter */
  24. int cookieCount;
  25.  
  26. main(int argc, char *argv[])
  27. {
  28. long t;
  29.  
  30. /* Initialization */
  31.  
  32. pthread_mutex_init(&mutex, NULL); /* Initialize mutex lock */
  33. pthread_attr_init(&attr); /* Initialize pthread attributes to default */
  34. sem_init(&Full, 0, 0); /* Initialize full semaphore */
  35. sem_init(&Empty, 0, BUFFER_SIZE); /* Initialize empty semaphore */
  36. cookieCount = 0; /* Initialize global counter */
  37.  
  38.  
  39. /* Create the producer threads */
  40.  
  41. for(t = 0; t < NUM_THREADS; t++)
  42. {
  43. pthread_create(&tid[t], &attr, producer, NULL);
  44. printf("Creating producer #%d\n", tid[t]);
  45. }
  46.  
  47. /* Create the consumer threads */
  48. for(t = 0; t < NUM_THREADS; t++)
  49. {
  50. pthread_create(&tid[t], &attr, consumer, NULL);
  51. printf("Creating consumer #%d\n", tid[t]);
  52. }
  53.  
  54. /* Ending it */
  55. sleep(1);
  56.  
  57. printf("All threads are done.\n");
  58. pthread_mutex_destroy(&mutex);
  59. sem_destroy(&Empty);
  60. sem_destroy(&Full);
  61. printf("Cleanup is now Complete\n");
  62. exit(0);
  63. }
  64.  
  65.  
  66. void *producer(void *param)
  67. {
  68. int threadid;
  69.  
  70. for (int i = 1; i <= 5; i++)
  71. {
  72.  
  73. sleep(1);
  74. sem_wait(&Empty); /* Lock empty semaphore if not zero */
  75. pthread_mutex_lock(&mutex);
  76. if(insert_item(threadid))
  77. {
  78. fprintf(stderr, "Producer error.");
  79. }
  80.  
  81. pthread_mutex_unlock(&mutex);
  82. sem_post(&Full); /* Increment semaphore for # of full */
  83. }
  84. }
  85.  
  86. void *consumer(void *param)
  87. {
  88. int threadid;
  89.  
  90. for (int i = 1; 1 <= 5; i++)
  91. {
  92. sleep(1);
  93. sem_wait(&Full); /* Lock empty semaphore if not zero */
  94. pthread_mutex_lock(&mutex);
  95. if(remove_item(threadid))
  96. {
  97. fprintf(stderr, "Consumer error.");
  98. }
  99.  
  100. pthread_mutex_unlock(&mutex);
  101. sem_post(&Empty); /* Increments semaphore for # of empty */
  102. }
  103. }
  104.  
  105.  
  106. int insert_item(int threadid)
  107. {
  108. if(cookieCount < BUFFER_SIZE) /* Buffer has space */
  109. {
  110. cookieCount++;
  111. printf("Producer #%d inserted cookie. Total: %d\n", threadid, cookieCount);
  112. return 0;
  113. }
  114.  
  115. else /* Buffer full */
  116. {
  117. return -1;
  118. }
  119. }
  120.  
  121. int remove_item(int threadid)
  122. {
  123. if(cookieCount > 0) /* Buffer has something in it */
  124. {
  125. cookieCount--;
  126. printf("Consumer #%d removed cookie. Total: %d\n", threadid, cookieCount);
  127. return 0;
  128. }
  129.  
  130. else /* Buffer empty */
  131. {
  132. return -1;
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement