Search code examples
csemaphore

System V Semaphores - semop() in different function


I created and initialized System V semaphores with semget() and semctl() in the main function, but the semop() function is called in a different function. I simply passed the ids of the semaphores as parameters to that function. It looks something like this:

int manageProcesses(int* data, int* numProcesses, int semId, int mutex, int time)
{
    semop(mutex, &semwait, 1);
    ...
}

Does this work or should semop() be called in the same function as semget() and semctl()?


Solution

  • Does this work or should semop() be called in the same function as semget() and semctl()?

    The id of a semaphore set, as obtained from semget(), is a process-scoped handle on an object (the semaphore set) with kernel persistence. Once you obtain one, you can use that anywhere in the program in conjunction with semctl() and / or semop(), until and unless you remove the semaphore set via semctl(..., IPC_RMID), or some other process removes it.