Introduction
Process Synchronization is a concept at the operating system level. In multi-program environments, there are different constraints. To coordinate these constraints and achieve resource sharing and process collaboration, this avoids conflicts between processes and introduces process synchronization.
Critical resources
In the operating system, a process occupies the smallest unit of resources (the thread can access all resources in the process, but the thread itself does not possess resources or only occupies a certain amount of required resources ). However, for some resources, they can only be occupied by one process at a time. The resources that can only be occupied by one process at a time are so-called critical resources. Typical critical resources, such as physical printers, variables and data shared by multiple processes in hard disks or memory (if such resources are not considered critical resources for protection, it is likely to cause data loss ).
For access to critical resources, mutual action is required. That is, when a critical resource is occupied, another process that applies for a critical resource will be blocked until the requested critical resource is released. The code used to access critical resources in a process becomes a critical zone.
The access process for the critical section is divided into four parts:
1. access zone: Check whether the critical zone is accessible. If yes, go to step 2. Otherwise, the process will be blocked.
2. critical section: perform operations in the critical section
3. Exit zone: Clear the Mark occupied by the critical zone
4. Remaining zone: code that is irrelevant to processes and Critical Zones
The concept of inter-process synchronization and mutual complaints
Process Synchronization
Process Synchronization is a direct constraint between processes. It is two or more threads established to complete a task, this thread needs to coordinate their work order in some locations and wait for and transmit information to control the relationship. The direct control relationship between processes comes from the cooperation between them.
For example, process a needs to read the information generated by process B from the buffer zone. When the buffer zone is empty, process B is blocked because it cannot read the information. When process a generates information and puts it into the buffer zone, process B is awakened. Concept 1.
Figure 1. synchronization between processes
Use the C # code to simulate synchronization between processes, as shown in Code 1.
Class processsyn {Private Static mutex mut = new mutex (); static void main () {console. writeline ("process 1 can be executed after process 2 is executed ....... "); thread thread1 = new thread (New threadstart (proc1); thread thread2 = new thread (New threadstart (proc2); thread1.start (); thread2.start (); console. readkey ();} Private Static void proc1 () {mut. waitone (); console. writeline ("thread 1 executes the operation .... "); thread. sleep (3000); mut. releasemutex (); // v operation} Private Static void proc2 () {mut. waitone (); // P operation console. writeline ("thread 2 executes the operation .... "); mut. waitone ();}}
Code 1.c# simulate synchronization between processes
The running result 2 is shown.
Figure 2. Running result
Process mutex
Process mutex is an indirect constraint between processes. When a process enters the critical zone to use critical resources, the other process must wait. Only when a process with critical resources exits the critical zone can the process be blocked.
For example, process B needs to access the printer, but process a occupies the printer, and process B will be blocked until process a releases the printer resources. Concept 3.
Figure 3. Mutual Exclusion between processes
Use C # To simulate mutual exclusion between processes. Here I started five threads, but only one thread can access critical resources at the same time. As shown in Code 2.
Class processmutex {Private Static mutex mut = new mutex (); Private const int numthreads = 5; static void main () {for (INT I = 0; I <= numthreads; I ++) {thread mythread = new thread (New threadstart (useresource); mythread. name = string. format ("thread {0}", I + 1); mythread. start ();} console. readkey ();} // synchronize Private Static void useresource () {// equivalent to the P operation mut. waitone ();/* the following code is the real work of the thread */console. writeline ("{0} has entered the critical section", thread. currentthread. name); random r = new random (); int rnum = R. next (2000); console. writeline ("{0} execution time: {1} ms", thread. currentthread. name, rnum); thread. sleep (rnum); console. writeline ("{0} has left the critical section \ r \ n", thread. currentthread. name);/* End of thread operation * // equivalent to V Operation mut. releasemutex ();} // mutually exclusive}
Code 2.c# simulate mutual exclusion between processes
The running result 4 is shown.
Figure 4.c# process mutex
The basic method to achieve mutex in the critical section
Hardware Implementation Method
The simplest way to achieve critical zones through hardware is to disable CPU interruption. From the computer principle, we know that the process switching of the cpu requires interruption. If the interruption is blocked, the code in the critical section can be successfully executed by the current process, thus implementing mutual exclusion. The process of this method is to block the interruption, execute the critical section, and enable the interruption. But this is not good, which greatly limits the processor's ability to execute tasks alternately. In addition, the permission for Guanzhong disconnection is handed over to the user code. If the user code is not opened after the interruption is blocked, will the system kneel down?
There is also a hardware command implementation method, which is exactly the same as the semaphore method. However, we will not elaborate on the hardware.
Semaphore Implementation Method
This is what we are familiar with p v Operations. By setting a semaphore s that represents the number of resources, the processes are mutually exclusive through the P and V operations on the semaphore S.
The P and V Operations are from the Dutch passeren and vrijgeven, respectively, indicating possession and release. The p v operation is the primitive of the operating system, meaning it is atomic.
The P operation first reduces the semaphores, indicating that a process will occupy or wait for resources, and then checks whether s is smaller than 0. If S is smaller than 0, it will be blocked. If it is greater than 0, it will occupy resources for execution.
The V operation is the opposite of the P operation. First, the semaphore is added, indicating that the number of processes occupying or waiting for resources is reduced by one. Then, check whether s is less than 0. If S is less than 0, wake up other processes waiting for s resources.
The synchronization and mutex of the above C # simulation process are actually implemented by semaphores.
Operating System-Process Synchronization and mutex