
Modify code
Write a C program to simulate producer-consumer problem using semaphores.
TASK:
DESCRIPTION
Producer-consumer problem, is a common paradigm for cooperating processes. A producer process produces information that is consumed by a consumer process. One solution to the producer-consumer problem uses shared memory. To allow producer and consumer processes to run concurrently, there must be available a buffer of items that can be filled by the producer and emptied by the consumer. This buffer will reside in a region of memory that is shared by the producer and consumer processes. A producer can produce one item while the consumer is consuming another item. The producer and consumer must be synchronized, so that the consumer does not try to consume an item that has not yet been produced.
12.3PROGRAM
#include<stdio.h>
void main()
{
int buffer[10], bufsize, in, out, produce, consume, choice=0; in = 0;
out = 0; bufsize = 10;
while(choice !=3)
{
printf(“\n1. Produce \t 2. Consume \t3. Exit”);
printf(“\nEnter your choice: ”);
scanf(“%d”, &choice);
switch(choice) {
case 1: if((in+1)%bufsize==out)
printf(“\nBuffer is Full”);
else
{
printf(“\nEnter the value: “);
scanf(“%d”, &produce);
buffer[in] = produce;
in = (in+1)%bufsize;
}
Break; case 2:
if(in == out)
printf(“\nBuffer is Empty”);
else
{
consume = buffer[out];
printf(“\nThe consumed value is %d”, consume);
out = (out+1)%bufsize;
|
|
} |
|
|
break; |
} } } |
|
|

Trending nowThis is a popular solution!
Step by stepSolved in 2 steps with 1 images

- Memory (address space) is set aside at program startup to store things like text, data, and data structures (stack, heap). Describe the applications of stacks and heaps, and what the limits on their use are for a given process.arrow_forwardA program becomes a process when it is an active entity loaded in main memory. max 0 ↓ 介 What region is used for variables local to a function? no option is correct data stack text heap sparse address spacearrow_forwardThe fragmentation of the "external" memory allocation mechanism isarrow_forward
- It's important to understand precisely what "dynamic memory" is and how it differs from "static memory."arrow_forwardMain Memory (MM) and Central Processing Unit are important resources of the computer system that must be carefully managed. The part of Operating System (OS) that manages the memory is called Memory Manager. 'Partition' is a memory management scheme, which allocates processes in MM into a unique fixed-size partition; each partition contains exactly one process. When a partition is free, the OS selects a process from the input queue and loads it into the free partition; when the process terminates, the partition becomes available; a free partition is called 'hole'. Please, reply to the following questions by giving your own ORIGINAL and UNIQUE answers: a) Draw a picture of the Main Memory (MM) divided into A 'assigned partitions' and B 'holes': fix your values for A and B1, and draw the corresponding MM layout. In the following, Pi, Pj and Pk are three processes residing in MM b) Considering the 7 states diagram, answer to the following questions: 1) Can Pi be in the 'New' state? Why?…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





