Synchronization of processes
City cooperation: Multi-process to complete the same task
?
Example 1:
driver ? Span style= "font-family: Italic" > ? ? ? ? ? ? ? ? ? ? ? ? conductor
While ) true) { ? ? ? ? ? ? ? ? ? While ) true) {
waiting to start the vehicle; ? ? ? ? ? ? ? ? ? ? To close the door; send a signal
? ? normal operation; ? ? ? ? ? ? ? ? ? ? ticketing;
? ? stop and send a signal ? ? ? ? ? ? waiting to open the door;
} ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
How do two processes work together in a synchronous way? Through the signal
Example 2: Document printing
Question: What kind of error will occur if the process is completely unaware of the other's existence? Practice
?
?
Practical Example: Producer-consumer examples
Sharing data
? These are the user states .
?
Producer Process
while (true) {
? ? While (counter = = buffer_size)
? ? ? ? ;//blocking ? buffer full, producer stopped
? ? Buffer[in] = Item;
? ? In = (in+1)%buffer_size;
? ? counter++;//signal to let consumers go
}
?
Consumer processes
while (true) {
? While (counter = = 0)
? ? ;//blocking ? Buffer empty, consumer stop
? Item=buffer[out];
? out = (out-1)%buffer_size;
? counter--;//signal for producers to go
}
?
Wait is the core of process synchronization
?
What is process synchronization? Where do I stop and where do I go?
process synchronization ? in fact, let the city take a walk and stop, to ensure that the multi-process cooperation of the rational and orderly
?
Just signaling doesn't solve all the problems,
1. Buffer full after producer P1 produce a item put in, will sleep
2. There is a producer P2 produces a item put in, will sleep
3. The consumer C executes 1 cycles, counter = = buffer_size-1, sends the signal to P1, P1 wake up.
4. Consumer C executes 1 cycles, counter = = BUFFER_SIZE-2,P2 cannot be awakened.
?
Counter cannot distinguish between how many producer processes are needed, not only the wake-up process (sending the signal), but also the amount of data that can be used to record the number of sleep processes to determine if a signal is to be signaled.
?
This leads to the signal volume .
??
From signal to signal volume
Not just wait for the signal, send a signal? Corresponds to sleep and wake
And I can record some information.
- Can record 2 processes waiting to be able to use a priority queue or a stack to store
- 1. Buffer full, P1 execution, P1 Sleep, recording the next process waiting ? sem =-1
- 2.p2 execution, P2sleep, record the next two processes waiting ?? sem =-2
- 3.C executes 1 cycles, finds two processes waiting, wakeup a wakeup P1? sem=-1
- C then perform 1 cycles, find a process waiting, wakeup a wakeup P2? sem=0
- C again performed 1 cycles 4, found no process waiting, SEM = 1
- P3 execution
Summarize:
When do I lose one?
When there is a process sleep, semaphore-1
When do you add one?
When the semaphore is found to be negative, the signal volume is +1 and the signal wakeup; If the signal is found to be positive, then the signal is not signaled and the amount of the signal is accumulated.
?
Problem: The number of a resource is 8, the current value of the semaphore corresponding to the resource is 2, description ():
The number of resources is 8, indicating that the initial SEM is 8, the current signal is 2, indicating that no process is waiting for this resource.
?
What is semaphore? definition of signal volume
Semaphore: 1965, a special variable, presented by the Dutch scholar Dijkstra, is used to record the signal used for sleep and wakeup
?
struct semaphore
{
? ? int value;//record number of data
? ? PCB *queue;//Record the process waiting on the semaphore
}
P (semaphore s); Consumer Resources
V (Semphore s); Generate resources
?
P (Semphore s)
{
? ? s.value--;
? ? if (S.value < 0) {
? ? ? ? sleep (s.queue);?
? ? }
}
?
Q: What is the code for V (s)?
V (Semphore s) {
? ? s.value++;
? if (s.value <= 0) {
? ? Wakeup (s.queue);
? }
}
?
Producer (item) {
? ? P (empty);
? ? P (mutex);
? ? reads in; Writes the item to the in position;
? ? V (mutex);
? ? V (full);
}
?
Constumer () {
? ? P (full);
? ? P (mutex);
? ? Read in Out, read from the file out location to item, print item ;
? ? V (mutex);
? ? V (empty);
}
?
Solving producer-consumer problems with semaphores
int fd = open ("Buffer.txt");
Write (fd, 0, sizeof (int)); Write in
Write (fd, 0, sizeof (int)); Write out
?
semaphore full = 0;
semaphore empty = Buffer_size;
semaphore mutex = 1;
?
How to understand the producer consumer Code with semaphores?
Full is used to determine if it is empty, and empty is used to determine whether it is filled. Sleep and wake processes through P and V, and release semaphores.
?
How do I implement mutually exclusive access?
When the buffer is entered, the value of the mutex is changed to 0 and is released after it is exhausted.
?
?
Process synchronization and Semaphores