0% found this document useful (0 votes)
0 views

week3

The document outlines a C program that implements the Producer-Consumer problem using semaphores and UNIX/Linux system calls. It details the algorithm for both producer and consumer processes, including semaphore initialization, mutex locking, and buffer management. The provided code demonstrates the creation of producer and consumer threads, along with synchronization mechanisms to manage access to a shared buffer.

Uploaded by

karunakar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

week3

The document outlines a C program that implements the Producer-Consumer problem using semaphores and UNIX/Linux system calls. It details the algorithm for both producer and consumer processes, including semaphore initialization, mutex locking, and buffer management. The provided code demonstrates the creation of producer and consumer threads, along with synchronization mechanisms to manage access to a shared buffer.

Uploaded by

karunakar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

WEEK-4

Write a C program to implement the Producer – Consumer problem using semaphores


using UNIX/LINUX system calls.

Aim:

Write a C program to implement the Producer – Consumer problem using semaphores


using UNIX/LINUX system calls.

Algorithm:
1. The Semaphore mutex, full & empty are initialized.
2. In the case of producer process
3. Produce an item in to temporary variable.
If there is empty space in the buffer check the mutex value for enter into
the critical section. If the mutex value is 0, allow the producer to add value
in the temporary variable to the buffer.
4. In the case of consumer process
i) It should wait if the buffer is empty
ii) If there is any item in the buffer check for mutex value, if the
mutex==0, remove item from buffer
iii) Signal the mutex value and reduce the empty value by 1.
iv) Consume the item.
5. Print the result
Code

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

#define BUFFER_SIZE 5 // Size of the buffer

int buffer[BUFFER_SIZE]; // Shared buffer


int in = 0, out = 0; // Index for producer and consumer

pthread_mutex_t mutex;
sem_t empty, full;

// Producer function
void* producer(void* arg) {
int item;
for(int i = 0; i < 10; i++) { // Producing 10 items
item = i + 1; // Produce an item

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


pthread_mutex_lock(&mutex); // Lock buffer access

buffer[in] = item; // Place item in buffer


printf("Producer produced: %d\n", item);
in = (in + 1) % BUFFER_SIZE; // Circular buffer

pthread_mutex_unlock(&mutex); // Unlock buffer


sem_post(&full); // Increase count of filled slots

sleep(1); // Simulate time taken to produce


}
return NULL;
}

// Consumer function
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 buffer access

item = buffer[out]; // Consume item


printf("Consumer consumed: %d\n", item);
out = (out + 1) % BUFFER_SIZE; // Circular buffer

pthread_mutex_unlock(&mutex); // Unlock buffer


sem_post(&empty); // Increase count of empty slots

sleep(2); // Simulate time taken to consume


}
return NULL;
}

int main() {
pthread_t prod, cons;

// Initialize semaphores
sem_init(&empty, 0, BUFFER_SIZE); // Buffer starts as empty
sem_init(&full, 0, 0); // No full slots initially
pthread_mutex_init(&mutex, NULL);

// Create producer and consumer threads


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

// Wait for threads to finish


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

// Destroy semaphores and mutex


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

return 0;
}
Output:

You might also like