About Sem_unlink when to delete semaphores

Source: Internet
Author: User
Tags sleep function probook

In The Man Handbook, Sem_unlink has this passage:

Sem_unlink () Removes the named semaphore referred to by name.  
is removed immediately. The semaphore is
Destroyed once all and processes that has the semaphore open close it.

This passage means that Sem_unlink will immediately delete the specified semaphore name, but wait until all processes that have opened the semaphore have closed the semaphore before deleting the signal. In detail, when a process creates a well-known semaphore, a sem.xxx file is generated under/DEV/SHM, and all processes that open the semaphore (including the process that created it) increase the reference count of the file, and the count is managed by the kernel. When Sem_unlink is called, the Sem.xxx file under/dev/shm is immediately deleted, but the semaphore itself is not deleted, and all processes that have opened the semaphore will still be able to use it normally. The kernel does not actually delete semaphores until all processes that have opened the semaphore have closed the semaphore.

Therefore, the signal volume and the sem.xxx are not altogether survival. Below to test the following:

/**/
#include <iostream>#include<fcntl.h>/*For O_* Constants*/#include<sys/stat.h>/*For Mode Constants*/#include<semaphore.h>#include<unistd.h>#include<cerrno>#include<cstring>intMain () {sem_t*psem = Sem_open ("/sem_test", O_creat,s_irwxu,1 ); if(sem_failed = =Psem) {Std::cout<<"Sem_open fail:"<< strerror (errno) <<Std::endl; return 0; }        intSval =0; Sem_getvalue (Psem,&sval); Std::cout<<"sleep now .... Value is"<< Sval <<Std::endl; Sleep ( - ); Sem_getvalue (Psem,&sval); Std::cout<<"wake up .... Value is"<< Sval <<Std::endl;    Sem_post (Psem); Sem_getvalue (Psem,&sval); Std::cout<<"raise value and try again ..... value is"<< Sval <<Std::endl; Sem_close (Psem);}
/** * * * sem_destruct.cpp*/#include<iostream>#include<fcntl.h>/*For O_* Constants*/#include<sys/stat.h>/*For Mode Constants*/#include<semaphore.h>#include<cerrno>#include<cstring>intMain () {sem_t*psem = Sem_open ("/sem_test", O_creat,s_irwxu,1 ); if(sem_failed = =Psem) {Std::cout<<"Sem_open fail:"<< strerror (errno) <<Std::endl; return 0; } std::cout<<"decrease value and remove sem now ..."<<Std::endl;        Sem_wait (Psem);    Sem_close (Psem); Sem_unlink ("/sem_test" ); Std::cout<<"sem should remove from/dev/shm,but not really remove"<<Std::endl;}

Compile the above file separately:

g++-pthread-o sem_create sem_create. CPP g+ +-pthread-o sem_destruct sem_destruct. CPP

Run Sem_create first, then run sem_destruct and you can see the output as:

[email protected]:~/code/sem$./sem_createsleep1[email protected]- hp-probook-4446s:~/code/sem$.//dev/01

As can be seen, even if sem_destruct from/dev/shm to remove the semaphore, LS/DEV/SHM also confirm that the corresponding file deleted. However, the sem_create process after sleep can still operate on the semaphore after wake up.

As you can see, once a well-known semaphore is opened, it doesn't matter much about the name. Let's test the well-known semaphore call Sem_unlink, but there are still processes in use, and another process is creating a semaphore with the same name.

/** * * * sem_again.cpp*/#include<iostream>#include<fcntl.h>/*For O_* Constants*/#include<sys/stat.h>/*For Mode Constants*/#include<semaphore.h>#include<unistd.h>#include<cerrno>#include<cstring>intMain () {sem_t*psem = Sem_open ("/sem_test", o_creat| O_excl,s_irwxu,1 ); if(sem_failed = =Psem) {Std::cout<<"Sem_open fail:"<< strerror (errno) <<Std::endl; return 0; }        intSval =0; Sem_getvalue (Psem,&sval); //std::cout << "Sleep now ..... value is" << sval << Std::endl; //sleep (+);Sem_getvalue (Psem,&sval); Std::cout<<"value is"<< Sval <<Std::endl;    Sem_post (Psem); Sem_getvalue (Psem,&sval); Std::cout<<"raise value and try again ..... value is"<< Sval <<Std::endl;    Sem_close (Psem); Sem_unlink ("/sem_test" );}

Compile: g++-pthread-o sem_again sem_again.cpp, then run Sem_create, Sem_destruct, Sem_again

 xzc[email protected]:~/code/sem$./sem_create  sleep  now .... value is 1  [email  Protected] -hp-probook-4446s:~/code/sem$./sem_destructdecrease value and Remove SEM now...sem should remove from /dev/shm,but not really remove[email  Protected] -hp-probook-4446s:~/code/sem$./sem_againvalue is  1  raise value and try again ..... value is  2< /span>wake up .... value is  0  raise value and try again ..... value is  1  

The o_create| was deliberately used in the Sem_again O_EXCL flag, if a semaphore with the same name already exists, the creation fails. But from the above results, the creation is successful and does not interfere with each other. That is, a well-known semaphore can be used as a nameless semaphore after opening and calling Sem_unlink (but the new process also wants to use the semaphore, which is not possible).

  Note: The above only made a simple test, did not test the sem_unlink after the competition of the process, in the production environment, please self-test.

PS: On Debian 6, you can call the sleep function without unistd.h, not on Ubuntu 14.04. On Debian 6 is the link to the library is-LRT, on Ubuntu 14.04 is not-PTHREAD,-LRT. Linux this is what to make.

About Sem_unlink when to delete semaphores

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.