【问题标题】:IPC using msgsnd and msgrcv使用 msgsnd 和 msgrcv 的 IPC
【发布时间】:2013-05-28 14:17:17
【问题描述】:

为了学习,我写了2个IPC程序,一个服务器和一个使用消息发送的客户端。由于某种原因,服务器似乎没有接收到它已发送的数据。您能给我一些提示,告诉我如何调试这样的问题吗?

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

#define CENTRAL_MAILBOX 1200
#define CLIENT_MAILBOX 1201

struct {
long priority;
int value;
int pid;
} msgp, cmbox;

int main(int argc, char **argv) {

int uid = 0; // Process id of the server(this process) set to 0
int length = -1; // Length of the structure we are going to send over
int result = -1; // Result of the msgrcv operation
int status = -1;

if ( argc != 2 ) {
    fprintf(stderr, "Usage: ./server <temperature>\n");
    exit(-1);
}

printf("[+] Starting server\n");

printf("[+] Creating the mailbox\n");
int server_msgid = msgget(CENTRAL_MAILBOX, 0600 | IPC_CREAT);

printf("[+] Creating a mailbox for the client process\n");
int client_msgid = msgget(CLIENT_MAILBOX, 0600 | IPC_CREAT);

printf("[+] Initializing data to be sent across\n");
msgp.priority = 1;
msgp.value = 31337;
msgp.pid = uid;

length = ( sizeof(msgp) > sizeof(long) ? sizeof(msgp)-sizeof(long) : sizeof(long)-sizeof(msgp) );
printf("[+] Calculating the size of the message we are about to send across=%d\n", length);

// msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);
result = msgrcv(server_msgid, &cmbox, length, 1, 0);
printf("Result = %d\n", result);
printf("[+] Received message pid=%d, value=%d, priority=%ld\n", cmbox.pid, cmbox.value, cmbox.priority);

printf("[+] Sending message\n");
// int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
result = msgsnd(client_msgid, &msgp, length, 0);

printf("[+] Shutting down server\n");
status = msgctl(server_msgid, IPC_RMID, 0);

if ( status != 0 ) {
fprintf(stderr, "[*] ERROR: closing mailbox failed\n");
}

}

我的客户:-

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

#define CENTRAL_MAILBOX 1200
#define CLIENT_MAILBOX 1201

struct {
long priority;
int value;
int pid;
} msgp, cmbox;

int main(int argc, char **argv) {

int temp = atoi(argv[1]);
int uid = getpid(); //7171;
int length, result, status;

if ( argc != 2 ) {
// TODO find actual process id
fprintf(stderr, "Usage: ./client <temperature>\n");
exit(-1);
}

printf("[+] Creating server mailbox\n");
int server_msgid = msgget(CENTRAL_MAILBOX, 0600 | IPC_CREAT);

printf("[+] Creating client mailbox\n");
int client_msgid = msgget(CLIENT_MAILBOX, 0600 | IPC_CREAT);

printf("[+] Initializing the data to be sent across\n");
cmbox.priority = 2;
cmbox.value = 1337;
cmbox.pid = uid;

length = ( sizeof(msgp) > sizeof(long) ? sizeof(msgp)-sizeof(long) : sizeof(long)-sizeof(msgp) );
printf("[+] Calculating the size of the message we are about to send across=%d\n", length);

printf("[+] Sending message to server\n");
result = msgsnd(server_msgid, &cmbox, length, 0);
printf("Result = %d\n", result);

result = msgrcv(client_msgid, &msgp, length, 1, 0);
printf("[+] Received message pid=%d, value=%d, priority=%ld\n", msgp.pid, msgp.value, msgp.priority);

printf("[+] Removing the mailbox\n");
status = msgctl(client_msgid, IPC_RMID, 0);

if ( status != 0 ) {
printf("Error when closing mailbox\n");
}

}

【问题讨论】:

  • 检查 msg*() 函数调用的返回值。另外,您使用 Sys V 消息队列而不是 POSIX 消息队列是否有某些原因?
  • 没有特别的原因。接下来我将尝试 POSIX。 msgsnd 的返回值为 0
  • msgrcv的返回值呢?我询问了 POSIX 消息队列,因为与旧类型相比,您更有可能找到有关这些消息队列的有用资源。
  • msgrcv 不返回。呼叫被阻止,因为它似乎没有收到任何输入

标签: linux ipc message-passing


【解决方案1】:

您的服务器在第一个 msgrcv() 阻塞,因为它需要类型 1 的消息(您在 msgp, cmbox 结构中称为优先级):

// msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);
result = msgrcv(server_msgid, &cmbox, length, 1, 0);

当客户端发送类型 2 的消息时:

printf("[+] Initializing the data to be sent across\n");
cmbox.priority = 2;

// [...]

printf("[+] Sending message to server\n");
result = msgsnd(server_msgid, &cmbox, length, 0);

看看如何使用手册页中的msgtyp 值:

int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);

[...]

msgp 参数是指向调用者定义的结构的指针,其一般形式如下:

struct msgbuf {
    long mtype;       /* message type, must be > 0 */
    char mtext[1];    /* message data */
};

[...]

msgrcv()

[...]

参数msgtyp指定请求的消息类型如下:

  • 如果msgtyp 为0,则读取队列中的第一条消息。
  • 如果msgtyp大于0,则读取msgtyp类型队列中的第一条消息,除非MSG_EXCEPTmsgflg中指定,在这种情况下队列中的第一条消息类型不是等于msgtyp 将被读取。
  • 如果msgtyp小于0,则读取队列中最低类型小于或等于msgtyp绝对值的第一条消息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-24
    • 2018-09-09
    • 2018-11-11
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-02
    相关资源
    最近更新 更多