A talk C Chestnut bar (100th time: C-language example-using semaphores for inter-process synchronization and mutual exclusion)

Source: Internet
Author: User
Tags semaphore

Ladies and gentlemen, crossing. Hello everyone, last time we talked about the process of synchronization and mutual exclusion of the sample, this time we say the example is: using the signal to process synchronization and mutual exclusion .

Gossip Hugh, words return to the positive. Let's talk C chestnuts together!

Crossing, the semaphore is a concept proposed by the famous computer scientist Dijkstra (Dijkstra), specifically designed to solve inter-process synchronization and mutual exclusion. In the concept he proposes, the semaphore is a nonnegative integer value .

The semaphore operation can only have two atomic operations :

    • wait for the signal;
    • Send a signal.

"What is atomic manipulation?" There are crossing under the table asking questions. An atomic operation means that an action cannot be interrupted by another action at run time, and it continues until the action is finished. For example, when we write code, suddenly receive an e-mail, this time the system will temporarily interrupt the code to write the program vim (I use VIM), and then let the mailbox client send a message to receive the notification. And then back to the vim to write the code in action. The action of using vim to write code in the sample is not an atomic operation, it is an atomic operation only if it cannot be interrupted by another action.

Next we describe the atomic operation on the semaphore.

Wait for the signal

The wait signal is also called P operation.

Example P (SEM) indicates a P operation on the signal volume SEM.

    • Assuming that the value of the SEM is greater than 0, the p operation subtracts the value of the SEM by 1.
    • Assuming that the value of the SEM is equal to zero, then the process of running P operation is suspended;
Send Signal

The sending signal is also called V operation. For example, V (SEM) indicates a V operation on the signal volume SEM.

    • If a process is suspended due to waiting for SEM, then the process of waking up waits;
    • Suppose no process is suspended because it waits for SEM. Then add the value of the SEM to 1.
Pseudo code

The following are pseudo-code that uses semaphores for process synchronization and mutual exclusion :

code     //非临界区的代码P(sem);             //运行P操作,进入临界区。运行临界区中的代码{    code;  //临界区代码    do something}V(sem);            //运行V操作,离开临界区code    //非临界区的代码

Assuming that the semaphore value in the pseudo-code above is 1, process a starts running the above pseudo-code. Before entering the critical section of the signal to carry out the P operation, then the value of the SEM to 0, and then process A to run the code in the critical section, this time process B also started to run the above pseudo-code, before entering the critical section of the signal to carry out the P operation, then process A has not left the critical area, the value of the Process B will be suspended. Until process a leaves the critical section to run the V operation, the value of the SEM changes to 1, and then the process B that waits for the SEM is awakened. Then process B enters the critical section and runs the code in the critical section.

Everyone can see. The p/v operation by the semaphore. can be guaranteed within the same time. There is only one process running the code in the critical section, which means that the process is synchronized and mutually exclusive.

Crossing, they are. The code is not written back in this chapter. Since we haven't yet used the semaphore. In the back of the chapter, we will describe the operation of the semaphore. And with a detailed example, the pseudo-code is converted into actual code.

You crossing, for examples of using semaphores for inter-process synchronization and mutual exclusion let's talk about this. I want to know what the following example, and listen to tell.

A talk C Chestnut bar (100th time: C-language example-using semaphores for inter-process synchronization and mutual exclusion)

Related Article

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.