内核缓冲区 用户缓冲区_有界缓冲区问题

内核缓冲区 用户缓冲区

Bounded buffer problem, which is also called producer consumer problem, is one of the classic problems of synchronization. Let's start by understanding the problem here, before moving on to the solution and program code.

有界缓冲区问题(也称为生产者消费者问题 )是同步的经典问题之一。 在继续解决方案和程序代码之前,让我们首先了解这里的问题。

什么是问题陈述? (What is the Problem Statement?)

There is a buffer of n slots and each slot is capable of storing one unit of data. There are two processes running, namely, producer and consumer, which are operating on the buffer.

有一个n个插槽的缓冲区,每个插槽都可以存储一个数据单元。 有两个正在运行的进程( 生产者使用者)在缓冲区上运行。

Bounded Buffer Problem

Bounded Buffer Problem

有界缓冲区问题

A producer tries to insert data into an empty slot of the buffer. A consumer tries to remove data from a filled slot in the buffer. As you might have guessed by now, those two processes won't produce the expected output if they are being executed concurrently.

生产者尝试将数据插入缓冲区的空白插槽。 使用者尝试从缓冲区中已填充的插槽中删除数据。 您可能已经猜到了,如果这两个进程同时执行,将不会产生预期的输出。

There needs to be a way to make the producer and consumer work in an independent manner.

需要一种使生产者和消费者独立工作的方法。

这是一个解决方案 (Here's a Solution)

One solution of this problem is to use semaphores. The semaphores which will be used here are:

解决此问题的一种方法是使用信号量。 这里将使用的信号灯是:

  • m, a binary semaphore which is used to acquire and release the lock.

    m ,一个二进制信号量 ,用于获取和释放锁。

  • empty, a counting semaphore whose initial value is the number of slots in the buffer, since, initially all slots are empty.

    empty ,一个计数信号量,其初始值为缓冲区中的插槽数,因为最初所有插槽均为空。

  • full, a counting semaphore whose initial value is 0.

    full ,一个计数信号量,其初始值为0

At any instant, the current value of empty represents the number of empty slots in the buffer and full represents the number of occupied slots in the buffer.

在任何时候,empty的当前值代表缓冲区中的空插槽数,full值代表缓冲区中的已占用插槽数。

生产者经营 (The Producer Operation)

The pseudocode of the producer function looks like this:

生产者函数的伪代码如下所示:

do 
{
    // wait until empty > 0 and then decrement 'empty'
    wait(empty);   
    // acquire lock
    wait(mutex);  
    
    /* perform the insert operation in a slot */
    
    // release lock
    signal(mutex);  
    // increment 'full'
    signal(full);   
} 
while(TRUE)
  • Looking at the above code for a producer, we can see that a producer first waits until there is atleast one empty slot.

    查看上面的生产者代码,我们可以看到生产者首先等待直到至少有一个空插槽。

  • Then it decrements the empty semaphore because, there will now be one less empty slot, since the producer is going to insert data in one of those slots.

    然后它减少了信号量,因为现在将减少一个空插槽,因为生产者将在这些插槽之一中插入数据。

  • Then, it acquires lock on the buffer, so that the consumer cannot access the buffer until producer completes its operation.

    然后,它获取缓冲区上的锁,以便使用者在生产者完成其操作之前无法访问缓冲区。

  • After performing the insert operation, the lock is released and the value of full is incremented because the producer has just filled a slot in the buffer.

    执行插入操作后,由于生产者刚刚填充了缓冲区中的一个插槽,因此释放了锁定,并增加了full的值。

消费者运营 (The Consumer Operation)

The pseudocode for the consumer function looks like this:

消费者函数的伪代码如下所示:

do 
{
    // wait until full > 0 and then decrement 'full'
    wait(full);
    // acquire the lock
    wait(mutex);  
    
    /* perform the remove operation in a slot */ 
    
    // release the lock
    signal(mutex); 
    // increment 'empty'
    signal(empty); 
} 
while(TRUE);
  • The consumer waits until there is atleast one full slot in the buffer.

    使用者等待直到缓冲区中至少有一个完整的插槽。

  • Then it decrements the full semaphore because the number of occupied slots will be decreased by one, after the consumer completes its operation.

    然后,它减少了整个信号量,因为在用户完成其操作之后,占用的插槽数将减少一个。

  • After that, the consumer acquires lock on the buffer.

    之后,使用者获得缓冲区上的锁。

  • Following that, the consumer completes the removal operation so that the data from one of the full slots is removed.

    之后,使用者完成删除操作,以便删除完整插槽之一中的数据。

  • Then, the consumer releases the lock.

    然后,消费者释放锁。

  • Finally, the empty semaphore is incremented by 1, because the consumer has just removed data from an occupied slot, thus making it empty.

    最后,由于使用者刚刚从占用的插槽中删除了数据,因此使信号量增加了1。

翻译自: https://www.studytonight.com/operating-system/bounded-buffer

内核缓冲区 用户缓冲区

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值