DEV Community

Cover image for Producer Consumer Problem | Operating System - M03 P02
Rahul Mishra
Rahul Mishra

Posted on • Originally published at programmingport.hashnode.dev

Producer Consumer Problem | Operating System - M03 P02

This is a multipart blog article series, and in this series I am going to explain you the concepts of operating system. This article series is divided into multiple modules and this is the third module which consists of 10 articles.

In this article we will discuss about producer consumer problem in an operating system.

Producer consumer problem

  • It is standard problem of multiprocessor synchronization.
  • In this we have two process one of producer and another of consumer and both the process are arriving at same time and they are sharing something (cooperative process.)

Consumer program

void consumer(void) {
    int itemC;
    while(true);
    {
        while(count == 0)
        itemC = Buffer(out);
        out = count - 1;
        count = count - 1;
        process_item(itemC); 
    }
}
Enter fullscreen mode Exit fullscreen mode

Producer program

int count = 0;
void poducer(void) {
    int itemP;
    while(true) {
        producer_item(itemP);
        while(count == n);
        Buffer[in] = itemP;
        in = (in+1)mod n;
        count = count + 1;
    }
}
Enter fullscreen mode Exit fullscreen mode
  • Here producer process will generate an item while executing and put it in buffer. Consumer will execute the code (consumer code) and then it will use item by taking out line from buffer and perform any task/process.
  • This is the ideal case in this case producer will put the item in buffer and consumer will take it out and use, and the buffer is completely field then it will start over again from zero.

This was all about producer consumer problem. Hope you liked it and learned something new from it.

If you have any doubt, question, queries related to this topic or just want to share something with me, then please feel free to contact me.

📱 Contact Me

Twitter
LinkedIn
Telegram
Instagram

📧 Write a mail

rahulmishra102000@gmail.com

🚀 Other links

GitHub
HackerRank

Top comments (0)