0

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 
}
eesiraed
  • 4,626
  • 4
  • 16
  • 34
csavvy
  • 761
  • 4
  • 13
  • Only way anyone can help is if you provide [A Minimal, Complete, and Verifiable Example (MCVE)](http://stackoverflow.com/help/mcve). – David C. Rankin Apr 19 '20 at 01:47
  • @David C.Rankin added code – csavvy Apr 19 '20 at 01:52
  • C is not C++, please don't use irrelevant tags. – eesiraed Apr 19 '20 at 03:07
  • There is nothing that immediately jumps out looking over your code and reading through the relevant man pages, e.g. [man 2 msgop](http://man7.org/linux/man-pages/man2/msgop.2.html) You show starting receiver before sending so as long as you have write permission in `/tmp`, your `qid` should both be created and found. Debug and ensure there are no problems with your key and type. Having it fail to identify an existing message queue suggests a permission issue. The man page example works fine from different terminals. Dumb question, but you are running on Linux, not WSL right? – David C. Rankin Apr 19 '20 at 05:11
  • Yes, Running on Linux, and I also did touch /tmp/CMN_KEY and checked from both terminals before running processes, it shows the file – csavvy Apr 19 '20 at 05:22
  • I suspect may be something wrong with the environment of shell, but not sure what is that – csavvy Apr 19 '20 at 13:42

0 Answers0