HUST OS 4.5 operating synchronization and [PV] Self notes

Copyright: learning, can there be so many rules ~ https://blog.csdn.net/Irish_Moonshine/article/details/91128293

4.5 PV and synchronous operation

4.5.1 concept of synchronous and mutually exclusive
4.5.2 PV operating concept
4.5.3 PV operation to solve the problem of mutual exclusion
4.5.4 PV operation to solve synchronization problems
4.5.5 classical synchronization problems
4.5.1 The concept of process synchronization and mutual exclusion

Mutually exclusive relationship processes:
number of processes due to the sharing of exclusive resources, you must coordinate the process of the order of access to resources: ensuring that no two or more processes simultaneously access operation.
Exclusive resources and sharing of relevant
resources: the critical resource
access operation area: Critical Area

Synchronization relationship process:
Several cooperation process in order to complete a common task that requires coordination pace run. A process must request another process before the start of an operation has completed an operation, or in front of the process can only wait.

Synchronization relationship processes - Another explanation for
the process of cooperation needs to meet certain relationship or have an operation could be required to meet certain preconditions between certain operations, or can only wait.

4.5.2 PV operation concept

Lights concept
definition of PV operation of
PV operations to achieve process synchronization
classical synchronization problems

Lights is an effective process synchronization mechanism
in 1965, the Netherlands scholars Dijkstra's

State control process by lights during operation, and change the status lights.

  • Process controlled: lights status can be blocked or wake-up process.
  • Changing lights: The status lights can be changed process,

Data Structure of light:
light variable is defined as a binary vector (S, Q)
S: an integer, non-negative initial value (S, also known as a semaphore)
Q: the PCB queue (queue process), the initial value set is empty

struct SEMAPHORE{
	int S;
	pointer_PCB q;
};

Two operations:
P operation (function or procedure, P (S, Q)) V
operation (function or procedure, V (S, q))

P, V is Dutch: Passeren by, Vrijgeven released.

P(S,q)
{
	S = S - 1;
	if (S < 0){
		Insert(Caller , q);
		Block(Caller);
		转调度函数();
	}
}
  • P principle of operation (P (S, q), P (S)) S
    value minus 1;
    if the difference is greater than or equal to zero, the process continues;
    if the difference is less than zero, then the process blocked and added to the queue in q and transferred scheduling function.

Tip: P operation may make the process of blocking at the call.
Tip: S initial value is very important

  • The operating principle of V (V (S, q), V (S))
    S + 1'd value;
    if greater than zero, then the process continues;
    if less than zero, the process continues to wake up simultaneously in a process from q.
V(S,q){
	S = S + 1;
	if ( S < 0 ){
		Remove( q , pid );
		Wakeup( pid );
	}
}
4.5.3 PV operation to solve the problem of mutual exclusion

The essence is to achieve exclusive access to the critical region
allows up to a critical process in the region

  • Application procedure:
    first operations performed before entering the critical section P;
    then perform operations V after leaving the critical section;
main(){
	cobegin
	Pa();
	Pb();
	Pc();
	coend
}

Pa(){
	P(mutex );
	CSa;
	V(mutex );
}

Pb(){
	P(mutex );
	CSb;
	V(mutex );
}

Pc(){
	P(mutex );
	CSc;
	V(mutex );
}
4.5.4 PV operation to solve synchronization problems

Synchronization operation using the semaphore PV

  • Synchronization mechanisms essence:
    when operating conditions are not satisfied, let the process be suspended.
    When the operating conditions are met, allowing the process to continue immediately.

PV application and operation of process synchronization basic idea:
to suspend the current process: performing a P operation before the critical operations
can be paused if necessary
to continue the process: V operation after the implementation of critical operations
wake cooperation process if necessary,
meaningful definition of semaphore S, and set appropriate initial value.
Semaphore S can clear that "operating conditions."

Analysis of the conductor and the driver of the process of cooperation.

4.5.5 classical synchronization problems

Classical synchronization problems 1: Producer and consumer issues
a group of producers to provide goods (data) to a group of consumers, the shared buffer

Rules:
1. Do not fill the buffer to store the product
2. Do not take the product from an empty buffer
3. Each time only allows a producer or consumer deposit or take a product

Classic Synchronous Question 2: readers and editors problems

Problem Description: There is a book;
readers reading; there are more readers
have editors compiled the book; there are multiple editors

Requirements:
1. allow multiple simultaneous read pronunciation
2. allowed readers, editors simultaneously operate
3. does not allow more simultaneous operation editors

PV semaphore operation mechanisms solve the synchronization problem
1. distinguish critical operations or influence or operating conditions;
2. Before operating the key operation P
3. After the operation of the key operation V

Producer - consumer problem:
synchronous and mutually exclusive mix;

Readers - writers problem of
mutual exclusion problem;

Guess you like

Origin blog.csdn.net/Irish_Moonshine/article/details/91128293