其他分享
首页 > 其他分享> > 父级和子级之间的消息队列导致msgsnd中的参数无效

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

作者:互联网

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

#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设置为零的消息.

标签:gcc,message-queue,c-3,linux,fork
来源: https://codeday.me/bug/20191027/1947881.html