Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views5 pages

Lab7 RJ

The document describes a C program that simulates a bakery scenario using semaphores to address the Producer-Consumer problem. It includes code for creating producer and consumer threads, managing a buffer for bread production and consumption, and ensuring thread safety with mutexes. The program initializes semaphores for empty and full slots and demonstrates the coordination between bakers and customers.

Uploaded by

Raj Singh
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)
4 views5 pages

Lab7 RJ

The document describes a C program that simulates a bakery scenario using semaphores to address the Producer-Consumer problem. It includes code for creating producer and consumer threads, managing a buffer for bread production and consumption, and ensuring thread safety with mutexes. The program initializes semaphores for empty and full slots and demonstrates the coordination between bakers and customers.

Uploaded by

Raj Singh
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/ 5

LAB-7

By-Raj Singh
2300290130150

Imagine a bakery with bakers (producers) making bread and


customers (consumers) purchasing it. To avoid chaos, you must
coordinate produc on and consump on using semaphore signals.
Write a C program to simulate this bakery scenario using semaphores
to solve the Producer-Consumer problem.

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>

#define BUFFER_SIZE 5
#define NUM_PRODUCERS 2
#define NUM_CONSUMERS 3

int buffer[BUFFER_SIZE];
int in = 0, out = 0;

sem_t emptySlots;
sem_t fullSlots;
pthread_mutex_t mutex;

void* producer(void* arg) {


int id = *((int*)arg);
while (1) {
int bread = rand() % 100;

sem_wait(&emptySlots);
pthread_mutex_lock(&mutex);

buffer[in] = bread;
prin ("Baker %d baked bread %d at slot %d\n", id, bread, in);
in = (in + 1) % BUFFER_SIZE;

pthread_mutex_unlock(&mutex);
sem_post(&fullSlots);

sleep(rand() % 3);
}
}

void* consumer(void* arg) {


int id = *((int*)arg);
while (1) {
sem_wait(&fullSlots);
pthread_mutex_lock(&mutex);

int bread = buffer[out];


prin ("Customer %d bought bread %d from slot %d\n", id, bread, out);
out = (out + 1) % BUFFER_SIZE;

pthread_mutex_unlock(&mutex);
sem_post(&emptySlots);
sleep(rand() % 4);
}

int main() {
pthread_t producers[NUM_PRODUCERS], consumers[NUM_CONSUMERS];
int ids[NUM_PRODUCERS > NUM_CONSUMERS ? NUM_PRODUCERS :
NUM_CONSUMERS];

sem_init(&emptySlots, 0, BUFFER_SIZE);
sem_init(&fullSlots, 0, 0);
pthread_mutex_init(&mutex, NULL);

for (int i = 0; i < NUM_PRODUCERS; i++) {


ids[i] = i + 1;
pthread_create(&producers[i], NULL, producer, &ids[i]);
}

for (int i = 0; i < NUM_CONSUMERS; i++) {


ids[i] = i + 1;
pthread_create(&consumers[i], NULL, consumer, &ids[i]);
}

for (int i = 0; i < NUM_PRODUCERS; i++) {


pthread_join(producers[i], NULL);
}
for (int i = 0; i < NUM_CONSUMERS; i++) {
pthread_join(consumers[i], NULL);
}
sem_destroy(&emptySlots);
sem_destroy(&fullSlots);
pthread_mutex_destroy(&mutex);

return 0;
}

//OUTPUT

You might also like