0

我正在尝试运行两个进程,它们基本上是使用消息队列(SYS V)发送和接收消息。虽然我可以通过将接收器保持在后台来在同一个终端选项卡上运行两个进程

./receiver &

和前台的发件人。

 ./sender

这工作正常,但导致我来自发送者和接收者的所有打印都显示在同一个选项卡上。

如果我尝试在一个终端选项卡上运行接收器并在另一个终端选项卡上运行发送器,则进程无法正常工作,它们无法识别系统上存在的消息队列。

我不确定是终端问题还是我的程序问题,我使用的是 MobaXterm 终端。

在下面添加了代码,我是否错过了我想知道的两个不同终端上正在运行的进程。

接收器.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");
   }
 }

发件人.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 
}
4

0 回答 0