V15H4L

OS_Assign4a

Feb 16th, 2023
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.11 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <semaphore.h>
  3.  
  4. #define BUFFER_SIZE 10
  5.  
  6. int buffer[BUFFER_SIZE];
  7. int in = 0, out = 0;
  8.  
  9. sem_t empty;
  10. sem_t full;
  11. pthread_mutex_t mutex;
  12.  
  13. void *producer(void *arg) {
  14.     int item;
  15.  
  16.     while (true) {
  17.         // produce item
  18.         item = produce_item();
  19.  
  20.         // wait for empty slot in buffer
  21.         sem_wait(&empty);
  22.  
  23.         // acquire mutex to access buffer
  24.         pthread_mutex_lock(&mutex);
  25.  
  26.         // add item to buffer
  27.         buffer[in] = item;
  28.         in = (in + 1) % BUFFER_SIZE;
  29.  
  30.         // release mutex and signal full slot
  31.         pthread_mutex_unlock(&mutex);
  32.         sem_post(&full);
  33.     }
  34. }
  35.  
  36. void *consumer(void *arg) {
  37.     int item;
  38.  
  39.     while (true) {
  40.         // wait for filled slot in buffer
  41.         sem_wait(&full);
  42.  
  43.         // acquire mutex to access buffer
  44.         pthread_mutex_lock(&mutex);
  45.  
  46.         // remove item from buffer
  47.         item = buffer[out];
  48.         out = (out + 1) % BUFFER_SIZE;
  49.  
  50.         // release mutex and signal empty slot
  51.         pthread_mutex_unlock(&mutex);
  52.         sem_post(&empty);
  53.  
  54.         // consume item
  55.         consume_item(item);
  56.     }
  57. }
  58.  
  59. int main() {
  60.     // initialize semaphores and mutex
  61.     sem_init(&empty, 0, BUFFER_SIZE);
  62.     sem_init(&full, 0, 0);
  63.     pthread_mutex_init(&mutex, NULL);
  64.  
  65.     // create producer and consumer threads
  66.     pthread_t producer_thread, consumer_thread;
  67.     pthread_create(&producer_thread, NULL, producer, NULL);
  68.     pthread_create(&consumer_thread, NULL, consumer, NULL);
  69.  
  70.     // join threads
  71.     pthread_join(producer_thread, NULL);
  72.     pthread_join(consumer_thread, NULL);
  73.  
  74.     // destroy semaphores and mutex
  75.     sem_destroy(&empty);
  76.     sem_destroy(&full);
  77.     pthread_mutex_destroy(&mutex);
  78.  
  79.     return 0;
  80. }
  81.  
  82. /*
  83. The producer-consumer problem is a classical synchronization problem in computer science, where a producer thread produces data that is consumed by one or more consumer threads. The challenge is to ensure that the producer and consumer threads do not access the shared buffer simultaneously, which could lead to race conditions and data corruption.
  84.  
  85. To solve the producer-consumer problem using semaphores, we can use two semaphores: one to represent the number of empty slots in the buffer and one to represent the number of filled slots in the buffer. The buffer itself is shared memory that can be accessed by both the producer and consumer threads.
  86.  
  87. Above is a possible implementation in C using POSIX semaphores:
  88.  
  89. In this implementation, the producer() function produces an item and adds it to the buffer, while the consumer() function removes an item from the buffer and consumes it. The sem_wait() and sem_post() functions are used to wait for and signal the empty and full slots in the buffer, respectively. The pthread_mutex_lock() and pthread_mutex_unlock() functions are used to acquire and release the mutex to access the buffer.
  90.  
  91. By using semaphores to coordinate access to the shared buffer, we can ensure that the producer and consumer threads do not access the buffer simultaneously and avoid race conditions and data corruption.
  92. */
Add Comment
Please, Sign In to add comment