0% found this document useful (0 votes)
16 views3 pages

New Exp 6

The document contains a C program that implements a producer-consumer problem using threads, semaphores, and mutexes. It defines a shared buffer of fixed size, where a producer thread generates random items and a consumer thread consumes them, ensuring synchronization through semaphores and mutual exclusion with a mutex. The program initializes the necessary synchronization primitives, creates the producer and consumer threads, and cleans up resources upon completion.

Uploaded by

aryanrokade444
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views3 pages

New Exp 6

The document contains a C program that implements a producer-consumer problem using threads, semaphores, and mutexes. It defines a shared buffer of fixed size, where a producer thread generates random items and a consumer thread consumes them, ensuring synchronization through semaphores and mutual exclusion with a mutex. The program initializes the necessary synchronization primitives, creates the producer and consumer threads, and cleans up resources upon completion.

Uploaded by

aryanrokade444
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <stdio.

h>

#include <stdlib.h>

#include <pthread.h>

#include <semaphore.h>

#define BUFFER_SIZE 5 // Size of the shared buffer

int buffer[BUFFER_SIZE]; // Shared buffer

int in = 0; // Index for producer to insert an item

int out = 0; // Index for consumer to remove an item

sem_t empty; // Semaphore to count empty slots

sem_t full; // Semaphore to count filled slots

pthread_mutex_t mutex; // Mutex to ensure mutual exclusion

void* producer(void* arg) {

int item;

for (int i = 0; i < 10; i++) { // Producing 10 items

item = rand() % 100; // Produce a random item

sem_wait(&empty); // Wait for an empty slot

pthread_mutex_lock(&mutex); // Lock the buffer

buffer[in] = item;

printf("Producer produced: %d at index %d\n", item, in);

in = (in + 1) % BUFFER_SIZE; // Increment circular buffer index

pthread_mutex_unlock(&mutex);

sem_post(&full); // Signal that a slot is full

return NULL;

void* consumer(void* arg) {

int item;

for (int i = 0; i < 10; i++) { // Consuming 10 items


sem_wait(&full); // Wait for a filled slot

pthread_mutex_lock(&mutex); // Lock the buffer

item = buffer[out];

printf("Consumer consumed: %d from index %d\n", item, out);

out = (out + 1) % BUFFER_SIZE; // Increment circular buffer inde

pthread_mutex_unlock(&mutex); // Unlock the buffer

sem_post(&empty); // Signal that a slot is empty

return NULL;

int main() {

pthread_t prodThread, consThread;

sem_init(&empty, 0, BUFFER_SIZE);

sem_init(&full, 0, 0);

pthread_mutex_init(&mutex, NULL);

pthread_create(&prodThread, NULL, producer, NULL);

pthread_create(&consThread, NULL, consumer, NULL);

pthread_join(prodThread, NULL);

pthread_join(consThread, NULL);

sem_destroy(&empty);

sem_destroy(&full);

pthread_mutex_destroy(&mutex);

return 0;
}

You might also like