c - 父子之间的消息队列导致 msgsnd 中的参数无效

标签 c linux gcc fork message-queue

该进程创建了 n 个子进程(n 从标准输入读取),每个子进程必须每 2 秒向父进程发送一条消息,然后父进程将收到的每条消息发送给所有子进程。我正在使用 2 个消息队列:一个是所有子级向父级发送消息,另一个是父级发送消息,每个子级都读取。每个成功发送或接收的 child 都会打印其 pid 和从 msg 队列接收到的数据(数据只是一个随机数)。 但是程序显示错误“msgsnd:无效参数”。 我已经进行了一些调试以检查传递的任何参数是否为空或无效,但事实并非如此。不,我有点卡住了,不太确定如何继续。任何帮助将不胜感激。

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <time.h>

typedef struct msgbuf {
    long    mtype;
    int     num;
} message_buf;

long n;
void sigalrm(int signo){
        alarm(5);
        n=n+2;
        printf("%ld seconds elapsed\n",n);
        }
int main(int argc,char *argv[]){
    int id1,id2;
    int msgflg = IPC_CREAT | 0666;
    key_t key1,key2;
    key1 = ftok ("test2.c", 'A');
    key2 = ftok ("test2.c", 'B');
    message_buf sbuf,rbuf;
    size_t buf_length=sizeof(int);

    time_t t;


        pid_t pid;
        int i,j,k,n;
        n=atoi(argv[1]);

        if ((id1 = msgget(key1, msgflg )) < 0) {
                perror("msgget");
            }
        else 
            printf("Queue 1 created\n");  

        if ((id2 = msgget(key2, msgflg )) < 0) {
                perror("msgget");
            }
        else 
             printf("Queue 2 created\n");        

        for(i=0;i<n;i++)    {
        pid = fork();
        if(pid==0)  {
        setpgid(getpid(),getppid());
        signal(SIGALRM, sigalrm);
        alarm(2);
        while(1)        {
                srand((unsigned)time(&t));
                sbuf.mtype = i;
                sbuf.num=rand()%50;

                if  (msgsnd(id1, &sbuf, buf_length,0) < 0) {
                        perror("msgsnd");
                }
                else    {
                printf("msg sending successful\n");
                printf("%ld\t%d\n",(long)getpid(),sbuf.num);
            }
        pause();
 // paused so that now parent can send the messages before children receive it

            for(j=0;j<n;j++)    {

            if (msgrcv(id2, &rbuf, buf_length, j, 0) < 0) {
                perror("msgrcv");
                }
            else    {
                printf("msg receiving successful\n");
                printf("%ld\t%d\n",(long)getpid(),sbuf.num);
            } 
        }

    }

}

else if(pid>0)  {


while(1)    {
sleep(3);
//sleeping so that children can first send the message
            if (msgrcv(id1, &rbuf, buf_length,0, IPC_NOWAIT) < 0) {
                perror("msgrcv");
                }
            else    {
                printf("msg receiving successful\n");

            sbuf.num=rbuf.num;
            sbuf.mtype=rbuf.mtype;
for(k=0;k<n;k++)    {
            if (msgsnd(id2, &sbuf, buf_length, IPC_NOWAIT) < 0) {
                perror("msgsnd parent\n");
                //exit(1);

            }
        }
    }
    }

}
}
}

输出:

Queue 1 created
Queue 2 created
msgsnd: Invalid argument
2 seconds elapsed
msgrcv: No message of desired type
msgrcv: No message of desired type
4 seconds elapsed
msgrcv: Interrupted system call
msgrcv: No message of desired type
6 seconds elapsed
msgrcv: Interrupted system call
msgrcv: No message of desired type
msgrcv: No message of desired type
8 seconds elapsed
msgrcv: Interrupted system call

(used ctrl+c here).

最佳答案

根据 msgsnd man page :

... The mtype field must have a strictly positive integer value. ...

...

EINVAL

Invalid msqid value, or nonpositive mtype value, or invalid msgsz value (less than 0 or greater than the system value MSGMAX).

您不能发送 mtype 设置为零的消息。

关于c - 父子之间的消息队列导致 msgsnd 中的参数无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32679425/

相关文章:

c - 在 c 中扫描空白/整行

c - 如何在c中的函数中释放分配的数组

c - C 错误中的 SIGSEGV

linux - 为什么系统调用号在 amd64 linux 中不同?

linux - 使用通配符限制 nginx 上的用户

c - 了解 gcc 4.9.2 自动矢量化输出

c - 是否可以在不违反严格别名的情况下使用字符数组作为内存池?

linux - 使用可变参数自动执行

Linux/海湾合作委员会 : ldd functionality from inside a C/C++ program

c++ - 添加c++ 11编译器后打开devc++时显示警告