我想不出我错过了什么。我第一次运行这个msgget() retuns 0但是msgctl()可以删除它。第二次仍有0msgctl()的操作因无效参数错误而中止。
已经尝试使用某个键而不是IPC_PRIVATE

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <sys/msg.h>
#include <time.h>

#define DEBUG

int main(){
    int queue_id;

    if(queue_id = msgget(IPC_PRIVATE, IPC_CREAT | IPC_EXCL | 0600) == -1){
         perror("queue");
         return -1;
    }

    if(msgctl(queue_id, IPC_RMID, NULL) == -1) {
         perror("queue rmid:");
    }

             return 0;
}

最佳答案

===结合更紧密。尝试在queue_id的赋值周围加上括号,或将其放在自己的行上:

queue_id = msgget(IPC_PRIVATE, IPC_CREAT | IPC_EXCL | 0600);
if(queue_id == -1) {
         perror("queue");
         return -1;
}

使用-Wall -Wextra -Werror运行编译器将有助于解决这类问题。

关于c - 创建队列消息的ID返回零,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55499452/

10-14 18:38
查看更多