首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用msgsnd和msgrcv的IPC

使用msgsnd和msgrcv的IPC
EN

Stack Overflow用户
提问于 2013-05-28 14:17:17
回答 1查看 3.2K关注 0票数 0

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

代码语言:javascript
复制
#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");
}

}

我的当事人:-

代码语言:javascript
复制
#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");
}

}
EN

回答 1

Stack Overflow用户

发布于 2018-06-04 10:28:28

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

代码语言:javascript
复制
// msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);
result = msgrcv(server_msgid, &cmbox, length, 1, 0);

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

代码语言:javascript
复制
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参数是指向以下一般形式的调用方定义结构的指针: 结构msgbuf { long mtype;/*消息类型,必须为>0 */ char mtext1;/*消息数据*/ }; ..。 msgrcv() ..。 参数msgtyp指定请求的消息类型如下:

  • 如果msgtyp为0,则读取队列中的第一条消息。
  • 如果msgtyp大于0,则读取msgtyp类型队列中的第一条消息,除非在msgflg中指定了MSG_EXCEPT,在这种情况下,类型不等于msgtyp的队列中的第一条消息将被读取。
  • 如果msgtyp小于0,则将读取最低类型小于或等于msgtyp绝对值的队列中的第一条消息。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16794410

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档