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

Content Beyond Syllabus - 1 Aim

This document describes a C program that implements the producer-consumer problem using semaphores. It defines producer and consumer threads that insert and remove random integers from a shared fixed-size buffer. Semaphores are used to synchronize access between the threads and prevent buffer overflows or underflows. The program successfully runs three producer and three consumer threads that insert and remove items from the buffer.

Uploaded by

Paras Gupta
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)
25 views3 pages

Content Beyond Syllabus - 1 Aim

This document describes a C program that implements the producer-consumer problem using semaphores. It defines producer and consumer threads that insert and remove random integers from a shared fixed-size buffer. Semaphores are used to synchronize access between the threads and prevent buffer overflows or underflows. The program successfully runs three producer and three consumer threads that insert and remove items from the buffer.

Uploaded by

Paras Gupta
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

CONTENT BEYOND SYLLABUS -1

AIM : Write a program to implement Producer-Consumer


problem using Semaphores.
SOURCE CODE:
#include <pthread.h>
#include <semaphore.h>
#include <stdlib.h>
#include <stdio.h>
#define MaxItems 3
#define BufferSize 3
sem_t empty;
sem_t full;
int in = 0;
int out = 0;
int buffer[BufferSize];
pthread_mutex_t mutex;
void *producer(void *pno)
{
int item;
for(int i = 0; i < MaxItems; i++) {
item = rand();
sem_wait(&empty);
pthread_mutex_lock(&mutex);
buffer[in] = item;
printf("Producer %d: Insert Item %d at %d\
n", *((int *)pno),buffer[in],in);
in = (in+1)%BufferSize;
pthread_mutex_unlock(&mutex);
sem_post(&full);
}
}
void *consumer(void *cno)
{
for(int i = 0; i < MaxItems; i++) {
sem_wait(&full);
pthread_mutex_lock(&mutex);
int item = buffer[out];
printf("Consumer %d: Remove Item %d from
%d\n",*((int *)cno),item, out);
out = (out+1)%BufferSize;
pthread_mutex_unlock(&mutex);
sem_post(&empty);
}
}
int main()
{
pthread_t pro[3],con[3];
pthread_mutex_init(&mutex, NULL);
sem_init(&empty,0,BufferSize);
sem_init(&full,0,0);
int a[5] = {1,2,3};
for(int i = 0; i < 3; i++) {
pthread_create(&pro[i], NULL,
(void *)producer, (void *)&a[i]);
}
for(int i = 0; i < 3; i++) {
pthread_create(&con[i], NULL,
(void*)consumer, (void *)&a[i]);
}
for(int i = 0; i < 3; i++) {
pthread_join(pro[i], NULL);
}
for(int i = 0; i < 3; i++) {
pthread_join(con[i], NULL);
}
pthread_mutex_destroy(&mutex);
sem_destroy(&empty);
sem_destroy(&full);
return 0;
}
RESULT : Programs for Producer-Consumer Problem using
semaphore was successfully implemented.

You might also like