Professional Web Applications Themes

Problems with semaphores - Mac Programming

Hi UNIX experts, I'm trying to use semaphores to get named locks (like a Win32 MUTEX) between 2 concurrent threads. The behaviour is not at all what I was expecting. Here is a resumé of my code: // creation char name[] = "/test"; semt* sem = sem_open(name,O_CREATE,0666,1); // lock sem_wait(sem); // unlock sem_post(sem); // disposal sem_close(sem); sem_unlink(name); Everything works fine when there is NO concurrent access. However when there is, that is when the semaphore is locked in the first thread, the semaphore cannot be OPENED by the second thread. The errno is EACCES. I was expecting the second thread ...

  1. #1

    Default Problems with semaphores

    Hi UNIX experts,

    I'm trying to use semaphores to get named locks (like a Win32 MUTEX) between
    2 concurrent threads.

    The behaviour is not at all what I was expecting. Here is a resumé of my
    code:

    // creation
    char name[] = "/test";
    semt* sem = sem_open(name,O_CREATE,0666,1);

    // lock
    sem_wait(sem);

    // unlock
    sem_post(sem);

    // disposal
    sem_close(sem);
    sem_unlink(name);

    Everything works fine when there is NO concurrent access. However when there
    is, that is when the semaphore is locked in the first thread, the semaphore
    cannot be OPENED by the second thread. The errno is EACCES. I was expecting
    the second thread to block on sem_wait.

    Also, I noticed that if I don't call sem_unlink, I cannot create the same
    semaphore twice without rebooting. However, the SharedMemory sample from
    Apple doesn't make this call.

    There's obviously something I don't understand about semaphores in unix, so
    any help will be appreciated.

    Eric


    Eric Guest

  2. #2

    Default Re: Problems with semaphores

    > Also, I noticed that if I don't call sem_unlink, I cannot create the same 

    As a matter of fact, that sample is bugged.

    Launch it, type 1000 for size and toto for key. Click on attach. Quit.

    Start again. Type the sam values, Click on attach, now click on start
    editing. You can never get the lock, unless you reboot.

    So it seems calling sem_unlink is mandatory, but I cannot find useful
    examples on the web.

    Eric

    Eric Guest

  3. #3

    Default Re: Problems with semaphores

    In article <BBFAD401.16FFB%fr>,
    Eric VERGNAUD <fr> wrote:
     

    if the second attempt to call sem_open fails with EACCES it should mean
    you're using an inappropriate value for the mode parameter. Something in
    the back of my brain is saying you should use 0777.

    G

    G
    Gregory Guest

  4. #4

    Default Re: Problems with semaphores

    dans l'article attbi.com,
    Gregory Weston à com a écrit le 9/12/03 3:06:
     
    >
    > if the second attempt to call sem_open fails with EACCES it should mean
    > you're using an inappropriate value for the mode parameter. Something in
    > the back of my brain is saying you should use 0777.
    >
    > G
    >
    > G[/ref]

    4+2+1 eaning write+read+execute ?

    Why execute ?

    I'll try it.

    Eric

    Eric Guest

  5. #5

    Default Re: Problems with semaphores

    In article <BBFAD401.16FFB%fr>,
    Eric VERGNAUD <fr> wrote:
     

    Why not use pthread mutexes or other pthread constructs? You can't name
    them, but that shouldn't be much of a problem, and they're far better
    doented.

    -Eric

    --
    Eric Albert edu
    http://rescomp.stanford.edu/~ejalbert/
    Eric Guest

  6. #6

    Default Re: Problems with semaphores

    dans l'article stanford.edu, Eric Albert
    à edu a écrit le 9/12/03 7:56:
     
    >
    > Why not use pthread mutexes or other pthread constructs? You can't name
    > them, but that shouldn't be much of a problem, and they're far better
    > doented.
    >
    > -Eric[/ref]

    Because I need the name, since the pieces of code that will use them know
    nothing about each other.

    Eric

    Eric Guest

  7. #7

    Default Re: Problems with semaphores

    In article <BBFAD7E6.1700C%fr>,
    Eric VERGNAUD <fr> wrote:
     

    Having used those APIs myself before, I can say that they are a pain.
    If you can, use something else like pthreads, etc.
    Sean Guest

Similar Threads

  1. Replies: 1
    Last Post: December 15th, 09:08 PM
  2. Replies: 0
    Last Post: September 22nd, 02:18 PM
  3. Semaphores
    By L0t3k in forum PHP Development
    Replies: 0
    Last Post: August 8th, 05:45 AM
  4. Shared memory and semaphores - semop() error
    By Simon Fisher in forum UNIX Programming
    Replies: 0
    Last Post: June 23rd, 12:39 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139