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

Os Program 3

Uploaded by

priyaraikar071
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)
2 views

Os Program 3

Uploaded by

priyaraikar071
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
You are on page 1/ 3

Lab 03) Develop a C program to simulate producer-consumer

problem using semaphores.

#include <stdio.h>

#include <stdlib.h>

int mutex = 1;

int full = 0;

int empty = 10, x = 0;

void producer()

--mutex;

++full;

--empty;

x++;

printf("\nProducer produces item %d\n",x);

++mutex;

void consumer()

--mutex;

--full;

++empty;

printf("\nConsumer consumes item %d\n",x);

x--;
++mutex;

void main()

int n, i;

printf("\n1. Press 1 for Producer"

"\n2. Press 2 for Consumer"

"\n3. Press 3 for Exit");

for (i = 1; i > 0; i++)

printf("\nEnter your choice:");

scanf("%d", &n);

switch (n) {

case 1:

if ((mutex == 1)&& (empty != 0))

producer();

else

printf("Buffer is full!");

break;
case 2:

if ((mutex == 1) && (full != 0))

consumer();

else

printf("Buffer is empty!");

break;

case 3:

exit(0);

break;

You might also like