2.3 Process synchronization

2.3.1 Basic concepts of process synchronization

In a multi-program environment, processes are executed concurrently, and there are different constraints between different processes.In order to coordinate the mutual constraints between processes, the basic concept of process synchronization is introduced.
Asynchrony: Each concurrently executing process advances at an independent and unpredictable speed.


1. Critical resources

许多资源只能为一个进程所用,我们将一次仅允许一个进程使用的资源称为临界资源
Access to critical resources must 互斥be
code to access critical resources is called 临界区

2. Sync

Synchronization is also known as direct control relationshipRefers to the existence of certain events in multiple processes先后顺序

3. Mutual
exclusion is also called indirect restrictionMeans that multiple processes are not allowed to use the same 临界资源


为了禁止两个进程同时进入临界区,同步机制应遵循以下原则:
1) Let in when idle
2) Wait when busy
3) Limited wait
4) Wait in power When the process cannot enter the critical area, the processor should be released immediately to prevent the process from waiting busy.

2.3.2 The basic method to achieve mutual exclusion of critical sections


1. Software realization method
1) a Algorithm: single notation
departing 空闲让进set variable turn, turn = 0 P0 allowed to enter, turn = 1 P1 allowed to enter. If P0 leaves smoothly, the critical area is free at this time, but P1 has no intention of entering the critical area, turn=1 is always established, and other processes cannot enter.
Insert picture description here

2) Algorithm 2: Double flag method first check

Before accessing the critical section, each process checks whether the critical resource is being accessed. To do this, set a data flag[i]. For example, the value of the i-th element is FALSE, which means that the process Pi has not entered the critical area, and TRUE is the critical area.
Insert picture description here
== advantages: == enter without alternating, continuous use
== disadvantages: == ①②③④ if performed in the order, while the two processes will enter the critical section departing 忙则等待Principles

3) Algorithm 3: Post-check after double-mark method

Algorithm 2 first checks the process status of the other party, and sets its own flag, which will cause the two processes to enter the critical section at the same time.
to this end算法三先将标志位设为TRUE,再检测对方标志,若对方为TRUE则进程等待,否则进入
Insert picture description here
When two processes want to enter the critical area almost at the same time, they set their flags flag to TRUE and detect the other's flag. When they find that the other party is also entering, the two processes do not enter the critical area.

导致饥饿现象

4) Algorithm 4: Peterson's Algorithm

In order to prevent two processes from waiting indefinitely, set the variable turn, and each process first sets its own flag and then sets its own turn turn表示谦让,flag表示意愿.

Insert picture description here
Insert picture description here
不遵循 “让权等待原则”will happen忙等


Second, the hardware implementation method

1) Interrupt shielding
The easiest way to prevent other processes from entering the critical section for access isProhibit all interrupts from occurring, or call it shielded interrupts, close interrupts

Insert picture description here
只适用于单处理系统(操作系统内核)


2) Hardware instruction method

TestAndSet instruction: This instruction is an atomic operation, execute the code不允许被中断

bool TSL(bool *lock){
    
    
bool old;
old = *lock;
*lock=true;
return old;
}
while(TSL(&lock));
//临界区代码段
lock = false;
//剩余区代码段

不满足让权等待原则

Swap instruction:也不满足让权等待原则

Guess you like

Origin blog.csdn.net/weixin_38220799/article/details/109105420