semaphores

This is a discussion on semaphores within the C Programming forums, part of the General Programming Boards category; hi all, How can you share a semaphore variable amoung processes. In the sem_init(sem_t*,int,int) function , if you make the ...

  1. #1
    char main() RoshanX's Avatar
    Join Date
    Mar 2003
    Posts
    68

    semaphores

    hi all,

    How can you share a semaphore variable amoung processes.
    In the sem_init(sem_t*,int,int) function , if you make the second integer variable a non zero value, it says you can share the semaphore variable amoung processes.(this won't work...and I am using Sun solaris..). But do you have to allocate a shared memory area and pass this variable through this ?

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    You probably want to be using threads in order to share the address space. Check out using man pthread or using solaris threads.

  3. #3
    char main() RoshanX's Avatar
    Join Date
    Mar 2003
    Posts
    68
    The problem is, I want to do with a processes (not threads). In this case, how can you share a semaphore variable. Just copying the structure to a shared memory area won't work. Any suggestions ?

  4. #4
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    don't know if this helps but quoting the linux sem_init man page
    int sem_init(sem_t *sem, int pshared, unsigned int value);

    sem_init initializes the semaphore object pointed to by sem. The count
    associated with the semaphore is set initially to value. The pshared
    argument indicates whether the semaphore is local to the current pro-
    cess ( pshared is zero) or is to be shared between several processes (
    pshared is not zero). LinuxThreads currently does not support process-
    shared semaphores
    , thus sem_init always returns with error ENOSYS if
    pshared is not zero.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    The problem is, I want to do with a processes (not threads). In this case, how can you share a semaphore variable. Just copying the structure to a shared memory area won't work. Any suggestions ?
    This has an example of using semaphores and processes.
    http://www.talideon.com/notes/dcom3/shmem.pdf

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Semaphores, need advice on implementation.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 01-13-2009, 12:54 AM
  2. semaphores
    By Dr Spud in forum C Programming
    Replies: 7
    Last Post: 09-22-2007, 12:45 PM
  3. Semaphores Problems
    By mhelal in forum Linux Programming
    Replies: 2
    Last Post: 05-06-2007, 10:36 PM
  4. Semaphores
    By Jules in forum C Programming
    Replies: 4
    Last Post: 01-18-2004, 12:58 PM
  5. Semaphores
    By edk in forum C++ Programming
    Replies: 1
    Last Post: 11-25-2001, 02:55 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21