(process management) 03. Synchronization mode of process and thread

Time:2021-9-27

Synchronization mode of process and thread:

The synchronization methods of processes and threads are basically the same, mainly locks and semaphores

1. Lock

Lock is to use the exclusivity and mutex of resources to ensure the synchronization of processes (threads).

In short, a process (thread) obtains a lock on a resource, which is equivalent to that the process (thread) obtains the right to use the resource and refuses to be occupied by other processes (threads). However, other processes (threads) can get the lock, but they will be blocked. They can’t get the lock of the resource until the lock of the resource is released.

2. Semaphore

Semaphore mechanism is similar to lock mechanism. Semaphores will record the number of resources. Whenever a process (thread) requests a certain number of resources, it will update the number of free resources; Once a process (thread) requests more resources than the number of free resources, it will send a signal of insufficient resources to prompt that the process (thread) cannot obtain enough resources and block until there are enough resources.

Recommended Today

Question 30: how to understand cardinality sorting?

What is cardinality sorting? Basic idea: cardinality sorting is sorting first according to the low order, and then collecting; Then sort according to the high order, and then collect; And so on until the highest order Visual expression: it is to split each number according to its digits, and compare and sort each corresponding digit […]