OS review---process synchronization

Process synchronization refers to the existence of different restriction relationships in a multi-program environment. In order to coordinate this mutual restriction relationship, realize resource sharing and process cooperation, so as to avoid conflicts between processes, and then introduce process synchronization.

Critical resource

For some resources, they can only be occupied by one process at the same time, and these resources are called critical resources. Typical critical resources such as physical printers and so on.

Access to critical resources must be mutually exclusive, that is, when a critical resource is occupied, another process that applies for a critical resource will be blocked until the critical resource it applied for is released. The code that accesses critical resources in a process is called a critical section.

Process synchronization

Refers to two or more threads established to complete a task. These threads need to coordinate their work in certain positions while waiting and transmitting information.

For example, process B needs process A to be executed before it can be executed, so in order to wait for process A, process B will block first, and will not execute until process A is executed.

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-k09jxWW4-1615636902447)(https://s3-us-west-2.amazonaws.com/secure.notion -static.com/6b0ee893-ac6f-4763-b673-fb603ddb26ea/0603E3BA-6A21-4BC8-93D2-CD58C6625191.jpeg)]

Process mutual exclusion

After a process enters the critical section, another process that wants to enter the critical section must wait. Only when the process in the critical section exits the critical section, the waiting process can enter the critical section.

Methods to achieve mutual exclusion of critical sections

signal

That is PV operation. Set a semaphore S that represents the number of resources, and realize mutual exclusion of processes through P and V operations on S.

P is to occupy, that is, to perform a subtraction operation on S, which means that a process will occupy or wait for resources. If S ≥ 0, continue, otherwise the process will be blocked and put into the blocking queue.

V is to release, that is, to add operation to S, which means that a resource-occupying process has released the resource. If S≤0, a process will be awakened from the blocking queue for execution.

The difference between semaphore and mutex

  • Semaphore: used by multiple threads synchronously; after one thread completes a certain action, it tells other threads through a signal, and then other threads can perform certain actions;
  • Mutex: used by multiple threads for mutual exclusion; one thread occupies a certain resource, then other threads cannot access it, and other threads can access the resource until the thread leaves;

Producer consumer problem

Three semaphores are required: empty (empty resource), full (full resource), mut (mutual exclusion)

When the producer produces resources, it is P(empty) V(full), and when P(mut) comes out when entering the critical zone, V(mut).

When consumers consume resources, they use P(full) V(empty), and when they enter the critical section, P(mut) comes out and V(mut).

Philosopher's dinner

The philosopher only needs a semaphore, but his core lies in his control of chopsticks. When there are 5 philosophers:

/*当哲学家饥饿时,总是先拿左边的筷子,再拿右边的筷子*/
// P操作
wait(chopstick[i]);
wait(chopstick[(i+1)%5]);
// 吃饭
/*当哲学家进餐完成后,总是先放下左边的筷子,再放下右边的筷子*/
// V操作
signal(chopstick[i]);
signal(chopstick[(i+1)%5]);

The reader writes the question

The mutually exclusive semaphore wrMutex between readers and writers, the semaphore semaphore readCount representing the number of readers, and the mutually exclusive semaphore rMutex that protects readCount.

The mutually exclusive semaphore wrt, the initial value is 1, represents a shared file, and solves the mutual exclusion of "read-write" and "write-write" mutual exclusion.

A counter, the integer variable readcount, records the number of readers. The initial value is 0. Come to a reader, add 1 to readcount. When readcount = 1 means the first reader, you need to perform p operation to preempt the file; otherwise, it means that there are readers who are reading data safely. If one reader is taken, readcount is reduced by 1. When readcount = 0, it means that it is the last reader, and the v operation is required to release resources; otherwise, it means that there are still readers reading data.

So for the reader, before starting to read, you have to increase readCount by one, and this operation needs to use the rMutex mutual exclusion semaphore; after reading, you have to let readCount—, and this operation also needs the rMutex mutual exclusion semaphore. The middle operation needs to use wrMutex, first P and then V.

Writer, directly P and then V

int readcount=0; 
semaphore mutex=1, wrt=1 ; 
 
读者进程:
        wait (mutex);
	readcount++;	
	if (readcount == 1)
	       wait(wrt);
	signal (mutex);
	   reading is performed
	wait (mutex);
	readcount--;
	if (readcount == 0)
		signal (wrt);
	signal (mutex);
 
 
写者进程:     
      wait(wrt);
         writing is performed
      signal(wrt);

Banker's algorithm

When a process applies for the use of resources, the banker algorithm first tries to allocate the resources to the process, and then uses the security algorithm to determine whether the allocated system is in a safe state, if it is not safe, the trial allocation is invalidated, and the process continues to wait.

[External link image transfer failed. The origin site may have an anti-hotlinking mechanism. It is recommended to save the image and upload it directly (img-AXXPGOn0-1615636902448)(https://s3-us-west-2.amazonaws.com/secure.notion -static.com/52877484-62dd-49b3-b7a1-4f7b6dff6634/Untitled.png)]

[External link image transfer failed. The origin site may have an anti-hotlinking mechanism. It is recommended to save the image and upload it directly (img-o3fYkMl1-1615636902452)(https://s3-us-west-2.amazonaws.com/secure.notion -static.com/1a848e2c-3b36-47e3-b34c-439ff3d07551/Untitled.png)]

The main ways of thread synchronization are: Critical Section, Mutex, Semaphore, Event.

Their main differences and characteristics are as follows:

**1) Critical section: ** Access to public resources or a piece of code through the serialization of multiple threads, which is fast and suitable for controlling data access. Only one thread is allowed to access shared resources at any time,

If there are multiple threads trying to access public resources, then after one thread enters, other threads trying to access public resources will be suspended and wait until the thread that entered the critical section leaves. After the critical section is released, other threads will Can preempt.

**2) Mutex: **Using the mechanism of mutual exclusion. Only the thread that owns the mutex object has the permission to access the public resource. Because there is only one mutex, it can be guaranteed that the public resource will not be accessed by multiple threads at the same time.

Mutual exclusion can not only realize the safe sharing of public resources of the same application, but also realize the safe sharing of public resources of different applications.

**3) Semaphore: ** It allows multiple threads to access the same resource at the same time, but it needs to limit the maximum number of threads that access this resource at the same time.

4) Events:  By means of notification operations to maintain thread synchronization, it is also convenient to implement the operation of comparing the priority of multiple threads.

Guess you like

Origin blog.csdn.net/why1092576787/article/details/114760455