Classical process synchronization problem

Producers and consumers

problem

A group of producers to a group of consumer goods, they share a border buffer zone, to which the producer put the product, consumers get from the product.

rule

For producers: producers to produce a product (data), normally placed in the warehouse (buffer). If anyone is visiting the warehouse (buffer), you need to wait; if not then accessed. If the warehouse (buffer) is full , then waiting, waiting for a consumer a consumer product (data), so that the warehouse (buffers) discontent, and then wake up; if dissatisfied can be placed directly.
Here Insert Picture Description
For consumers: consumers consume a product (data), go to the warehouse (buffer) to pick up. If anyone is visiting the warehouse (buffer), then wait. If the warehouse (buffer) is empty , waiting for a producer to produce the product (data) for consumer spending; if it is empty then take a product (data) for consumption.
Here Insert Picture Description
The buffer is a critical resource , various process procedures of the shared buffer is a critical area, so there is a mutually exclusive issues.

Disposed two synchronizing signals, and an amount of a mutual exclusion semaphore
Empty: specifies the number of empty buffer locations, which initial value is a bounded buffer size n. That is how much warehouse space.
Full: specifies the number (i.e. the number of product) over the buffer unit, the initial value is 0. That warehouse has many products (how many positions are accounted for)
Mutex: Description of the bounded buffer is a critical resource that must be mutually exclusive use, the initial value of 1, to ensure that any time only one process using buffer .

Int in=0;out=0;
Item buffer[n];
Semaphore mutex=1,empty=n,full=0;
//生产者
void   procedure() {
               do{
      					produce an item in nextp;wait(empty);//检验是否满了,若empty<0则阻塞 不执行下面的代码
                            wait(mutex);//表示正在使用缓冲区
                            buffer(in)=nextp;
                            in=(in+1)%n;
                            signal(mutex);//释放缓冲区
                            signal(full);//将产品数+1 做到进程同步
   }while(TRUE);
}

//消费者
Void consumer(){
               do{ 
                         wait(full);//检查是否为空,若full<0则说明缓冲区没有产品消费了 阻塞进程
                          wait(mutex);//表示正在使用缓冲区
                          nextc=buffer(out);
                          out=(out+1)%n;
                           signal(mutex);//释放缓冲区
                           signal(empty);//空位+1 做到进程同步
            				consumer the item in nextc;}while (TRUE);
}

Void main() {
	cobegin 
	     procedure();
	     consumer();
	coend;
}

Note: The
two are not interchangeable wait position, because if the swap, the producers assume the process of implementation, taking up critical resources mutex, but this time found that the critical area is full ( empty <0 ), they put their obstruction did not release mutex; consumer resources at this time to come, find critical resources mutex (critical area) are being used will also clog, resulting in a deadlock.
Also two are not interchangeable position signal, similarly.

Dining Philosophers

problem

Five philosophers share a round table, were sitting on chairs around five, there are five bowls of macaroni and five chopsticks on a round table, their lifestyles are alternately thinking and eating. Usually, philosophers think, then tried to access its left and right sides closest to his chopsticks when hungry, when he got only two chopsticks to eat. After the meal, put down the chopsticks continue to think.
Here Insert Picture Description

analysis

Two philosophers can not lift a chopsticks, chopsticks are so critical resources, can only be in the hands of one person.
Solution: a signal recording type use a mutex to control the amount of chopsticks, constituting the semaphore array of five semaphore. It is described as follows:

var chopstick: array [0,,4] of semaphore;

Here Insert Picture Description

Readers write the problem

problem

Reading allows multiple processes simultaneously reading a shared object, because the read operation does not make the data file confusion. But it does not allow a Writer process and other processes or Reader Writer processes simultaneously access the shared object. Because such access would cause confusion.
Here Insert Picture Description

Recording semaphore

In order to achieve mutual exclusion between the Reader and Writer process when reading or writing and set up a mutex Wmutex. In addition, and then set an integer variable representing the number of readcount was reading process.

semaphore rmutex=1,wmutex=1;
int readcount=0;
void reader(){
            do  {
                    wait(rmutex);
                    if (readcount=0) wait(wmutex);
                       readcount++;
                    signal(rmutex);
                           …
                       perform read operation;wait(rmutex);
					readcount--;
					if (readcount=0) signal(wmutex);
					signal(rmutex);
					 }while(TRUE);
}
void writer() {
     do {
       wait(wmutex); 
       perform write operation;
       signal(wmutex);
     } while(TRUE);
  }
Void main() {
 cobegin
     reader();writer();
  coend;
}

As long as there is a Reader in the process of reading, they are not allowed to write Writer process. Therefore, only when readcount = 0, represents no progress in reading Reader, Reader process only needs to be performed wait (wmutex) operation; if successful wait (wmutex) operation, Reader can read process, accordingly, do readcount + 1 operation .
Similarly, only when the Reader process after performing readcount decremented its value is 0, it will be required to perform signal (wmutex) operation, so that the process of writing Writer.
Readcount is a critical resource that can be accessed more Reader process, therefore, should set a mutex rmutex for it.
Here Insert Picture Description
Here Insert Picture Description

Semaphore set

And in front of the audience - different write the problem, added a limitation that only allows a maximum of RN readers simultaneously read. For this purpose, and the introduction of a semaphore L, and the RN is given its initial value, by executing the wait (L, 1,1) operate to control the number of the reader, enters whenever a reader, it is necessary to wait execution (L, 1,1) operation, the L-value minus 1. When a reader into the reading RN, L will be reduced to 0, the first RN + 1 into the readers to read, because inevitably wait (L, 1,1) blocking operation failure.

int RN;
   semaphore   L=RN,mx=1;
Void reader() {
    do {
            Swait(L,1,1);
            //Swait(mx,1,0)语句起着开关的作用。只要无writer进程进入写,mx=1,reader进程就都可以进入读;一旦有writer进程进入写时,其mx=0,则任何reader进程就都无法进入读;
            Swait(mx,1,0);
                      …
           perform read operation;Ssignal(L,1);
      }while(TRUE);
}
void writer() {
    do {
    		//Swait(mx,1,1,L,RN,0)语句表示仅当既无writer进程在写(mx=1),又无reader进程在读(L=RN)时,writer进程才能进入临界区写。
            Swait(mx,1,1;L,RN,0);
			perform write operation;
			Ssignal(mx,1);
  }while(TRUE);
}
Void main() {
   cobegin
    reader();writer();
   coend;
  }

Summed up a quick afternoon ... tired
Here Insert Picture Description

Published 40 original articles · won praise 94 · views 9574

Guess you like

Origin blog.csdn.net/qq_41718454/article/details/104076381