Problem Running Message Queue on two terminals

302 Views Asked by At

I am trying to run two processes which are basically sending and receiving messages using message queue(SYS V). While I can run two process on same terminal tab by keeping my receiver in background

./receiver &

and sender in the foreground.

 ./sender

which is working fine but causing all my prints from sender & receiver display on same tab.

If I try to run receiver on one terminal tab and sender on other terminal-tab, the processes are not working correctly, they fail to identify message queue exist on the system.

I am not sure if its the terminal issue, or my program issue, I am using MobaXterm terminal.

Added code below, Am I missing w.r.t running processes on two different terminals I would like to know.

receiver.c

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

#define PATH "/tmp/CMN_KEY"

struct message_text {
   int qid;
   char buf [200];
};

struct message {
  long message_type;
  struct message_text message_text;
};

int main (int argc, char **argv)
{
   key_t key;
   int qid;
   struct message message;

  if ((key = ftok (PATH,'Z')) == -1) {
    printf ("ftok");
    exit (1);
}

if ((qid = msgget (key, IPC_CREAT | 0660)) == -1) {
    printf ("msgget");
    exit (1);
}

printf ("Receiver: Waiting for MSG!\n");

while (1) {
    // read an incoming message
    if (msgrcv (qid, &message, sizeof (struct message_text), 0, 0) == -1) {
        printf ("msgrcv");
        exit (1);
    }

    printf ("Receiver: MSG Received.\n");
    // message from sender
    int length = strlen (message.message_text.buf);
    char buf [20];
    sprintf (buf, " %d", length);
    strcat (message.message_text.buf, buf);

    int client_qid = message.message_text.qid;
    message.message_text.qid = qid;

    // send reply message to Sender
  if (msgsnd (client_qid, &message, sizeof (struct message_text), 0) == -1) {  
        printf ("msgget");
        exit (1);
    }

    printf ("Receiver: Response sent to Sender .\n");
   }
 }

sender.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

#define PATH "/tmp/CMN_KEY"

struct message_text {
    int qid;
    char buf [200];
};

struct message {
    long message_type;
    struct message_text message_text;
};

int main (int argc, char **argv)
{
    key_t key;
    int sender_qid, myqid;
    struct message my_message, return_message;

    // queue for receiving messages from receiver
    if ((myqid = msgget (IPC_PRIVATE, 0660)) == -1) {
        printf ("msgget: myqid");
        exit (1);
    }
    printf("Sender created q with ID: %d\n" , myqid);   
    if ((key = ftok (PATH,'Z')) == -1) {
        printf ("ftok");
        exit (1);
    }

    if ((sender_qid = msgget (key, 0)) == -1) {
        printf ("msgget: sender_qid");
        exit (1);
    }

    my_message.message_type = 1;
    my_message.message_text.qid = myqid;

    printf ("Input a message: ");

    while (fgets (my_message.message_text.buf, 198, stdin)) {
        int length = strlen (my_message.message_text.buf);
        if (my_message.message_text.buf [length - 1] == '\n')
           my_message.message_text.buf [length - 1] = '\0';

        // send message to Receiver
        if (msgsnd (sender_qid, &my_message, sizeof (struct message_text), 0) == -1) {
            printf ("client: msgsnd");
            exit (1);
        }

        // read response from Receiver
        if (msgrcv (myqid, &return_message, sizeof (struct message_text), 0, 0) == -1) {
            printf ("client: msgrcv");
            exit (1);
        }

        //  Return message from Receiver
        printf ("Return Message From Receiver: %s\n\n", return_message.message_text.buf);  

        printf ("type a one more message: ");
    }
    // remove message queue
    if (msgctl (myqid, IPC_RMID, NULL) == -1) {
            printf ("client: msgctl");
            exit (1);
    }
    return 
}
0

There are 0 best solutions below