LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-02-2005, 02:54 PM   #1
tbruer1138
LQ Newbie
 
Registered: Feb 2005
Posts: 5

Rep: Reputation: 0
Cool Named mutex in Linux


Does the pthread library support the idea of a named mutex? I want to use a named mutex to prevent multiple binary execution.
 
Old 03-02-2005, 02:59 PM   #2
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
You could use SysV semaphores. (see semget() and friends) The more traditional way is to just use a lock file you create with open()'s O_CREAT|O_EXCL flags. Of course with either one you need to be careful about cleaning it up in response to any signals you might get.

The lock file has the advantage that an average user has a better idea of how to get rid of it. Nobody knows what to do with ipcrm.

A lazier way would be to dig around in /proc or the output of ps. That way you dont have to worry about cleanup, but you do have to worry about false positives depending on how carefully you parse it.
 
Old 05-16-2016, 09:07 AM   #3
nickdu
Member
 
Registered: Aug 2015
Posts: 41

Rep: Reputation: Disabled
Not sure how sound this advice is since I'm a noob at linux. I just created my own "pseudo" named mutex in linux using a UNIX domain socket with an abstract name. The bind() will fail if the socket is already bound, thus there can be only one owner, and the OS will cleanup the abstract socket if the process dies. Unfortunately I do not know of a way to "wait" on this "pseudo" mutex becoming available.

Code:
    // Create an abstract socket to use as a mutex.                             

    int err;
    int mutex_sock = socket(AF_UNIX, SOCK_STREAM, 0);
    if (mutex_sock == -1)
        {
        err = errno;
        printf("main, failed creating mutex socket: %s\n",
                get_error_string(errno, error_string, sizeof(error_string)));
        log_event(LOG_LEVEL_ERROR, "main, failed creating mutex socket: "
                "%s", get_error_string(errno, error_string,
                sizeof(error_string)));
        errno = err;
        goto done;
        }

    // Bind to abstract socket.  We use this as a sort of named mutex.          

    struct sockaddr_un addr;
    memset(&addr, 0, sizeof(addr));
    addr.sun_family = AF_UNIX;
    strncpy(addr.sun_path + 1, socket_name, sizeof(addr.sun_path) - 2);
    result = bind(mutex_sock, (struct sockaddr*) &addr, sizeof(addr));
    if (result == -1)
        {
        err = errno;
        if (errno == EADDRINUSE)
            {
            printf("main, failed bind to mutex socket: %s.  "
                    "Another instance must be running with the same "
                    "socket file.\n", get_error_string(errno,
                    error_string, sizeof(error_string)));
            log_event(LOG_LEVEL_ERROR, "main, failed bind to mutex socket: "
                    "%s.  "
                    "Another instance must be running with the same "
                    "socket file.", get_error_string(errno,
                    error_string, sizeof(error_string)));
            }
        else
            {
            printf("main, failed bind to mutex socket: %s\n",
                    get_error_string(errno, error_string,
                        sizeof(error_string)));
            log_event(LOG_LEVEL_ERROR, "main, failed bind to mutex socket: %s",
                    get_error_string(errno, error_string,
                    sizeof(error_string)));
        errno = err;
        goto done;
        }

    // We should be able to safely delete the socket in case it was left        
    // around for some reason.                                                  

    unlink(socket_name);
As you can see, I haven't wrapped it in its own set of methods. An abstract name begins with a null byte. You can see from the code above that I create an abstract socket with the same name as my non-abstract socket, eg:

Code:
    strncpy(addr.sun_path + 1, socket_name, sizeof(addr.sun_path) - 2);
Thanks,
Nick

Last edited by nickdu; 05-16-2016 at 09:39 AM.
 
Old 05-16-2016, 11:58 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 2,894

Rep: Reputation: 821Reputation: 821Reputation: 821Reputation: 821Reputation: 821Reputation: 821Reputation: 821
pthreads is unrelated to process-process synchronization. Maybe flock-ing a file would be the simplest: if flock(2) fails, then another instance of the program is running.
 
Old 05-16-2016, 03:42 PM   #5
nickdu
Member
 
Registered: Aug 2015
Posts: 41

Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
pthreads is unrelated to process-process synchronization. Maybe flock-ing a file would be the simplest: if flock(2) fails, then another instance of the program is running.
While I'm a noob, it appears from this stackoverflow post: http://stackoverflow.com/questions/9...56347#37256347 if the mutex is contained in shared memory you can use it for cross process synchronization.

Thanks,
Nick
 
1 members found this post helpful.
Old 05-17-2016, 12:59 AM   #6
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,186

Rep: Reputation: 474Reputation: 474Reputation: 474Reputation: 474Reputation: 474
Named mutex is obscure, why not simply use a atomic bool.
OP says nothing about process synchronisation. if that's wanted, put the 'onlye once' stuff in a process, if its up, its already running, every daemon does it like that.
 
Old 05-17-2016, 09:25 AM   #7
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 6,842

Rep: Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038
Your strategy very much depends on just how contended you think the lock-attempt will be. How likely there is to be a "race," and how neck-and-neck that race might be.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Best browser for linux, what is it named? TruongAn General 3 11-24-2005 08:41 AM
cannot find named.conf and /var/named kaushikma Red Hat 1 02-07-2004 12:49 PM
Linux named domain redirects? Nic-MDKman General 2 02-04-2004 09:42 AM
Virtual Host type, named or IP via SSL? Named VH is not possible? piratebiter Linux - Security 3 08-20-2003 05:27 PM
mutex equivalent in Linux devershetty Linux - Newbie 1 07-24-2003 10:02 PM


All times are GMT -5. The time now is 06:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration