简体   繁体   中英

semop() returns EINVAL instead of EIDRM?

I have the following code:

int rc = 0;

key_t key = ftok("test.sh", 100);
if (key == -1) {
  std::cout << "ftok failed" << std::endl;
}
int sem_id = semget(key, 1, IPC_CREAT | (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP |
                                   S_IROTH | S_IWOTH));
if (sem_id == -1) {
  std::cout << "semget failed" << std::endl;
}

//removing semaphore set
union semun sem_union;
rc = semctl(sem_id, 1, IPC_RMID, sem_union);  
if (rc == -1) {
  std::cout << "semctl failed" << std::endl;
}

struct sembuf command_buf[2];
// Wait for 0
command_buf[0].sem_num = 0;
command_buf[0].sem_op  = 0;
command_buf[0].sem_flg = SEM_UNDO;
// Increment by 1
command_buf[1].sem_num = 0;
command_buf[1].sem_op  = 1;
command_buf[1].sem_flg = SEM_UNDO;

rc = semop(sem_id, command_buf, 2);
if (rc == -1) {
  std::cout << "errno=" << errno << std::endl;
}

I got the output "errno=22" which is EINVAL. Shouldn't I be getting EIDRM instead, given it is the error code for when "the semaphore set was removed" according to the documentation and supported by the answer here ?

I got the output "errno=22" which is EINVAL. Shouldn't I be getting EIDRM instead, given it is the error code for when "the semaphore set was removed" according to the documentation and supported by the answer here?

You really ought to be using perror() or retrieving an error message via strerror() instead of attempting to interpret errno values directly.

But I don't find it at all strange that the error you get when attempting a semaphore operation on a previously removed semaphore is EINVAL . The docs you referenced say that this is used when

The semaphore set doesn't exist, or semid is less than zero, or nsops has a nonpositive value.

. Certainly the semaphore set does not exist after it has been removed, so that seems to fit. Do you expect that the system will remember indefinitely which semaphore IDs have ever been used?

Of course, that begs the question of when EIDRM would ever be indicated. My expectation is that that would happen if the semaphore was valid when semop() was called, but was removed while semop() was blocked trying to perform an operation on it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM