Talk C chestnuts together (100th back: C language instance -- use semaphores for inter-process synchronization and mutex-1)
Hello, everyone. The last time we talked about the examples of inter-process synchronization and mutual exclusion, this example is as follows:Use semaphores for Process Synchronization and mutex. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!
As a concept proposed by the famous computer scientist Dijkstra, semaphores are used to solve inter-process synchronization and mutual exclusion. In his conceptSemaphores are non-negative integer values..
Semaphore operations can only beTwo atomic operations:
Waiting signal; Sending signal.
"What is an atomic operation ?" An atomic operation means that an action cannot be interrupted by other actions during operation and will continue until the operation is completed. For example, when we write code, we suddenly receive an email. At this time, the system will temporarily interrupt the code writing program vim (I use vim ), then, let the mailbox client send a notification to receive the email, and then restore it to the vim code writing action. In this example, writing code using vim is not an atomic operation. It is an atomic operation only when it cannot be interrupted by other operations. Next we will introduce atomic operations on semaphores.
WAITING SIGNAL
The waiting signal is also called P operation. In this example, P (sem) is used to perform P operations on semaphores sem.
If the sem value is greater than zero, the p operation will subtract 1 from the sem value. If the sem value is equal to zero, the process that executes the p operation will be suspended and a signal will be sent.
The Send signal is also called the V operation. For example, V (sem) indicates that the V operation is performed on the semaphores sem.
If a process is suspended due to waiting for sem, wake up the waiting process. If no process is suspended due to waiting for sem, add the sem value to 1. pseudo code
Below isUse semaphores for Process Synchronization and mutex pseudocode:
Nocritical code // non-critical code P (sem); // execute the P operation to enter the critical section and execute the code {critical code; // do something} V (sem); // execute the V operation to exit the nocritical code in the critical section. // code in the non-critical section
Assume that the semaphores sem value in the preceding pseudo-code is 1. process A starts to execute the above pseudo-code and performs the P operation on the semaphores before entering the critical section. Then, the sem value changes to 0, then process A executes the code in the critical section. At this time, process B also begins to execute the above pseudo code. before entering the critical section, process A performs the P operation on the semaphore. At this time, process A has not left the critical section, when the semaphores sem value is zero, process B will be suspended until process A leaves the critical section to perform the V operation, the sem value changes to 1, and then wake up process B waiting for sem, then process B enters the critical section and executes the code in the critical section.
We can see that through the P/V Operation of semaphores, only one process can execute the code in the critical section at the same time, that is to say, the process is synchronized and mutually exclusive.
The readers will not write code in this chapter, because we have not yet introduced how to use semaphores. In the subsequent chapter, we will introduce the operations of semaphores and use specific examples, convert pseudo code into actual code.
For more information, see the examples of using semaphores for inter-process synchronization and mutex. I want to know what examples will be provided later, and I will try again.