简体   繁体   English

msgrcv:参数无效错误

[英]msgrcv: Invalid argument Error

I am getting an error that says: 我收到的错误是:

msgrcv: Invalid argument msgrcv:参数无效

what could be causing this error? 可能导致此错误的原因是什么? Here is my code 这是我的代码

Bassically I'm passing a message from a parent to a child then I want to pass a message from the child to the parent and even though I'm using basically the same code for both, it isn't working for the second receive. 我正在将一条消息从父母传递给一个孩子,然后我想将一条消息从孩子传递给父母,即使我对两者使用的代码基本相同,但它不能用于第二次接收。

    struct msg {
        long int    mtype;      /* message type */
        char        mtext[1024];   /* message text */
} msg;
int len, msgflg = 0, msqid, *pint;
pid_t pid;
size_t msgsz = 40;
long int msgtyp;
msqid = msgget(IPC_PRIVATE,S_IRWXU);
char* charpid[250];
msg.mtype = 1;
if (msqid < 0) {
        perror("msgget");
        exit(1);
}

switch(pid=fork()) //fork child process
{
case 0: //Child process 

    //receive message from parent
    if(msgrcv(msqid,&msg,sizeof msg.mtext, 1,IPC_NOWAIT)>=0){
        printf("Serving for client pid #%s",msg.mtext);         
        asprintf(&charpid[0], "%ld\n", pid);
        strncpy(msg.mtext,charpid[0], 1024);

        if(msgsnd(msqid,&msg,strlen(msg.mtext),msgflg)<0){
            perror("msgsnd");       
        }
    }
    else
        perror("msgrcv");

    msgctl(msqid, IPC_RMID, NULL);
    exit(0);

case -1:
    printf("fork failed");
    exit(2);
    break;
default:
    //convert pid to string.
    asprintf(&charpid[0], "%ld\n", pid);
    //set mtext
    strncpy(msg.mtext,charpid[0], 1024);
    //send message

    if(msgsnd(msqid,&msg,strlen(msg.mtext),msgflg)<0){
        //report error
        perror("msgsnd");
    }
    //wait for child process to die
    wait(NULL); 
    //receive message from child
    if(msgrcv(msqid,&msg,sizeof msg.mtext, msg.mtype,IPC_NOWAIT)>=0){
        //print pid
        printf("Received reply from pid #%s",msg.mtext);
    }
    else

        //report error
        perror("msgrcv");   
    exit(0);
}

The "Invalid argument" error is from your original process (not the forked child), and occurs when it tries to receive the reply from the child. “无效参数”错误来自原始进程(而不是分叉子进程),并且在尝试接收来自子进程的回复时发生。 The error occurs because the child has already removed the queue by then. 发生此错误是因为该子项已经删除了该队列。 Since your original process created the queue and waits for the child to exit anyway, it would make more sense to remove the queue there (after receiving the reply). 由于您的原始进程创建了队列并等待子进程退出,因此在那里删除队列(收到回复后)会更有意义。

Even if you fix that, you may still find that when the child does its msgrcv it might not get anything, since your original process may not have sent it yet (and you specify IPC_NOWAIT ). 即使您修复了这个问题,您仍然可能会发现当孩子执行msgrcv它可能无法获得任何内容,因为您的原始进程可能尚未发送它(并且您指定了IPC_NOWAIT )。 To get your code to work, with both receives, I had to move the msgctl call as noted above, and also add a sleep call before the msgrcv in the child. 为了让你的代码工作,两个接收,我必须如上所述移动msgctl调用,并在子msgrcv中的msgrcv之前添加一个sleep调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM