Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Lab No.

011

Lab Title: Producer Consumer Problem using Semaphores


Software used:

 VMware
 Ubuntu
 VI editor
Introduction:

Producer Consumer Problem using Semaphores. The producer consumer problem is a


synchronization problem. There is a fixed size buffer and the producer produces items and enters
them into the buffer. ... A producer should not produce items into the buffer when the consumer
is consuming an item from the buffer and vice versa.

Task 1: Write a C program to implement Producer and Consumer Problem using Semaphores.

Coding:

#include<stdio.h>

#include<stdlib.h> void producer(){

#include <string> if(e == 1){

#include <iostream> cout<<"Please enter the values in buffer";

#include<sys/types.h> for(int i = 0 ; i<5 ; i++){

#include<sys/wait.h> cin>>buffer[i]

class ABC{ }

ABC(){ e=0;

} f=1;

int s; }

int e;

int f; void consumer(){

int buffer[5]; if(f==1){


cout<<"Now buffer is getting empty"; class ABC = new ABC();

for(int i = 0 ; i<5 ; i++){ ABC obj;

cout<<buffer[i]<<"\n"; obj.producer();

buffer[i] =0; obj.consumer();

e=1; system("pause");

f=0; return 0;

} }

int main(){

output:

Conclusion:

In this lab we have study about the system call and its services. We have learned about the fork (),
wait () and exit () function in this lab. Also, we have executed different process using the above
command to run the program. We performed all the tasks given by the teacher.

You might also like