ALL > Computer and Education > courses > university courses > undergraduate courses > Operating System > ZSTU class(2019-2020-1) > student directories > 2017329621055黄浩然 >
Homework 4黄浩然 Version 0
👤 Author: by 602592575qqcom 2019-10-09 10:44:11
I. producer-consumer problem

1. Problem descr iption: producer-consumer model describes that there are a group of producer processes producing products and providing these products to consumers in parallel, which has the typical characteristics of concurrent programs. PCM sets a buffer pool with multiple buffers between the producer and consumer processes to make them run concurrently. The producer process can put the products it produces into a buffer, and the consumer process can retrieve the products from a buffer to consume. Although all producer and consumer processes run asynchronously, they must be kept in sync, neither allowing the consumer process to take products from an empty buffer nor the producer process to put products into a full buffer.

2. Problem analysis: we can use an array to represent the buffer with n buffer units mentioned above. Using the input pointer in as the write pointer, in is added 1 every time the producer process produces and places a product. Similarly, the out pointer ACTS as a read pointer, increasing by 1 every time a consumer takes away a product. Since the buffer is a circular queue, add 1 to write in=(in+1)mod n, same as out. When (in+1)mod n==out, the buffer is full; when in==out, the buffer is empty. Count has also been introduced, adding 1 when the producer produces and puts it in, and subtracting 1 when the consumer takes it out.

3. Program descr iption:

Shared variables:

Int n, buffer [n].

Int the in and out; / / in and out (0, n - 1)

Int count; / / count (0, n]

Pseudocode:

Void producer () {

Producer an item in nextp;

While (count == n) {

Buffer [in] = nextp;

In = (in + 1)mod n;

Count++;

}

Void consumer () {

While (count = = 0) {

Nextc = buffer [out].

Out = (out + 1)mod n;

The count -;

Consumer the item in nextc; }

Please login to reply. Login

Reversion History

Loading...
No reversions found.