Operating system operating systems process synchronization with semaphore processes synchronization and Semaphore
Process cooperation: Multi-process work together to complete a task
From the paper to the actual: producer −− consumer Instances
Sharing data
#define BUFFER_SIZE 10
typedef struct {...} item;
Item Buffer[buffer_size];
int in = out = Counter = 0; Note: These are user-state programs!
Producer Process
while (true) {
while (counter== buffer_size);
Buffer[in] = Item;
in = (in + 1)% Buffer_size;
counter++;
}
Consumer processes
while (true) {
while (counter== 0);
item = Buffer[out];
Out = (out + 1)% Buffer_size;
counter--;
}
Where to stop and when to go?
Need to let the "process stop and stop" to ensure a reasonable and orderly multi-process cooperation
This is the process synchronization
Producer Process
while (true) {
while (counter== buffer_size); the buffers are full and the producers stop.
Buffer[in] = Item;
in = (in + 1)% Buffer_size;
counter++;
The signal lets the consumer go again ...
Consumer processes
while (true) {
while (counter== 0) ; the cache is empty, consumers want to stop
item = Buffer[out];
Out = (out + 1)% Buffer_size;
counter--;
The signal lets the producer go again ...
Just signaling doesn't solve all the problems,
(1) The buffer is full after the producer P 1 produces a item put in, will sleep signal
(2) Another producer P 2 produces a item put in, will sleep
(3) Consumer C performs 1 cycles, counter==buffer_size-1, sends a signal to P 1, p 1 Wakeup
(4) Consumer C re-executes 1 cycles, counter==buffer_size-2, P 2 cannot be awakened
From signal to signal volume
not just wait for the signal, send a signal? Corresponds to sleep and wake up!
You should be able to record some information.
Can record "2 process Wait" on it ...
(1) Buffer full, p 1 execute, p 1 sleep, record the next 1 processes waiting
(2) P 2 execution, p 2 sleep, recording the next 2 processes waiting
(3) C executes 1 cycles, finds 2 processes waiting, wakeup a
(4) C to perform 1 cycles, to find a way to wait, again?
(5) C then perform 1 cycles, What should I do? What about P 3?
What is semaphore? Record some information ( volume ) and decide whether to sleep or wake ( signal ) based on this information.
Semaphore starts to work ...
Initial Time SEM =?
(1) Buffer full, p 1 executed, P 1 sleep sem = 1 What does it mean?
(2) P 2 performed,P 2 sleep sem = 2
(3) C performs 1 cycles,wakeup P 1 sem = 1
(4) C re-execute 1 cycles,wakeup P 2 sem = 0
(5) C again to perform 1 cycles, sem = 1 what meaning?
(6) P 3 execution,P 2 continue to perform sem = 0
Sum up: When is this integer-1? When does +1 sleep? When does it wake up?
What is semaphore? The definition of the semaphore ...
Semaphore: 1965, a special integer variable, presented by the Dutch scholar Dijkstra, is used to record the signal used for sleep and wakeup
struct semaphore
{
int value; Record the number of resources
PCB *queue;//Record the process waiting on the semaphore
}
P (semaphore s); Consumer Resources
V (semaphore s); Generate resources
The name of P is derived from the Dutch Proberen, which is the test
The name of V is also derived from the Dutch language Verhogen (increment)
P (semaphore s)
{
s.value--;
if (S.value < 0) {
Sleep (S.queue); }
}
Solving the problem of producer −− consumer with signal quantity
int fd = open ("Buffer.txt");
Write (fd, 0, sizeof (int)); Write in
Write (fd, 0, sizeof (int)); Write out define shared buffers with files
semaphore full = 0;
semaphore empty = Buffer_size; definition and initialization of signal volume
semaphore mutex = 1;
Producer (item) {
P (empty);
P (mutex);
Read in; Writes the item to the in position;
V (mutex);
V (full); }
Consumer () {
P (full);
P (mutex);
Read in Out; Read from the out position in the file to item; Print item;
V (mutex);
V (empty); }
Critical area protection critical section of signal volume
Co-modifying the problem of signal volume derivation
Initial situation
empty =-1; What does that mean?
Producer (item) {
P (empty);
...}
Producer P 1
Register = empty;
Register = register-1;
empty = register;
Producer P 2
Register = empty;
Register = register-1;
Empty = register;
One possible execution (dispatch)
P 1. Register = empty;
P 1. Register = P 1. register-1;
P 2. Register = empty;
P 2. Register = P 2. register-1;
empty = P 1. Register;
Empty = P 2. Register; What is the final empty equal to ?
Competitive conditions (Race Condition)
Competitive Conditions: shared data semantics errors related to scheduling
Implementation of sub-paragraph I
P 1. Register = empty;
P 1. Register = P 1. register-1;
P 2. Register = empty;
P 2. Register = P 2. register-1;
empty = P 1. Register;
Empty = P 2. Register;
Implementation of the first J
P 1. Register = empty;
P 1. Register = P 1. register-1;
empty = P 1. Register;
P 2. Register = empty;
P 2. Register = P 2. register-1;
Empty = P 2. Register;
Errors are caused by multiple process concurrent operations sharing data
Error and scheduling order, difficult to find and debug
Intuitive ideas for solving competitive conditions
Prevents other processes from accessing empty while writing a shared variable empty
Still the execution sequence.
P 1. Register = empty;
P 1. Register = P 1. register-1;
P 2. Register = empty;
P 2. Register = P 2. register-1;
empty = P 1. Register;
Empty = P 2. Register;
Producer P 1
Check and lock the empty
P 1. Register = empty;
P 1. Register = P 1. register-1;
Producer P 2
Check for empty locks ? A piece of code allows only one process to enter at a time
Producer P 1
empty = P 1. Register;
To unlock the empty
Producer P 2
Check and lock the empty
P 2. Register = empty;
P 2. Register = P 2. register-1;
empty = C.register;
To empty
Critical area (Critical section)
critical section: a piece of code that allows only one process to enter the process at a time
A very important work: find out the critical section code in the process the code for reading and writing the semaphore must be a critical section ...
Protection principle of critical section code
Basic principle: mutual exclusion: If a process executes in a critical section, other processes are not allowed to enter
The constraint relationships between these processes are called mutexes (mutual exclusion)
It's guaranteed to be a critical section.
The principle of good critical zone protection
free-to -enter: When several processes require access to an idle critical area, a process should be brought into the critical section as soon as possible
Limited wait: from the process to enter the request to allow access, can not wait indefinitely
Enter the critical section Peterson
Combining the two ideas of marking and rotation
Correctness of the Peterson algorithm
Satisfy Mutual exclusion entry:
If two processes are entered, then Flag[0]=flag[1]=true, turn==0==1, contradictions!
Satisfy the free-to-enter:
If the process P 1 is not in the critical section, then Flag[1]=false, or turn=0, all P 0 can enter!
Meet the Limited waiting:
P 0 is required to enter, Flag[0]=true; P 1 is not allowed to enter, since P 1 is executed once will let turn=0
What about multiple processes? −− Bread Shop Algorithm
It's still a combination of marks and rotations.
How to rotate: each process gets a sequence number with the smallest entry
How to tag: The sequence number is 0 when the process leaves, and the number that is not 0 is marked
Bakery: Each customer entering the store receives a number, the number of the smallest first to get the service, the number of the same, the first name before the service.
The correctness of the bakery algorithm
Mutex Entry: P I in the critical area, p K attempt to enter, there must be (Num[i], i) < (num[k],k), p K cycle wait.
free-to -enter: If no process is in the critical section, the process of the smallest ordinal must be able to enter.
Limited Wait: The process of leaving the critical section again enters a certain rank at the end (FIFO), so any one wants to enter the process at most n
Another type of solution for critical zone protection ...
Think about the critical section: what does it mean to allow only one process to enter and enter another process?
dispatched: Another process can only be executed if it is scheduled to go into the critical section, how can it prevent scheduling?
- When does it not make?
- Multi-CPU (multicore) ...
Question: Why not?
Hardware atomic instruction method for critical zone protection
Process P I
while (Testandset (&lock));
Critical section
lock = false;
Remaining area
think about it: How much CPU is not good to make?
[NO00003C] Operating system operating systems process synchronization with semaphore processes synchronization and Semaphore