1

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
user3266496
  • 23
  • 1
  • 3

1 Answers1

2

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.

jim mcnamara
  • 16,005
  • 2
  • 34
  • 51
  • Also, continually calling `semget` is almost certainly unnecessary. Once you have the semid, you have the semid. – Duck Feb 03 '14 at 16:02
  • Semid returns success, what else should I check? – user3266496 Feb 03 '14 at 16:10
  • @Duck I agree. But this is the only code I can see. If you make a call then check the return code. To the OP: review your code and make sure there are no error return codes. And you check return codes. That will eliminate a lot of possible causes. – jim mcnamara Feb 05 '14 at 05:02