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

OS5

The document outlines an experiment aimed at synchronizing producer and consumer processes using semaphores. It details an algorithm for creating shared memory, initializing semaphores, and implementing the producer-consumer model through a C program. The result confirms successful synchronization between the processes for accessing shared memory.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

OS5

The document outlines an experiment aimed at synchronizing producer and consumer processes using semaphores. It details an algorithm for creating shared memory, initializing semaphores, and implementing the producer-consumer model through a C program. The result confirms successful synchronization between the processes for accessing shared memory.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

EX.NO:3 NAME: AKSHAYA K.S.

DATE:22.01.2025 ROLLNO:23ITB02

PRODUCER-CONSUMER PROBLEM

Aim:
To synchronize producer and consumer processes using semaphore.
Algorithm:
1. Create a shared memory segment BUFSIZE of size 1 and attach it.
2. Obtain semaphore id for variables empty, mutex and full using semget function.
3. Create semaphore for empty, mutex and full as follows:
a. Declare semun, a union of specific commands.
b. The initial values are: 1 for mutex, N for empty and 0 for full
c. Use semctl function with SETVAL command
4. Create a child process using fork system call.
a. Make the parent process to be the producer
b. Make the child process to the consumer
5. The producer produces 5 items as follows:
a. Call wait operation on semaphores empty and mutex using semop function.
b. Gain access to buffer and produce data for consumption
c. Call signal operation on semaphores mutex and full using semop function.
6. The consumer consumes 5 items as follows:
a. Call wait operation on semaphores full and mutex using semop function.
b. Gain access to buffer and consume the available data.
c. Call signal operation on semaphores mutex and empty using semop function.
7. Remove shared memory from the system using shmctl with IPC_RMID argument
8. Stop
Code:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

int mutex=1,full=0,empty=10,data=0;

void prod()
{
--mutex;
++full;
--empty;
data++;
printf("Produces item no.: %d\n",data);
++mutex;
}

void cons()
{
--mutex;
--full;
++empty;
printf("Consumes item no.: %d\n",data);
data--;
++mutex;
}

void main()
{
int n,i;
clrscr();
printf("Name: Akshaya K.S Roll No.:23ITB02");
printf("\n***Menu*** \n1-Produce \n2-Consume \n3-Exit");
for(i=1;i>0;i++)
{
printf("\nEnter choice: ");
scanf("%d",&n);

switch(n)
{
case 1:
if((mutex==1)&&(empty!=0))
prod();
else
printf("!Buffer is full!");
break;

case 2:
if((mutex==1)&&(full!=0))
cons();
else
printf("!Buffer is empty!");
break;
case 3:
exit(0);
break;

default:
printf("Invalid Choice");
}
}
getch();
}

Output:
RUBRICS MARKS

Aim 2

Algorithm / Procedure 3

Program / Experiment 5

Execution 3

Result 2

Viva-Voice 5

Record submission 5

Total 25

Result:
Thus synchronization between producer and consumer process for access to a shared
memory segment is implemented.

You might also like