PRODUCER – CONSUMER RELATIONSHIP Using c program |

AIM:
To write a c program in UNIX environment to implement the producer – consumer relationship. Operating System (OS) LAB in LINUX environment
PROGRAM DESCRIPTION:
Producer – consumer relationship is a type of interprocess communication. if one process a producer, is generating information that a second process, a consumer is using. They communicate by a single shared integer variable, number buffer. The producer does some calculations and then writes the result into number buffer; the consumer reads the data from number buffer and prints it. It is possible that the producer and consumer processes could run quite nicely in tandem, or their speeds could be grossly mismatched. if every time the producer deposits a result in number buffer the consumer immediately reads it and prints it, then the printed output will faithfully represents the stream of numbers generated by the producer.

But if the speeds of the processes are mismatched i.e., if the consumer is operating faster than the producer, the consumer could read and print the same number twice before the producer deposits the next number. if the producer is operating faster than consumer, the producer could overwrite its previous results before the consumer has had a chance to read it and print it; a fast producer could in fact do this several times so that many results would be lost.


ALGORITHM:
• Declare the function process1 and process2.
• Initialize two variables and pass it to first function.
• Generate the number up to value z.
• Generate the number using second function generated number are displayed on screen.
• Terminate the program.

PROGRAM SOURCE CODE
#include<stdio.h>
int pro1(int a,int z)
{
while(z>0)
{
a++;
z--;

}
return pro2(a,z);
}
int pro2(int a,int z)
{
pritnf("\n %d",a);
pritnf("process2");
pro1(a,z);
return 1;
}

void main()
{
int pro1(int a,int z);
int pro2(int a,int z);
int a=0,z=10;
pro1(a,z);

}
CONCLUSION:

Thus the c program to implement a producer- consumer relationship was written and executed successfully.

Related post