📜  C中的生产者消费者问题

📅  最后修改于: 2021-05-26 03:36:15             🧑  作者: Mango

生产者-消费者问题是多进程同步问题的一个示例。该问题描述了两个过程,共享公用固定大小缓冲区的生产者和使用者将其用作队列。

  • 生产者的工作是生成数据,将其放入缓冲区中,然后重新开始。
  • 同时,使用者正在一次消费一块数据(即,将其从缓冲区中删除)。

问题:给定公用的固定大小的缓冲区,任务是确保生产者在缓冲区已满时不能将数据添加到缓冲区中,而使用者不能从空缓冲区中删除数据。

解决方案:如果缓冲区已满,生产者将进入睡眠状态或丢弃数据。消费者下一次从缓冲区中删除项目时,会通知生产者,后者开始再次填充缓冲区。以相同的方式,如果消费者发现缓冲区为空,则可以入睡。生产者下一次将数据放入缓冲区时,将唤醒睡眠中的消费者。

注意:解决方案不足可能导致死锁,这两个进程都在等待被唤醒。

方法:想法是使用并行编程和关键部分的概念,使用OpenMP实现C语言中的生产者-消费者问题。

下面是上述方法的实现:

C
// C program for the above approach
  
#include 
#include 
  
// Initialize a mutex to 1
int mutex = 1;
  
// Number of full slots as 0
int full = 0;
  
// Number of empty slots as size
// of buffer
int empty = 10, x = 0;
  
// Function to produce an item and
// add it to the buffer
void producer()
{
    // Decrease mutex value by 1
    --mutex;
  
    // Increase the number of full
    // slots by 1
    ++full;
  
    // Decrease the number of empty
    // slots by 1
    --empty;
  
    // Item produced
    x++;
    printf("\nProducer produces"
           "item %d",
           x);
  
    // Increase mutex value by 1
    ++mutex;
}
  
// Function to consume an item and
// remove it from buffer
void consumer()
{
    // Decrease mutex value by 1
    --mutex;
  
    // Decrease the number of full
    // slots by 1
    --full;
  
    // Increase the number of empty
    // slots by 1
    ++empty;
    printf("\nConsumer consumes "
           "item %d",
           x);
    x--;
  
    // Increase mutex value by 1
    ++mutex;
}
  
// Driver Code
int main()
{
    int n, i;
    printf("\n1. Press 1 for Producer"
           "\n2. Press 2 for Consumer"
           "\n3. Press 3 for Exit");
  
// Using '#pragma omp parallel for'
// can  give wrong value due to
// synchronisation issues.
  
// 'critical' specifies that code is
// executed by only one thread at a
// time i.e., only one thread enters
// the critical section at a given time
#pragma omp critical
  
    for (i = 1; i > 0; i++) {
  
        printf("\nEnter your choice:");
        scanf("%d", &n);
  
        // Switch Cases
        switch (n) {
        case 1:
  
            // If mutex is 1 and empty
            // is non-zero, then it is
            // possible to produce
            if ((mutex == 1)
                && (empty != 0)) {
                producer();
            }
  
            // Otherwise, print buffer
            // is full
            else {
                printf("Buffer is full!");
            }
            break;
  
        case 2:
  
            // If mutex is 1 and full
            // is non-zero, then it is
            // possible to consume
            if ((mutex == 1)
                && (full != 0)) {
                consumer();
            }
  
            // Otherwise, print Buffer
            // is empty
            else {
                printf("Buffer is empty!");
            }
            break;
  
        // Exit Condition
        case 3:
            exit(0);
            break;
        }
    }
}


输出:

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。