Technology Sharing

OS_Synchronization and Mutex

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

2024-07-04: Notes on operating system synchronization and mutual exclusion


Concurrent processes or programs lose their closure when they are executed. That is, if two programs use a shared resource in different regions, the results of each run may be different.
insert image description here
The reason is that we did not protect the shared resource x that both program a and program b have to access.


9.1 Basic Concepts of Synchronization and Mutual Exclusion

9.1.1 Synchronization Relationship

The relationship of mutual restraint is synchronization. In simple terms, synchronization means that there must be an order when the processes are executed.
insert image description here


9.1.2 Mutually exclusive relationships

The indirect mutual restriction relationship is called a mutually exclusive relationship, e.g. a printer
insert image description here


9.1.3 Critical Resources

  • Resources that are only accessible to one process at a time are called critical resources (or exclusive resources).
  • For the sharing of critical resources, each process should adopt a mutually exclusive method

"Producer and Consumer Model"
insert image description herecounter+ +

首先把变量放到寄存器里面
register1 = counter;
接下来对寄存器进行一个自增
register1 = register1 + 1;
再把结果返回到counter里
counter = register1;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

counter- -

首先把变量放到寄存器里面
register2 = counter;
接下来对寄存器进行一个自减
register2 = register2 - 1;
再把结果返回到counter里
counter = register2;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

insert image description here

Solution: Treat counter as a critical resource and allow processes to access the variable counter exclusively (we will learn about synchronization mechanisms later)


9.1.4 Critical Sections

Regardless of whether it is a hardware critical resource or a software critical resource, multiple processes must access it mutually exclusively.
insert image description here
These areas are essentially codes

  • Enter the area
  • Critical Section
  • Exit Zone
  • Remaining area

9.1.5 Synchronization mechanisms should follow rules

  • Free time

When no process is in the critical section, it indicates that the critical section resources are idle. A process requesting to enter the critical section should be allowed to enter its own critical section immediately to effectively utilize resources.

  • Busy Waiting

When a process enters the critical section, it indicates that the critical resource is being accessed, so other processes trying to enter the critical section must wait to ensure mutual exclusive access to the critical resource.

  • Bounded Waiting

For processes that require access to critical resources, it should be ensured that they can enter their own critical section within a limited time to avoid falling into a "waiting to die" state.

  • Give up the right to wait

When a process cannot enter its critical section, it should release the processor (CPU) immediately to prevent the process from falling into a "busy waiting" state.


9.2 Software Synchronization Mechanism

9.2.1 Single-sign method

  • Set a public integer variable to indicate whether there is a process in the critical section. If there is a process in the critical section, wait through a loop check in the entry section. After the process leaves the critical section, modify the flag in the exit section.
  • The single flag method sets a common integer variable turn to indicate the process number allowed to enter the critical section. When turn=0, process P0 is allowed to enter the critical section.
  • When turn=1, process P1 is allowed to enter the critical section, and when it exits the critical section, the right to use the critical section is granted to another process (when Pi exits the critical section, turn=j)

insert image description here
insert image description here
两个进程必须交替进入临界区,若一个进程不再进入临界区,则另一个进程也无法进入临界区,违背了“空闲让进”准则


9.2.2 Double-sign check first method

The double-flag check method sets a Boolean array flag[2] to mark the willingness of each process to enter the critical section. flag[i]=true means that Pi wants to enter the critical section.

  • Before Pi enters the critical section, it first checks whether the other party has entered the critical section. If so, it waits
  • Otherwise, set flag[i] to true before entering the critical section
  • When Pi exits the critical section, set flag[i] to false

insert image description here

insert image description here

  • The while loop is equivalent to the traffic light mechanism
  • Setting the other party's flag is equivalent to changing the other party's traffic light
  • However, since both processes check the flag first [check the traffic light first], both are false [both are green] at the beginning, it is possible that the two processes pass the traffic light at the same time and enter the critical section together.

9.2.3 Double-mark post-inspection method

