Owndizzler
Owndizzler

Reputation: 11

Getting a "bad system call (core dumped)" when using a message queue in C

I'm trying to do a homework involving message queues and I'm getting "bad system call(core dumped)" when I try to do anything and I'm not sure why. Also when I use get_queue_ds() function it wouldn't compile for me but I assume that is because I'm using cygwin on windows. Any help would be appreciated or a link to similar code(searching for something like this didn't yield any results). Thank you.

#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);
}

Upvotes: 1

Views: 3651

Answers (1)

Hristo Iliev
Hristo Iliev

Reputation: 74485

Cygwin uses a special background service in order to implement the persistent XSI IPC. Check the documentation of Cygserver on how to configure and start it.

Upvotes: 1

Related Questions