簡體   English   中英

錯誤:參數無效; 發送msgsnd()消息時; 不匹配隊列ID

[英]error: Invalid argument; while sending msgsnd() message; not matching queue ID

我只是在linux上學習IPC,並提出了三個簡單的程序。 一個是創建(並在該功能中管理)消息隊列。 第二個應該只是將消息發送到第一個創建的隊列。 第三個程序是從隊列接收數據。

所有程序都繼承自相同的根目錄,並且每個源和二進制文件都分別插入到不同的目錄中。

所以我們只關注創建和發送部分,這也將幫助我修復第三個程序。

添加隊列main.c:

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <errno.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#define FAILED -1


int main(int argc, char *argv[]) {
  // message data
  key_t key;
  int msgqid;

  if ((key = ftok("../src/main.c", 'Z')) == FAILED) {
    perror("ftok");
    exit(1);
  }

  if ((msgqid = msgget(key, 0666 | IPC_CREAT)) == FAILED) { /* create an message queue with owner & group & others permission set to rw- */
    perror("msgget");
    exit(1);
  }

  printf("Message Queue %i with key %i, been created [press return to delete]", msgqid, key);
  getchar();

  if (msgctl(msgqid, IPC_RMID, NULL) == FAILED) {
    perror("msgctl");
    exit(1);
  }
  printf("I'm outta here! \n");

  return 0;
}

發送main.c:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/msg.h>
#include <stddef.h>
#include <string.h>

#include "../../lib/shared_msgbuf.h" /* mbuf, MSGSZ */

#define FAILED -1


int main(void) {
  char s[MSGSZ];

  printf("Enter a message: ");

  if (fgets(s, sizeof s, stdin) == NULL)
    perror("fgets");
  else 
    strcpy(mbuf.mtext, s);

  printf("Connecting to the queue... \n", s);

  // Setup
  key_t key;
  int msgqid;

  if ((key = ftok("../../adqueue/src/main.c", 'Z')) == FAILED) {
    perror("ftok");
    exit(1);
  }

  if (msgqid = msgget(key, 0666) == FAILED) {
    perror("msget");
    exit(1);
  }

  printf("\n*CONNECTION ESTABLISHED* \n");
  printf("queue id: %i \n", msgqid);
  printf("queue key: %d \n", key);
  printf("message: %s \n", s);

  printf("Sending the message... \n");
  if (msgsnd(msgqid, &mbuf, MSGSZ, 0) == FAILED) {
    perror("msgsnd");
    exit(0);
  }

  return 0;
}

所以問題是我在嘗試發送消息時收到了Invalid argument錯誤號。 查看數據,我無法理解為什么id不匹配,因為連接到隊列似乎工作...

范例數據:

./cadqueue 
Message Queue 327680 with key 1510081535, been created [press return to delete]

./send 
Enter a message: test
Connecting to the queue... 

*CONNECTION ESTABLISHED* 
queue id: 0 
queue key: 1510081535 
message: test

Sending the message... 
msgsnd: Invalid argument

您的錯誤來自此行:

if (msgqid = msgget(key, 0666) == FAILED) {

它應該是

if ((msgqid = msgget(key, 0666)) == FAILED) {

在第一種情況下,由於操作員優先級 ,比較( == )在分配( = )之前完成。

在第二種情況下,paranthesis告訴編譯器必須首先完成什么。

這些是msgsnd()所需的頭文件

   #include <sys/types.h>
   #include <sys/ipc.h>
   #include <sys/msg.h>

已發布的代碼(可移植性)缺少其中兩個頭文件包含語句

===========

這個說法:

if (msgqid = msgget(key, 0666) == FAILED) {

錯過了IPC_CREAT ,它應該是0666參數的一部分

===========

mbuf有兩個字段, mtype字段,必須為正,> 0和mtext字段。

已發布的代碼正在設置mtext字段,但未能設置mtype字段。

=============

這個說法:

printf("Connecting to the queue... \n", s);

有一個輸入參數的問題,但它們在'格式字符串'中沒有格式說明符

============

每次訪問同一消息隊列時, key必須相同。

所以對ftok()的調用必須都具有相同的參數,否則它們將通過調用msgget()生成不同/唯一的消息隊列

============

注意:消息隊列不會因為程序退出而“消失”。 因此,控制程序(或最后一條消息的最后一個用戶)應該調用msgctl()來刪除消息隊列

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM