0
votes

I've set(semget + init to 1) a semaphore in process A. Forked A, and got B.

Forked B and got C (code of processes B,C is at another .c file, so I am passing the semid as a global integer with extern int semid).

Into the process C code, I try to apply down(semid) and getting "invalid argument" error.
What I am doing in the code for down function is this:

struct sembuf sem_d;
sem_d.sem_num = 0;
sem_d.sem_op = -1;
sem_d.sem_flg = 0;
if ( semop(semid, &sem_d, 1) == -1 )
{
    perror("error with down function");
    return -1;
}

What am I doing wrong?

I've also have reassured that the semid from when the semaphore is initialized is the same before semop.
Also, in process A,B I am using wait(-1).

1

1 Answers

0
votes

I'm not sure if you are allowed to use semget() over forks - it's a different process space after all.

semget() is part of the old System V semaphores anyway.

I would recommend to switch over to the POSIX semaphores - sem_open(), sem_wait() and friends and use named semaphores. Then open the same semaphore names in each process.