Ask a Question related to UNIX Programming, Design and Development.

  1. #1

    Default Semaphore tutorial?

    Does anybody have a good tutorial for how SysV semaphores work,
    specifically the semop() function? I've read the description in
    Stevens' Unix Network Programming several times, but I'm still totally
    baffled.

    What does SEM_UNDO do? Stevens describes how it affects the kernel's
    semadj variable, but I don't understand what the purpose of that is.

    Why would you want to have a value for sem_op which is anything other
    than -1, 0, or +1? If you wanted to obtain a resource, what would be
    the purpose of setting it to -42 instead of -1?

    Why would you ever want to have more than one semaphore in a set?

    Lastly, I'm working on a Linux box which appears to have the SysV
    versions of message queues, semaphores, and shared memory, in addition
    to the posix semaphores. Given that the posix semaphores look a whole
    lot simplier to use than the SysV kind, I'm tempted to mix and match,
    i.e. use the SysV shared memory (because it's the only kind available)
    with posix semaphores (because they're simplier to use). Any reason not
    to do that?
    Roy Smith Guest

  2. Similar Questions and Discussions

    1. #39168 [NEW]: Semaphore acquire timeout
      From: s dot s at terra dot com dot br Operating system: Freebsd 6 PHP version: 5.1.6 PHP Bug Type: Feature/Change Request...
    2. ANNOUNCE: POSIX::RT::Semaphore 0.01
      POSIX::RT::Semaphore is a Perl interface to POSIX.1b (Realtime) semaphores, at least as supported by your system. ;-) RT semaphores are objects...
    3. SysV Semaphore Troubles
      On Sun, 03 Aug 2003 12:21:05 -0400, Marc Rochkind wrote: The troubling scenario is when the program increments or decrements the semaphore so...
    4. semaphore example needed
      Hi all, I tried to use IPC::Semaphore but tried for a long time without success. What I want to do is like this : I want to limit a perl...
    5. Need help with semaphore creation/syncronization strategy.
      I've been playing with System V IPC semaphores a bit, and I've come to a point where I'm stuck. I've got two processes (call them master and...
  3. #2

    Default Re: Semaphore tutorial?

    On Sun, 20 Jul 2003 21:19:52 -0400, Roy Smith <roy@panix.com> wrote:
    > Does anybody have a good tutorial for how SysV semaphores work,
    > specifically the semop() function? I've read the description in Stevens'
    > Unix Network Programming several times, but I'm still totally baffled.
    >
    I have one in progress... can you wait a few months?
    > What does SEM_UNDO do? Stevens describes how it affects the kernel's
    > semadj variable, but I don't understand what the purpose of that is.
    This is a rollback mechanism that allows what a process did to a semaphore
    to be undone if it exits abnormally.
    >
    > Why would you want to have a value for sem_op which is anything other
    > than -1, 0, or +1? If you wanted to obtain a resource, what would be the
    > purpose of setting it to -42 instead of -1?
    Suppose the semaphore indicates the number of thingies available. If you
    produce 17 of them, you would bump the number by 17.
    >
    > Why would you ever want to have more than one semaphore in a set?
    >
    It's possible for more than one semaphore to be needed (one for each
    resource to be controlled, say). The advantage of sets is that a single
    semop is an atomic operation.
    > Lastly, I'm working on a Linux box which appears to have the SysV
    > versions of message queues, semaphores, and shared memory, in addition to
    > the posix semaphores. Given that the posix semaphores look a whole lot
    > simplier to use than the SysV kind, I'm tempted to mix and match, i.e.
    > use the SysV shared memory (because it's the only kind available) with
    > posix semaphores (because they're simplier to use). Any reason not to do
    > that?
    >
    Mixing and matching is perfectly OK.

    However, check your Linux again. My version of Linux has POSIX semaphores
    only as un-named, non-shared semaphores. (That is, sem_open is missing, and
    sem_init doesn't support the pshared argument.) Perhaps your Linux is more
    complete.

    My recommendation is to wrap the very elaborate Sys V semaphores in simple
    functions that look more like the POSIX semaphores: post and wait, and to
    use them that way. Go for the fancier features (increments other than one,
    and more than one in a set) only if your application absolutely requires
    it.

    System V semaphores have one other problem that Stevens explains very well
    (p. 284. of UNIX Network Programming: Interprocess Communications): A new
    semaphore is not initialized, and there is a race condition between its
    creation and the follow-on call to initialize. Stevens explains a
    complicated (but essential) way to program around this.

    --Marc

    Marc Rochkind Guest

  4. #3

    Default Re: Semaphore tutorial?

    Roy Smith <roy@panix.com> writes:
    >What does SEM_UNDO do? Stevens describes how it affects the kernel's
    >semadj variable, but I don't understand what the purpose of that is.
    "semadj" limits how much of "SEM_UNDO" can be done; "SEM_UNDO" allows
    you to create a process which is "semaphore value neutral" when it
    is killed or exists unexpectedly.
    >Lastly, I'm working on a Linux box which appears to have the SysV
    >versions of message queues, semaphores, and shared memory, in addition
    >to the posix semaphores. Given that the posix semaphores look a whole
    >lot simplier to use than the SysV kind, I'm tempted to mix and match,
    >i.e. use the SysV shared memory (because it's the only kind available)
    >with posix semaphores (because they're simplier to use). Any reason not
    >to do that?
    None that I can think of.

    Casper
    --
    Expressed in this posting are my opinions. They are in no way related
    to opinions held by my employer, Sun Microsystems.
    Statements on Sun products included here are not gospel and may
    be fiction rather than truth.
    Casper H.S. Dik Guest

Posting Permissions

  • You may not post new threads
  • You may 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