user3266496
user3266496

Reputation: 23

Semop: Invalid argument

I have this code that basically makes a P on the semaphore with number sem. The semaphore is in a pool. The problem is that sometimes I get Invalid argument and I can't figure out why.

bool sem_p(key_t key, int sem){
    int semid = semget(key, sem, 0666);
    struct sembuf sb = {sem, -1, 0};
    if(semop(semid, &sb, 1) == -1){
        perror("sem p"); 
        printf("sem %d\n", sem);
        return FALSE;
    }
    return TRUE;
}

When that function gets called, it prints

sem p: Invalid argument

Upvotes: 1

Views: 5059

Answers (1)

jim mcnamara
jim mcnamara

Reputation: 16399

Check if

int semid = semget(key, sem, 0666);

returns success or failure. As @VladLazaranenko mentioned there could be a lot of possible errors. Check every single return code for every single function you call (if they return a value) - for production systems.

Upvotes: 2

Related Questions