Question type 1 :PV operation
preface : This question will give us a task ( It can usually be done in several steps , Each step is a process ), Ask us to use PV Operation to realize the synchronization of the task , Something like this :
Let's start with a simple example ( producer - Consumer issues ) To understand PV operation :
A producer , A consumer , Common buffer :
The task can be divided into two parts ( That is, two processes ): process 1—— The producer produces a product and puts it into the buffer ;
process 2—— Consumer takes product from buffer for consumption . We just want to use PV Operation implementation process 1,2 Synchronization of .
Let's take a look at the complete code one :
begin //begin-end amount to {-} semaphore s1=1; // Indicates whether the buffer is empty , Initial value is 1 semaphore s2=0;
// Indicates whether the buffer is full , Initial value is 0 cobegin //cobegin And coend The purpose is to ensure that P production ( process ) And P consumption ( process ) Synchronous process P production
// use process Define process begin Ls: //Ls meaning L production , Also below Lx express L consumption Produce a product ; // Chinese must be written here , To explain pv operation
p(s1); //s1=s1-1=0, Means that the product is put in so that the buffer is not empty Product sent to buffer ; // Chinese must be written here , To explain pv operation v(s2);
//s2=s2+1=1, Means that the product is fetched and the buffer is empty goto Ls; //Ls and goto Ls And use the equivalent of while(true){
}, It is used to ensure that p,v Two operations exist in pairs , Avoid interruptions end process P consumption begin Lx: Fetch a product from the buffer ; p(s2);
Consume the product ; v(s1); goto Lx; end coend end
We can also replace code one with code two , as follows :
semaphore s1=1; semaphore s2=0; main() { cobegin shengchan(); xiaofei(); coend
} shengchan() { while(true){ //while(true){ }, It is used to ensure that p,v Two operations exist in pairs , Avoid interruptions
Produce a product ; // Chinese must be written here , To explain pv operation p(s1); //s1=s1-1=0, Means that the product is put in so that the buffer is not empty Product sent to buffer ;
// Chinese must be written here , To explain pv operation v(s2); } } xiaofei() { while(true){ Fetch a product from the buffer ; p(s2);
Consume the product ; v(s1); } }
We use code one for the exam .
Yes “ producer - consumer ” Let's take a look at the beginning of the article 3 Example questions , acquire a better understanding
There is a plate on the table , Only one fruit at a time . Dad put apples on the plate , Mom put oranges on the plate , My daughter only eats apples on the plate , My son only eats oranges on the plate . on trial P,V
Operation implementation parent , mother , son , Synchronization of female processes .
begin semaphore S1=1;//( Whether the disk is empty ) semaphore S2=0; //( Is there any apple on the plate ) semaphore S3=0;
//( Is there any orange on the plate ) cobegin process P father begin Lf: Take an apple ; P(S1); Put in the apple ; V(S2); goto Lf;
end process P mother begin Lm: Take an orange ; P(S1); Put in oranges ; V(S3); goto Lm; end process P son
begin Ls: P(S3); Take oranges from the plate ; V(S1); Eat oranges ; goto Ls; end process P female begin Ld: P(S2);
Take the apple from the plate ; V(S1); Eat apples ; goto Ld; end coend end
Software 2019 Real topic :
thank you “ Green peach and dew ”5 month 22 You gave it to me 8 Gross money
thank you “qq_52937762”5 month 23 You gave it to me 1.6 element
Technology