0% found this document useful (0 votes)
14 views6 pages

Producer-Consumer Problem in C on Ubuntu

The document details a lab assignment on implementing the Producer-Consumer problem using the Banker's Algorithm in C on Ubuntu. It includes the aim, theory, step-by-step procedure, and the complete C program code. The assignment was successfully executed, demonstrating the use of threads and semaphores for process synchronization.

Uploaded by

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

Producer-Consumer Problem in C on Ubuntu

The document details a lab assignment on implementing the Producer-Consumer problem using the Banker's Algorithm in C on Ubuntu. It includes the aim, theory, step-by-step procedure, and the complete C program code. The assignment was successfully executed, demonstrating the use of threads and semaphores for process synchronization.

Uploaded by

Piyush Garg
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Operating System Lab Assignment

Topic: Banker’s Algorithm


Student Name: Khushi Chauhan
Roll Number: 202510116100126
Course : MCA
Project Title: Producer Consumer
Submitted To: Shishpal Jatav
Date: 05th November 2025
Environment Used: Ubuntu (GCC)

Aim
To implement the Producer–Consumer (Bounded Buffer) Problem
using threads and semaphores in C on Ubuntu Linux, using the vi
editor.

Theory
The Producer–Consumer problem is a classic process-
synchronization problem. A producer generates data and places it
into a bounded buffer. A consumer retrieves data from the buffer.
Semaphores ensure:

- Producer waits if buffer is full

- Consumer waits if buffer is empty

- Mutual exclusion using mutex.

Step-by-Step Procedure
1. Open Terminal: Ctrl + Alt + T

3. Open file in vi: vi producer_consumer.c


4. Press 'i' to enter INSERT mode.

5. Type/paste the C program.

6. Press Esc → :wq → Enter to save and exit.

7. Compile the program:

gcc producer_consumer.c -o producer_consumer -pthread


8. Run program:

./producer_consumer

C Program
#include <stdio.h>

#include <stdlib.h>

#include <pthread.h>

#include <semaphore.h>

#include <unistd.h>

#define BUFFER_SIZE 5

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;

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

item = rand() % 100;

sem_wait(&empty);

pthread_mutex_lock(&mutex);

buffer[in] = item;

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

in = (in + 1) % BUFFER_SIZE;

pthread_mutex_unlock(&mutex);

sem_post(&full);

sleep(1);

pthread_exit(NULL);

void *consumer(void *arg) {

int item;

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


sem_wait(&full);

pthread_mutex_lock(&mutex);

item = buffer[out];

printf("\tConsumer consumed: %d\n", item);

out = (out + 1) % BUFFER_SIZE;

pthread_mutex_unlock(&mutex);

sem_post(&empty);

sleep(2);

pthread_exit(NULL);

int main() {

pthread_t prod, cons;

sem_init(&empty, 0, BUFFER_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);

printf("\nExecution completed.\n");

return 0;

Result
The Producer–Consumer problem was successfully implemented,
compiled, and executed on Ubuntu using vi, pthreads, semaphores,
and mutex.

You might also like