Process and thread synchronization:

Process and thread synchronization is basically the same way, mainly lock and semaphore

1, lock

Locks use the exclusive and mutual exclusion of resources to ensure that processes (threads) are synchronized.

In simple terms, a process (thread) acquires a lock on a resource, which means that the process (thread) acquires the right to use the resource and refuses to be occupied by other processes (thread). However, other processes (threads) can acquire the resource, but are blocked until the resource lock is released.

2. Semaphore

The semaphore mechanism is similar to the locking mechanism. The semaphore records the number of resources, and updates the number of idle resources whenever a process (thread) requests a certain number of resources. Once a process (thread) requests more resources than it has idle resources, it will signal that it cannot get enough resources and block until it has enough resources.