Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么msgrcv()会将msqid设置为0?_C_Message Queue - Fatal编程技术网

为什么msgrcv()会将msqid设置为0?

为什么msgrcv()会将msqid设置为0?,c,message-queue,C,Message Queue,我有一个c语言的程序,它应该通过msgq发送和接收ipc消息 我遇到的问题是,当我运行msgrcv()时,它将我的全局int msqid设置为0。当然,我在其他方法中也需要它,比如在信号处理器中 下面是一些代码: /* all the includes and some variables*/ #include "msg.h" // include the one I made int msgQ; // global int int main(int argc, char

我有一个c语言的程序,它应该通过msgq发送和接收ipc消息

我遇到的问题是,当我运行
msgrcv()
时,它将我的全局
int msqid
设置为0。当然,我在其他方法中也需要它,比如在信号处理器中

下面是一些代码:

/* all the includes and some variables*/
#include "msg.h" // include the one I made
int msgQ; // global int

int main(int argc, char *argv[])
{
    key = ftok("progfile", 65);
    msgQ = msgget(key, 0666 | IPC_CREAT);
    printf("msg queue id: %d \n", msgQ);

    start_tik_tok(); // setting up the timer and the signal handler
    /* irrelevant code */

    void read_msgs(msgQ);
}

void read_msgs(int msgQid)
{
    while (1)
    {
        printf("before the read local:%d goval:%d\n", msgQid, msgQ);
        int ret = msgrcv(msgQid, &message, sizeof(message), 1, 0);
        printf("after the read local:%d global :%d\n", msgQid, msgQ);
        if (ret == -1)
            /* error handling */

        switch (message.action_type)
        {
            /* mesage handling */
        }
}

void signal_handler(int signo)
{
    /*I need the global int here to send some messages */
}

void start_tik_tok()
{
    //timer interval for setitimer function
    struct itimerval timer;
    timer.it_interval.tv_sec = 1; //every 1 seconds
    timer.it_interval.tv_usec = 0;
    timer.it_value.tv_sec = 1; //start in 1 seconds
    timer.it_value.tv_usec = 0;

    //action for the signal
    struct sigaction new_sa;
    memset(&new_sa, 0, sizeof(new_sa));
    new_sa.sa_handler = &signal_handler;

    sigaction(SIGALRM, &new_sa, NULL);
    setitimer(ITIMER_REAL, &timer, NULL);
}
msg.h
文件:

#include <sys/msg.h>

struct msg_buff{
    long mesg_type; //reciver
    int sender; //sender
    char action_type;
    char time_tiks; //time in tiks
} message;
#包括
结构msg_buff{
长mesg_类型;//接收器
int sender;//sender
字符动作类型;
char time_tiks;//时间单位
}信息;
输出:

消息队列id:45416448

在读取本地:45416448全局:45416448之前

读取后本地:45416448全局:0


您可以看到,在我运行
msgrcv()
之后,
msgQ
的值变为0,即使我正在使用一个变量将值传递给方法
read\u msgs()

msgrcv
函数获取一个指向结构的指针,该结构以
long
类型的“header”开头,后跟消息数据。
msgrcv
msgsz
的第三个参数是消息数据体的大小,不包括作为标题的
long
。因此,您应该传递类似于
sizeof message-sizeof(long)
的消息。通过传递
sizeof message
,您要求它使缓冲区溢出
sizeof(long)
字节,这会破坏其他一些全局变量。

我找到了解决方案,我不确定为什么,但它解决了这个问题

我刚从一开始就初始化了int

更改:

int msgQ; // global int
用于:


不要发布带有假代码的问题,这些问题会遗漏与您的问题相关的任何内容。什么是
消息
?在哪里申报?在所有用注释省略的代码中,您在做什么?好的,让我添加它;)也许这个名字已经被图书馆使用了,你应该用别的名字,但这不太可能。你说你在信号处理器中也使用了
msgQ
?不是信号处理程序将其设置为0吗?请记住,一个信号将中断您的代码,如果它在
msgrcv()
调用中被中断,那么
msgQ
只能在一个
printf()
和另一个
,由信号处理程序修改的全局变量应该是易失的。如果这样做,我会得到错误7:参数列表太长
@JavierFuentes
int ret=msgrcv(msgQid,&message,sizeof(message)-sizeof(long),1,0)
yes,使用命令
ipcs-q
我可以看到每条消息都有16字节长,就像
sizeof(message)
retunr值一样。但是如果我做了
sizeof(message)-sizeof(long)
我得到了8,wich使'msgrcv'返回错误号7(参数列表太长)@JavierFuentes:您可能在发送端有相应的错误。
int msgQ = 0; // global int