C 消息队列给我的参数无效

C 消息队列给我的参数无效,c,C,我的代码有点问题。它应该创建一个消息队列并发送一条消息,而不是等待另一个程序接收该消息并应答。问题是,当我运行它时,我在msgsnd和msgrcv上都得到了一个无效的参数 #include <stdlib.h> #include <unistd.h> #include <stdio.h> #include <string.h> #include <errno.h> #include <sys/types.h> #includ

我的代码有点问题。它应该创建一个消息队列并发送一条消息,而不是等待另一个程序接收该消息并应答。问题是,当我运行它时,我在msgsnd和msgrcv上都得到了一个无效的参数

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/msg.h>

typedef struct my_msg{
    long type;
    char text[100];
    char sqdr;
}message;

static void score(int messagge_id, char* A_B){
    message send;
    send.type=1;
    strcpy(send.text, "Try to score");
    send.sqdr = *A_B;
    if((msgsnd(messagge_id, &send, sizeof(send), 0))<0)perror("Error msgsnd\n");
    sleep(3);
    if((msgrcv(messagge_id, &send, sizeof(send), 4, 0))==-1)perror("Error msgrcv 1\n");
    int test=atoi(send.text);
    printf("%d\n", test);
}


int main(){
    int caso, key;
    char team= 'A';
    key=1234;
    int msg_id=msgget(key, S_IRUSR|S_IWUSR);
    printf("Try function score\n");
    score(msg_id, &team);
    printf("After score\n");
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
typedef struct my_msg{
长型;
字符文本[100];
char-sqdr;
}信息;
静态无效分数(int messagge_id,字符*A_B){
消息发送;
send.type=1;
strcpy(send.text,“尝试评分”);
send.sqdr=*A_B;
如果((msgsnd(messagge_id,&send,sizeof(send),0))这样使用:-

if((msgsnd(messagge_id, (void *)&send, sizeof(send), 0))<0)perror("Error msgsnd\n");

if((msgrcv(messagge_id, (void *)&send, sizeof(send), 4, 0))==-1)perror("Error msgrcv 1\n");

if((msgsnd(消息id,(void*)和send,sizeof(send),0))您需要确保消息队列已创建。您可以使用密钥
IPC_PRIVATE
或将
IPC_create
添加到标志中。您还需要尝试正确读取消息。您发送了“type 1”消息并尝试读取“type 4”消息,因此读取挂起

这段代码还删除了消息队列。如果它是一个私有队列(程序终止时会删除这些队列),这并不重要,但对于使用
IPC_create
和用户定义键的队列来说,这很重要。(我还更改了消息文本,以便
atoi()
返回了比零更有趣、更令人信服的内容。该代码还使用了单独的发送和接收缓冲区,因此我们知道该代码没有欺骗和重用缓冲区中已有的数据。)


当然,在“打瞌睡”和“不伐木”之间有3秒钟的停顿。

有什么显著的变化?对
void*
的转换是无关紧要的;它不会改变任何内容。输出是“Try function score.Error msgsnd:invalid argument.Error msgrcv:invalid argument.After score”。为什么不检查
msgget()
call?成功了吗?是否有另一个进程创建了消息队列?我将尝试检查msgget,但我认为问题不在于消息队列的创建,因为我还没有编写第二个程序。这可能是使用的密钥的问题吗?我想很可能是您没有使用密钥
IPC\u PRIVATE
或指定
IPC_create
作为标志(与
S|u IRUSR | S|u IWUSR
一起),因此您没有附加到预先存在的消息队列,也没有创建它。请参阅(以及在完成后删除消息队列)。请注意,一旦创建了队列,它将一直存在,直到被删除。
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/msg.h>
#include <fcntl.h>

typedef struct my_msg
{
    long type;
    char text[100];
    char sqdr;
} message;

static void score(int messagge_id)
{
    message send;
    message recv;
    send.type = 1;
    strcpy(send.text, "47 tries to score");
    send.sqdr = 'A';
    if ((msgsnd(messagge_id, &send, sizeof(send), 0)) < 0)
        perror("Error msgsnd");
    printf("Dozing...\n");
    sleep(3);
    printf("Unslumbering...\n");
    if ((msgrcv(messagge_id, &recv, sizeof(recv), -4, 0)) == -1)
        perror("Error msgrcv");
    int test = atoi(recv.text);
    printf("%d\n", test);
}

int main(void)
{
    int key = 1234;
    int flags = S_IRUSR|S_IWUSR|IPC_CREAT;
    // int key = IPC_PRIVATE;
    // int flags = S_IRUSR|S_IWUSR;
    int msg_id = msgget(key, flags);
    if (msg_id < 0)
        perror("Error msgget");
    else
    {
        printf("Try function score\n");
        score(msg_id);
        printf("After score\n");
        if (msgctl(msg_id, IPC_RMID, 0) < 0)
            perror("Error msgctl");
    }
    return 0;
}
Try function score
Dozing...
Unslumbering...
47
After score