Operating System Concepts Learning Note 12 process synchronization (ii) Pipe path
A basic, advanced synchronization construct, the Enhancement (monitor) type.
Use:
The pipe type provides a set of operations defined by the programmer that are mutually exclusive within the pipe. The representation of a pipe type includes the declaration of a set of variables (the values of these variables define the state of a type instance) and the implementation of the subroutines and functions that manipulate those variables. The type of enhancement indicates that it cannot be used directly for each process. Therefore, a subroutine defined within a pipe can only access variables and formal parameters that are locally declared within a pipe range. Similarly, a local variable of a tube can be accessed by a local subroutine.
The tube structure ensures that only one process can be active within a pipe at a time. Write synchronization code that does not need to be displayed. For specific synchronization scenarios, additional synchronization mechanisms are required, which are provided by the condition (condition) structure.
x,y; x.wait(); x.signal();
Syntax for enhancement:
monitor monitor name{ //shared variable declarations procedure P1(…){…}procedure P2(…){…}…procedure Pn(…){…}initialization code(…){…}}
The method of solving the problem of philosophers ' dining
The solution requires philosophers to pick up chopsticks when they are available in two chopsticks.
To do this, the following data structures are introduced:
enum {THINKING, HUNGRY, EATTING} state[5];
To add a condition, the philosopher I can set the variable state[i] to eating only if its two neighbors no longer dine:
(state[(i+4)%5]!=eating) and (state[i+1]%5!=eating)
Philosopher I must invoke the operation in the following order
dp.pickup(i)...eat...dp.putdown(i)
The process realization based on signal volume
A method for solving the problem of the Philosopher's meal based on the semaphore: each tube has a semaphore mutex (initialized to 1), and the process must execute wait (mutex) before it enters the pipe, and must execute signal (mutex) after leaving the pipe.
Monitor dp{enum{thinking,hungry,eating} State[5]; Condition self[5]; void Pickup (inti) { State[I]=hungry; Test (i);if( State[i]!=eating) Self[i].wait();} void Putdown (inti) { State[I]=thinking; Test ((i+4)%5); Test ((i+1)%5);} void Test (inti) {if(( State[(i+4)%5]!=eating) && ( State[I]==hungry) && ( State[(i+1)%5]!=eating)) { State[I]=eating; Self[i].signal ();}} Initialization_code () { for(intI=0;i<5; i++) State[I]=thinking;}}
The implementation of the condition variable: for each condition variable x, a semaphore x_sem and an integer variable x_count are introduced, both of which are initialized to 0. Since the signal process has to wait, another semaphore next is introduced for the signal process to hang itself, next_count to count the processes that hang on next.
Implementation of X.wait ():
x_count++;if0) signal(next);else signal(mutex);wait(x_sem);x_count--;
Implementation of X.signal ():
if(x_count>0){ next_count++; signal(x_sem); wait(next); next_count--;}
Cooler Process Restart
Wait for the longest process to rerun first. You can also use conditional wait constructs.
X.wait (c); where C represents a precedence value (priority number), which is stored with the name of the suspension process.
When using a pipe to manage resources, there are two conditions that must be checked to ensure that the system is correct:
First, the user process must always call the enhancement in the correct order;
Second, it is important to ensure that a non-cooperative process cannot simply ignore the mutex provided by enhancement and directly access the shared resource without complying with the agreement.
Operating System Concepts Learning Note 12 process synchronization (ii) Pipe path