MarouaneTheViper

Untitled

Dec 20th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.24 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. #include <semaphore.h>
  5. #include <unistd.h>
  6. #include <time.h>
  7.  
  8. /*
  9. This project was made with the aim of implementing semaphores to solve a simple 2 producer-consumer problem, where the first problem
  10. is between mCounter and mMonitor threads as one is trying to access a shared resource count to change it, the other trying to read it then
  11. erase its contents. The other problem is between mMonitor and mCollector where a thread wants to write a buffer the item read from
  12. the shared resource count, while the other is trying to read it. The solution made below was made using semaphores (one empty and one full semaphore
  13. for each problem). To simulate random messages arriving to the system, rand() function is used. However, since rand isn't thread safe, it was not
  14. used during the critical section of each thread but rather in the main function loop assigning identifiers and creating threads.
  15. */
  16.  
  17. #define SIZE 20
  18. #define SIZEBUFF 5
  19. #define t1 10
  20.  
  21. struct thread_args {
  22. int id;
  23. int random;
  24. };
  25.  
  26. struct Queue
  27. {
  28. int front, rear, size;
  29. int* array;
  30. int capacity;
  31. };
  32.  
  33. pthread_t mCounter[SIZE];
  34. pthread_t mMonitor, mCollector;
  35. sem_t s_full,s_empty,s_full1,s_empty1;
  36. int count=0;
  37. struct Queue* buffer;
  38.  
  39. struct Queue* createQueue(int capacity); // UTILITY FUNCTION THAT INITIALIZES THE BUFFER QUEUE ACCORDING TO SIZEBUFF
  40. void enqueue(struct Queue* queue, int item); // UTILITY FUNCTION THAT ENQUEUES A CERTAIN ELEMENT IN THE QUEUE, THE BUFFER IN THIS CASE
  41. int dequeue(struct Queue* queue); // UTILITY FUNCTION THAT DEQUEUES THE FIRST ELEMENT IN THE QUEUE, THE BUFFER IN THIS CASE
  42. void* collectorCSection(void* arg); // CORE FUNCTION THAT MANAGES THE MCOLLECTOR CRITICAL SECTION
  43. void* counterCSection(void* arg); // CORE FUNCTION THAT MANAGES THE MCOUNTER CRITICAL SECTION
  44. void* monitorCSection(void* arg); // CORE FUNCTION THAT MANAGES THE MMONITOR CRITICAL SECTION
  45.  
  46. int main()
  47. {
  48. int i;
  49. buffer = createQueue(SIZEBUFF);
  50. sem_init(&s_full,0,0); // INITIALIZING THE FULL SEMAPHORE TO 0 SINCE THE COUNT IS NOT YET INCREMENTED
  51. sem_init(&s_empty,0,1); // INITIALIZING THE EMPTY SEMAPHORE TO 1 SINCE THE COUNT IS NOT YET INCREMENTED
  52. sem_init(&s_full1,0,0); // INITIALIZING THE FULL SEMAPHORE TO 0, SINCE THERE ARE NO ELEMENTS IN THE BUFFER YET
  53. sem_init(&s_empty1,0,SIZE); // INITIALIZING THE EMPTY SEMAPHORE TO THE SIZE OF THE COUNTER THREADS ARRAY
  54. srand(time(NULL));
  55. for(i = 0; i < SIZE; i++)
  56. {
  57. struct thread_args *args = malloc (sizeof (struct thread_args));
  58. int random = rand()%20;
  59. args->id = random;
  60. args->random = i+1;
  61. pthread_create(&mCounter[i] , NULL , counterCSection , args); // CREATING MCOUNTER THREADS AND ASSIGNING THEM TO THEIR CRITICAL SECTION
  62. }
  63. pthread_create(&mMonitor,NULL,monitorCSection,NULL); // CREATING MMONITOR THREAD AND ASSIGNING IT TO ITS CRITICAL SECTION
  64. pthread_create(&mCollector,NULL,collectorCSection,NULL); // CREATING MCOLLECTOR THREAD AND ASSIGNING IT TO ITS CRITICAL SECTION
  65. for(i = 0; i < SIZE; i++)
  66. {
  67. pthread_join(mCounter[i],NULL); // JOIN MCOUNTER THREADS
  68. }
  69. pthread_join(mMonitor,NULL); // JOIN MMONITOR THREAD
  70. pthread_join(mCollector,NULL); // JOIN MCOLLECTOR THREAD
  71. return 0;
  72. }
  73.  
  74. void* counterCSection(void* arg)
  75. {
  76. struct thread_args *args = (struct thread_args *) arg; // RECEIVE THE ARG ARRAY COMING FROM THE MAIN FUNCTION
  77. int counter = args -> id +1;
  78. sleep(args->random); // SLEEP THE THREAD ACCORDING TO THE PSEUDO-RANDOM NUMBER GENERATED FOR IT IN MAIN()
  79. printf("\nCounter Thread %d: received a message.",counter);
  80. printf("\nCounter Thread %d: waiting to write.",counter);
  81. sem_wait(&s_empty1); // DECREMENT THE EMPTY SEMAPHORE
  82. count++; // INCREMENT THE COUNT INDICATING THE NUMBER OF THREADS WORKING THEIR CRITICAL SECTION AT THE MOMENT
  83. printf("\nCounter Thread %d: incrementing counter. Value = %d.",counter,count);
  84. sem_post(&s_full1); // INCREMENT THE FULL SEMAPHORE
  85. }
  86.  
  87. void* monitorCSection(void* arg)
  88. {
  89. int i=0;
  90. int temp;
  91. while(i<t1)
  92. {
  93. sleep(2); // SLEEP THE THREAD FOR 2 SECONDS TO SIMULATE THE THREAD WORKING AT 2 SEC INTERVALS
  94. printf("\nMonitor Thread: waiting to read.");
  95. sem_wait(&s_full1); // DECREMENT THE FULL SEMAPHORE
  96. temp = count; // STORE THE CURRENT COUNT OF WORKING THREADS
  97. printf("\nMonitor Thread: reading counter value = %d.",temp);
  98. sem_post(&s_empty1); // INCREMENT THE EMPTY SEMAPHORE
  99. sem_wait(&s_empty); // DECREMENT THE EMPTY SEMAPHORE FOR THE SECOND PROBLEM
  100. count=0; // SET THE COUNT OF WORKING TREADS TO 0
  101. enqueue(buffer,temp); // PLACE THE STORED COUNT IN THE BUFFER QUEUE
  102. sem_post(&s_full); // INCREMENT THE FULL SEMAPHORE FOR THE SECOND PROBLEM
  103. i++;
  104. }
  105. }
  106.  
  107. void* collectorCSection(void* arg)
  108. {
  109. int i=0;
  110. while(i<t1)
  111. {
  112. sleep(2); // SLEEP THE THREAD FOR 2 SECONDS TO SIMULATE THE THREAD WORKING AT 2 SEC INTERVALS
  113. sem_wait(&s_full); // DECREMENT THE FULL SEMAPHORE FOR THE 2ND PROBLEM
  114. dequeue(buffer); // GET A VALUE FROM THE BUFFER QUEUE
  115. sem_post(&s_empty); // INCREMENT THE EMPTY SEMAPHORE FOR THE 2ND PROBLEM
  116. i++;
  117. }
  118. }
  119.  
  120. struct Queue* createQueue(int capacity)
  121. {
  122. struct Queue* queue = (struct Queue*) malloc(sizeof(struct Queue));
  123. queue->capacity = capacity;
  124. queue->front = queue->size = 0;
  125. queue->rear = capacity - 1;
  126. queue->array = (int*) malloc(queue->capacity * sizeof(int));
  127. return queue;
  128. }
  129.  
  130. void enqueue(struct Queue* queue, int item)
  131. {
  132. if (queue->size == queue->capacity)
  133. {
  134. printf("\nMonitor Thread: Buffer full.");
  135. return;
  136. }
  137. queue->rear = (queue->rear + 1)%queue->capacity;
  138. queue->array[queue->rear] = item;
  139. queue->size = queue->size + 1;
  140. printf("\nMonitor Thread: Writing to buffer at index = %d.",buffer->size);
  141. }
  142.  
  143. int dequeue(struct Queue* queue)
  144. {
  145. if (queue->size == 0)
  146. {
  147. printf("\nCollector Thread: Buffer is empty.");
  148. return -1;
  149. }
  150. int item = queue->array[queue->front];
  151. queue->front = (queue->front + 1)%queue->capacity;
  152. queue->size = queue->size - 1;
  153. printf("\nCollector Thread: Reading from the buffer value = %d.",item);
  154. return item;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment