Search code examples
clinuxposix

Open semaphore without changing its value


Opening a semaphore with sem_open will also initialize it to any given value, is there any way to open a semaphore that is used by another thread without changing its value?


Solution

  • What you say isn't true. Using sem_open to open an existing semaphore doesn't change its value.

    So, to answer you question,

    sem_t *sem = sem_open( name, O_RDWR );
    if ( sem == SEM_FAILED ) {
       perror( NULL );
       exit( EXIT_FAILURE );
    }