首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C Linux (Ubuntu) - msgsnd()和msgrcv() errno 22 (EINVAL)

C Linux (Ubuntu) - msgsnd()和msgrcv() errno 22 (EINVAL)
EN

Stack Overflow用户
提问于 2015-11-25 19:41:28
回答 1查看 4.4K关注 0票数 1

我正在尝试IPC (消息传递)一段时间,但我无法解决这个问题。msgget()的errno 22 (EINVAL)似乎是无效的消息队列标识符、非正消息类型或无效消息大小。现在,对msgrcv()的errno 22是msgqid无效的,或者msgsz小于0,因此这似乎表明我的队列ID是错误的,因为无效的msqueue id重叠在两个错误解释中。

我应该注意我的msgget()调用返回0作为队列ID,这个‘应该’很好,因为文档说任何非负的返回值都是有效的。有什么想法吗?

在将计数器调整为一个计数器后,运行输出(不想粘贴100条错误消息):

msqID = 0,sendMessage.msgText = Hola!:0,sendMessage.msgType = 1,MSG_SIZE = 256 roh,在父发送中::无效参数子msgrcv():msqID = 0,rcvMessage.msgText =,rcvMesage.msgType = 1,MSG_SIZE = 256 roh,在子rcv::无效参数中

这在第31行(父进程中的第一个msgsnd() )和第60行(子进程中的第一个msgrcv() )上失败。

代码语言:javascript
复制
#include <errno.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
const int MAX_TEXT_SIZE = 255;
const int COUNTER = 99;
int main(){
  srand(time(NULL));
  int childPID, key = rand(), msgSendRslt, msgRecvRslt, msqID;
  long int type  = 1;
  struct msg {long msgType; char msgText[MAX_TEXT_SIZE];};
  const int MSG_SIZE = sizeof(struct msg)-sizeof(long int);
    if(msqID = msgget(key, IPC_CREAT | 0666) == -1){
      perror("\nRuh roh: ");
      exit(1);
    }
    if((childPID = fork()) > 0){
      struct msg sendMessage, rcvMessage;
      sendMessage.msgType = type;
      char *sendMsg = malloc(MAX_TEXT_SIZE);
      for(int i = 0; i < 100; i++){
        sprintf(sendMsg, "Hola!: %d", i);
        strcpy(sendMessage.msgText, sendMsg);
        printf("\nmsqID = %d, sendMessage.msgText = %s, sendMessage.msgType = %ld, MSG_SIZE = %d", msqID, sendMessage.msgText, sendMessage.msgType, MSG_SIZE);
        printf("\n");
        if(msgsnd(msqID, &sendMsg, MSG_SIZE, IPC_NOWAIT) != -1){
          printf("\nFrom parent, sent msg %d", i);
          if(msgrcv(msqID, &rcvMessage, MSG_SIZE, rcvMessage.msgType, IPC_NOWAIT) != -1){;
            printf("\nIn the parent, received from child: %s", rcvMessage.msgText);
          }
          else
            perror("\nRuh roh, in parent rcv: ");
        }
        else
          perror("\nRuh roh, in parent send: ");
          printf("\nerrno: %d", errno);
      }
      strcpy(sendMessage.msgText, "Over and out!");
      if(msgsnd(msqID, &sendMsg, MSG_SIZE, 0) == -1)
        perror("\nRuh roh, in parent send at end: ");
    }
  else if (childPID == 0){ //child
    int count = 0;
    int test = ceil(3.8);
    char *countS = malloc(ceil(log(COUNTER)/log(10)));
    struct msg rcvMessage, sendMessage;
    strcpy(rcvMessage.msgText, "");
    sendMessage.msgType = type;
    rcvMessage.msgType = type;
    while(strcmp(rcvMessage.msgText, "Over and out!") != 0){
      sprintf(countS, "%d", count);
      strcat(strcpy(sendMessage.msgText, "10-4 good buddy!: "), countS);
      printf("\nchild msgrcv(): msqID = %d, rcvMessage.msgText = %s, rcvMesage.msgType = %ld, MSG_SIZE = %d", msqID, rcvMessage.msgText, rcvMessage.msgType, MSG_SIZE);
      printf("\n");
      if(msgrcv(msqID, &rcvMessage, MSG_SIZE, rcvMessage.msgType, IPC_NOWAIT) != -1){
        printf("\nIn the child, received from the parent: %s", rcvMessage.msgText);
        if(msgsnd(msqID, &sendMessage, MSG_SIZE, IPC_NOWAIT) == -1)
          perror("\nRuh roh, in child send: ");
          exit(1);
      }
      else
        perror("Ruh roh, in child rcv: ");
        printf("\nChild error: %d", errno);
        exit(1);
    }
  }
  else{
    perror("\nRuh roh, fork() problem: ");
    printf("\n");
    exit(1);
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-26 05:17:31

我盯着你的代码看了很长一段时间才能发现问题。

代码语言:javascript
复制
if(msqID = msgget(key, IPC_CREAT | 0666) == -1)

括号在错误的地方!该代码有效地将msgget(key, IPC_CREAT | 0666) == -1分配给msqID。也就是说,msqID是条件的结果,并解释了为什么msqID总是0

正确的代码应该如下所示。注意括号的位置。

代码语言:javascript
复制
if((msqID = msgget(key, IPC_CREAT | 0666)) == -1)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33925120

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档