【问题标题】:Getting a "bad system call (core dumped)" when using a message queue in C在 C 中使用消息队列时出现“错误的系统调用(核心转储)”
【发布时间】:2014-12-10 05:57:06
【问题描述】:

我正在尝试做一个涉及消息队列的作业,当我尝试做任何事情时我得到“错误的系统调用(核心转储)”,但我不知道为什么。此外,当我使用 get_queue_ds() 函数时,它不会为我编译,但我认为这是因为我在 Windows 上使用 cygwin。任何帮助或类似代码的链接将不胜感激(搜索类似的东西没有产生任何结果)。谢谢。

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#define MAX_SEND_SIZE 1024//Make sure I have comments

typedef struct mymsgbuf
{
    long mtype;                 /* type of message */
    long sender_id;             /* sender identifier */
    char msg[MAX_SEND_SIZE];    /* content of message */
}MSGBUFFER;


/* Function prototypes */
void send_message(int qid, MSGBUFFER *qbuf, long type, char *text);
void read_message(int qid, MSGBUFFER *qbuf, long type);
void remove_queue(int qid);
void change_queue_mode(int qid, char *mode);
void usage(void);

int main(int argc, char *argv[])
{
    key_t myKey;
    int   msgqueue_id;
    int qid;
    MSGBUFFER* qbuf;

    if(argc == 1)
        usage();
    else            
        myKey = ftok("/cis370/lab5", 1);//Creates a unique key

    //Opens message queue
    qid = msgget(myKey, IPC_CREAT | 0660);
    if(qid == -1)
    {
        printf("Creating message queue failed");
        exit(1);
    }

    switch(tolower(argv[1][0]))
    {
        case 's': send_message(qid, qbuf, MAX_SEND_SIZE, argv[2]);
                  break;
        case 'r': read_message(qid, qbuf, MAX_SEND_SIZE);
                  break;
        case 'd': remove_queue(qid);
                  break;
        case 'm': change_queue_mode(qid, argv[2]);
                  break;
        default: usage();
    }
    return(0);
}


void send_message(int qid, MSGBUFFER *qbuf, long type, char *text)
{
    int length, result;
    length = sizeof (MSGBUFFER) - sizeof (long);
    result = msgsnd(qid, (MSGBUFFER *)qbuf, length, 0);
    if (result == -1)
    {
        printf("Send message failed\n");
    }

}


void read_message(int qid, MSGBUFFER *qbuf, long type)
{
    int result, length;
    length = sizeof (MSGBUFFER) - sizeof (long);
    result = msgrcv(qid, (MSGBUFFER*)qbuf, length, type, 0);
    if (result == -1)
    {
        printf("Messege read failed\n");
    }
}


void remove_queue(int qid)
{
    int result;
    result = msgctl(qid, IPC_STAT, 0);
    if (result == -1)
    {
        printf("Queue removal failed");
    }

}



void change_queue_mode(int qid, char *mode)
{
    /*struct msqid_ds tmpbuf;
    int result;
    //isn't working on cygwin
    //get_queue_ds(qid, &tmpbuf);

    sscanf(mode, "%ho", &tmpbuf.msg_perm.mode);

    result = msgctl(qid, IPC_SET, &tmpbuf);
    if (result == -1)
    {
        printf("Queue mode change failed");
    }*/

}


void usage(void)
{
    printf("USAGE:\tmsgqueue\t(s)end  <type>  <message>\n");
    printf("\t\t\t(r)ecv  <type> \n");
    printf("\t\t\t(d)elete\n");
    printf("\t\t\t(m)ode  <mode>\n");

    exit(0);
}

【问题讨论】:

  • 你需要学习如何use a debugger
  • @DourHighArch 我的班级大多只使用 gedit 然后尝试使用终端编译/运行。什么是 c 的好调试器?
  • gedit 是一个编辑器。如果您使用 gcc 进行编译,则可以使用 gdb 进行调试。

标签: c message-queue


【解决方案1】:

Cygwin 使用特殊的后台服务来实现持久 XSI IPC。查看Cygserver的文档,了解如何配置和启动它。

【讨论】:

  • 这很可能是它失败的原因还是只是 get_queue_ds()?如果这是导致它无法正常工作的原因,我可以使用我学校的计算机来确保它运行。
  • get_queue_ds()的源代码是here。它使用msgctl,如果 Cygserver 未运行,它将在 Cygwin 下失败。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多