Operating System classroom notes (4) Process Synchronization and mutual exclusion in Process Management

Source: Internet
Author: User

4.3 Process Synchronization and mutex

4.3.1 interaction between processes

1. Category:

1) related processes and irrelevant processes (that is, whether they are logically related)

2) time-related errors

2. Process Synchronization

Process Synchronization refers to a direct cooperative working relationship between processes. It is a task that some processes work together to complete. The direct interaction between processes forms the synchronization of processes.

3. Process mutex

(1) process mutex

Some resources can only serve one process at a time. Therefore, these resources are mutually exclusive between processes. This relationship between processes is the mutual exclusion of processes. Indirect interaction between processes forms mutual exclusion between processes.

(2) critical section

Some resources in the system can only be used by one process at a time. Such resources are referred to as critical zone resources or shared variables. The program that accesses critical resources in a process is called a critical zone.

Rules for using the critical section: allow incoming requests with no blank waiting, select one among multiple conditions, select limited waiting, and authorize waiting.

The essence of process mutex is synchronization, which can be seen as a special process synchronization.

4. Synchronization Mechanism

Basic Requirements for the Process Synchronization Mechanism: 1) Strong descriptive ability, that is, to solve the problem of synchronization mutex between various processes 2) easy to implement and high efficiency 3) Easy to use

Existing synchronization mechanisms: hardware synchronization mechanisms; semaphore and P and V Operations; pipe processes; conditional critical domains; Path expressions (used in centralized systems); remote process calls (applicable to distributed systems).

4.3.3 process synchronization mechanism-semaphores, P, and V Operations

P and V are the first letters of "Waiting" and "sending signals" in Dutch text respectively. P operations and V Operations are synchronization primitives.

Each P operation is performed, which means one resource is allocated. Each V operation is performed to release one resource.

1) semaphores

A semaphore is a protected volume. Only the P, V Operations and semaphore initialization operations can access and change its value.

2) P and V Operations

Definition:

P (s ):

(1) S: = S-1;

(2) If S <0, the status of the process is set to the waiting status, then insert the PCB of the process into the corresponding S semaphore and wait until other processes execute the V operation on S.

V (s ):

(1) S: = S + 1;

(2) If S <= 0, release a process waiting in the S semaphore queue, change its status to the ready state, and insert it into the ready queue; then, the process of this operation continues to be executed.

Generally, the semaphore value can be interpreted as: the size of the S value indicates the number of resources of a certain type. When S> 0, there are resources available for allocation. When S <0, its absolute value indicates the number of processes waiting for the S semaphores in the queue.

3) Use P and V operations to achieve mutual exclusion between processes

So that the initial value of S is 1, and the program that processes A and B compete to enter the critical section can be written:

Process A process B

P (s );

Operation in critical section;

V (s );

4) semaphore and P and V Operation Summary

Weakness of P and V Operations: Because P or V Operations only add or subtract 1 at a time, that is to say, each time a P operation is executed, only one unit of resources can be requested to be allocated. Each time a V operation is executed, only one unit of resources can be released. Therefore, if a process needs to use multiple resources at a time, you need to perform multiple P operations in a row. You also need to perform the V Operation multiple times when releasing these resources. This not only increases the complexity of the program but also reduces the communication efficiency, resulting in a long wait time between processes, and may even lead to deadlocks.

P and V Operations must appear in pairs. If a P operation is performed, a V operation is required. When they are mutex operations, they are in the same process; when they are synchronous operations, they are not in the same process.

5) deadlock

(1) concept of deadlock

A deadlock occurs when two or more processes are blocked while waiting for another process to release resources. Neither of them can push forward, which is called a deadlock.

The reason for the deadlock is that shared resources are limited, multiple processes compete for shared resources, and the operation is improper.

(2) The four necessary conditions for deadlock generation are resource mutual exclusion, persistence and waiting, non-stripping, and cyclic waiting.

(3) solution to deadlock

There is generally a deadlock prevention, that is, damage to one or more of the four necessary conditions for deadlock, so that the system will never enter the Deadlock State;

Deadlock Prevention, that is, using a certain method to prevent the system from deadlocks during the process of dynamic resource allocation; and allowing the system to produce deadlocks, and then using detection algorithms to promptly discover and release it.

Security Status, Security Series, bankers algorithm, etc.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.