Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pthread.h>
- #include <semaphore.h>
- #define BUFFER_SIZE 10
- int buffer[BUFFER_SIZE];
- int in = 0, out = 0;
- sem_t empty;
- sem_t full;
- pthread_mutex_t mutex;
- void *producer(void *arg) {
- int item;
- while (true) {
- // produce item
- item = produce_item();
- // wait for empty slot in buffer
- sem_wait(&empty);
- // acquire mutex to access buffer
- pthread_mutex_lock(&mutex);
- // add item to buffer
- buffer[in] = item;
- in = (in + 1) % BUFFER_SIZE;
- // release mutex and signal full slot
- pthread_mutex_unlock(&mutex);
- sem_post(&full);
- }
- }
- void *consumer(void *arg) {
- int item;
- while (true) {
- // wait for filled slot in buffer
- sem_wait(&full);
- // acquire mutex to access buffer
- pthread_mutex_lock(&mutex);
- // remove item from buffer
- item = buffer[out];
- out = (out + 1) % BUFFER_SIZE;
- // release mutex and signal empty slot
- pthread_mutex_unlock(&mutex);
- sem_post(&empty);
- // consume item
- consume_item(item);
- }
- }
- int main() {
- // initialize semaphores and mutex
- sem_init(&empty, 0, BUFFER_SIZE);
- sem_init(&full, 0, 0);
- pthread_mutex_init(&mutex, NULL);
- // create producer and consumer threads
- pthread_t producer_thread, consumer_thread;
- pthread_create(&producer_thread, NULL, producer, NULL);
- pthread_create(&consumer_thread, NULL, consumer, NULL);
- // join threads
- pthread_join(producer_thread, NULL);
- pthread_join(consumer_thread, NULL);
- // destroy semaphores and mutex
- sem_destroy(&empty);
- sem_destroy(&full);
- pthread_mutex_destroy(&mutex);
- return 0;
- }
- /*
- 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.
- 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.
- Above is a possible implementation in C using POSIX semaphores:
- 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.
- 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.
- */
Add Comment
Please, Sign In to add comment