The double-flag post-check method checks the other party's flag and then sets its own. These two operations cannot be completed in one go, so the two processes may enter the critical section at the same time; so the post-check method was invented, first setting its own flag, then checking the other party's flag, if the other party's flag is true, wait; otherwise enter the critical section

insert image description here
insert image description here


9.2.4 Peterson Algorithm

Peterson's algorithm combinesSingle-sign methodandDouble sign post-inspectionThe idea is to use flag[] to solve the mutual exclusion access problem, and then use turn to solve the starvation problem.

If both parties are competing to enter the critical section, the process can give the opportunity to enter the critical section to the other party.
Before each process enters the critical section, it sets its own flag and sets the turn flag to allow entry. After that, it simultaneously detects the flag and turn of the other party to ensure that when both parties request to enter the critical section at the same time, only one process is allowed to enter.
insert image description here
Did not give up the right to wait
insert image description here


9.3 Hardware Synchronization Mechanism

It is difficult and very limited for software to solve the problem of mutually exclusive entry of each process into the critical section, so the computer provides some special hardware instructions to solve the problem.

9.3.1 Disable Interrupts

(1) Definition of disabling interrupts

  • Disabling interrupts is something we have seen in the computer organization theory. It means modifying a bit in the PSW register in the CPU. This bit will control whether the CPU will respond to the interrupt. This interrupt can only block maskable interrupts, but not non-maskable interrupts.
  • The scheduling of processes in the operating system depends on interrupts, such as clock interrupts.
  • When a process is executing in a critical section, the computer system turns off interrupts, so that scheduling will not be triggered, and there will be no process or thread switching.

(2) Disadvantages of disabling interrupts

  • Misuse of the terminal can lead to serious consequences
  • If the shutdown time is too long, it will affect the system efficiency
  • The interrupt disabling method is not applicable to multi-CPU systems. Disabling interrupts on one processor does not prevent the process from executing the same critical code on other processors.

9.3.2 Test-and-Set Instructions (TS Instructions)

insert image description here

The TS instruction can be considered as a function process (primitive) that is integral to the execution process.

  • There are two states of lock:
    • *lock=FALSE means the resource is idle
    • *lock=TRUE means the resource is being used

To use TS to manage critical sections, set a lock for each critical resource.

  • The initial value is FALSE, indicating that the critical resource is idle
  • Before the process enters the critical section, it first uses TS to test lock. If it is FALSE, it can enter and assign the TRUE value to lock, closing the critical resource.

9.3.3 Swap Instruction

insert image description here
Called the swap instruction, it is used to exchange the contents of two words

  • Set a global Boolean variable lock for each critical resource, with an initial value of FALSE
  • Use the above hardware instructions to complete process mutual exclusion
  • However, this method will also cause the access process to be constantly tested and in a "busy waiting" state, which does not conform to the "give up the right to wait" principle.

9.4 Semaphore Mechanism

9.4.1 Integer Semaphores

It is defined as an integer S used to represent the number of resources. There are only three operations on this integer semaphore: initialization (assigning initial value), wait (self-decrement), signal (self-increment)
insert image description here

  • Both wait and signal are atomic operations and cannot be interrupted during execution.
  • When a process is modifying a semaphore, no other process can modify the semaphore at the same time.

9.4.2 Recording Semaphores

Process synchronization mechanism without "busy waiting" phenomenon

  • In addition to an integer variable value used to represent the number of resources
  • It is also necessary to add a process linked list L to link all processes waiting for the resource.

insert image description here

  • The wait operation is equivalent to the P operation.
  • The signal operation is equivalent to the V operation.
  • Just two different names, the functions are exactly the same
  • wait(A) = P(A)
  • signal(B) = V(A)

(1) Using semaphores to implement process mutual exclusion

Set a mutual exclusion semaphore mutex=1, and then place the critical section for each process to access critical resources between wait(mutex) and signal(mutex)
insert image description here


(2) Using semaphores to achieve process synchronization

Set a synchronization semaphore S=0, so that the statement that needs to be executed first is signal(S), and then wait(S) is executed.
insert image description here