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

OS Lab Assignment 6 Bakery Simulation Suhani Bansal With Output

The document presents an OS lab assignment focused on the Producer-Consumer problem, implemented through a bakery simulation using semaphores in C. It includes code for producer and consumer threads that manage a circular buffer of bread items, utilizing semaphores for synchronization and a mutex for mutual exclusion. Sample output demonstrates the interaction between the producer and consumer as they produce and consume bread items.

Uploaded by

suhani bansal
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 views3 pages

OS Lab Assignment 6 Bakery Simulation Suhani Bansal With Output

The document presents an OS lab assignment focused on the Producer-Consumer problem, implemented through a bakery simulation using semaphores in C. It includes code for producer and consumer threads that manage a circular buffer of bread items, utilizing semaphores for synchronization and a mutex for mutual exclusion. Sample output demonstrates the interaction between the producer and consumer as they produce and consume bread items.

Uploaded by

suhani bansal
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

OS Lab Assignment 6

Name: Suhani Bansal

Roll Number: 2300290130193

Topic: Producer-Consumer Problem (Bakery Simulation using Semaphores)

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

#define SIZE 5 // buffer size

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

sem_t empty, full;


pthread_mutex_t mutex;

void* producer(void* p) {
int item;
for (int i = 0; i < 10; i++) {
item = rand() % 100;
sem_wait(&empty);
pthread_mutex_lock(&mutex);

buffer[in] = item;
printf("Baker produced bread %d at slot %d\n", item, in);
in = (in + 1) % SIZE;

pthread_mutex_unlock(&mutex);
sem_post(&full);
sleep(1);
}
return NULL;
}

void* consumer(void* c) {
int item;
for (int i = 0; i < 10; i++) {
sem_wait(&full);
pthread_mutex_lock(&mutex);

item = buffer[out];
printf("Customer bought bread %d from slot %d\n", item, out);
out = (out + 1) % SIZE;

pthread_mutex_unlock(&mutex);
sem_post(&empty);
sleep(2);
}
return NULL;
}

int main() {
pthread_t prod, cons;

sem_init(&empty, 0, SIZE);
sem_init(&full, 0, 0);
pthread_mutex_init(&mutex, NULL);

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


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

pthread_join(prod, NULL);
pthread_join(cons, NULL);

sem_destroy(&empty);
sem_destroy(&full);
pthread_mutex_destroy(&mutex);

return 0;
}
Sample Output

--- Sample Output ---

Baker produced bread 45 at slot 0


Customer bought bread 45 from slot 0
Baker produced bread 67 at slot 1
Customer bought bread 67 from slot 1
Baker produced bread 12 at slot 2
Customer bought bread 12 from slot 2
Baker produced bread 89 at slot 3
Customer bought bread 89 from slot 3
Baker produced bread 23 at slot 4
Customer bought bread 23 from slot 4
...
(continues until all 10 breads are produced and consumed)

You might also like