Use semaphore in Linux for mutual exclusion between processes

Source: Internet
Author: User

Semaphore is similar to the kernel object in windows. Once created, semaphore can be used by multiple processes. Of course, multiple threads in a process can also use semaphore for mutual exclusion. Speaking of mutex, the value of semaphore is generally 1. MAN 7 sem_overview can see a lot of useful information.

Like pipe, semaphore also has two types of names: one name and one name. It is easy to use between parent and child processes. well known, it is very convenient to use in multi-process with no boundaries.

Here I paste a piece of code, the code can best illustrate the problem, and others can refer to manual on OK.

/* Change umask temporarily to make sure the correct permission */
Old_mask = umask (0 );
Super_mmsem = sem_open ("/supermm", o_creat | o_rdwr, s_irusr | s_iwusr | s_irgrp | s_iwgrp | s_iroth | s_iwoth, 1 );
If (super_mmsem = sem_failed ){
Fprintf (stderr, "Create mm semaphore failed. Name: % s, reason: % s \ n", "/supermm", strerror (errno ));
Return 1;
}
Umask (old_mask );

............

/* Release after used */
If (super_mmsem! = NULL ){
Sem_close (super_mmsem );
Sem_unlink ("/supermm ");
}

 

1. Use umask to set the File Permission correctly. This is because all subsequent processes that are not root must be able to read and write this famous semaphore. Sem_open is similar to open. the permissions of the created files are the permissions we specify, that is, the permissions in the code above: s_irusr | s_iwusr | s_irgrp | s_iwgrp | s_iroth | the non-sum of s_iwoth and umask. That is: <the permissions we set> &~ Umask. This is why umask (0) is called. In Linux, the default umask value is 022. Therefore, if you want to set the permission to 666 ~ 022. The output is 644.

2. note that the first parameter of sem_open is the path of the famous Sem. Here, the path cannot be written as/tmp/AAA. SEM format, because in Linux, SEM is created under the/dev/SHM directory (Linux/dev/SHM is a special tmpfs, like/proc, in the same way as/sys, run the mount command.) and the SEM is added. therefore, the value in the code here is/supermm. The created file is/dev/SHM/SEM. supermm. Do not write the path. The name can start with/or.
3. like Windows's kernel object, SEM also has counter. Only processes that open the SEM are sem_close, And the SEM will be actually destroyed by the kernel. You can use sem_unlink to delete the file corresponding to the SEM.

After the image is created, you can use it. Like Windows Kernel Object, you can call sem_open to get an SEM handle first (of course, here the flag cannot write o_creat ), then sem_wait reduces the SEM value by 1. As the SEM definition defines that the SEM value cannot be less than 0, if the SEM value after sem_wait is less than 1, sem_wait will block, return is not returned until the SEM value is added, so as to achieve mutual exclusion. Sem_post is to add 1 to the SEM value.

If (super_mmsem = NULL)/* avoid create semaphore multi times */
{
Super_mmsem = sem_open ("/supermm", o_rdwr );
If (super_mmsem = sem_failed ){
/* How can we do now? */
Fprintf (stderr, "Open semaphore % s failed. Reason: % s \ n", "/supermm", strerror (errno ));
Return 1;
}
}

......

If (sem_wait (super_mmsem) =-1 ){
Fprintf (stderr, "Call sem_wait failed. Reason: % s \ n", strerror (errno ));
Return 1;
}

......

If (sem_post (super_mmsem) =-1 ){
Fprintf (stderr, "Call sem_post failed, this is fatal, maybe cause deadlock. Reason: % s \ n", strerror (errno ));
Return 1;
}

 

It is very convenient to use. Remember to use sem_close after use.

That's it. However, if you use the unknown semaphore, the APIs are different from these, such as sem_init. Therefore, if you use untitled SEM, refer to Linux manual.

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.