所以我有两个程序连接到一个消息队列,其中一个程序以结构的形式向另一个程序发送消息。然而,当我在收到结构后尝试访问它时,我会遇到分段错误。
我不知道在发送结构后需要做什么才能访问它。
这是我给发件人的代码:
#include <stdio.h>
#include <mqueue.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
typedef struct {
char path[2048];
char shm_name[50];
size_t shm_s;
char sem_send_name[50];
char sem_recv_name[50];
} cache_request;
static void showAttr(mqd_t fd)
{
struct mq_attr attr;
mq_getattr(fd, &attr);
printf("maxmsg = %ldn", attr.mq_maxmsg);
printf("msgsize = %ldn", attr.mq_msgsize);
printf("curmsgs = %ldn", attr.mq_curmsgs);
}
int main()
{
mqd_t fd;
int ret;
struct mq_attr attr;
int flags = O_RDWR | O_CREAT;
attr.mq_flags = 0;
attr.mq_maxmsg = 3;
attr.mq_msgsize = 2216;
attr.mq_curmsgs = 0;
fd = mq_open("/mq", flags,(S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH),&attr );
if (fd < 0) {
printf("open failed %dn", fd);
exit(EXIT_FAILURE);
}
printf("open okn");
sleep(1);
showAttr(fd);
cache_request* msg = (cache_request*)malloc(sizeof(cache_request));
strcpy(msg->path,"okn");
strcpy(msg->shm_name, "ex1n");
msg->shm_s = 250;
strcpy(msg->sem_send_name," ex2");
strcpy(msg->sem_recv_name, "ex3");
printf("hmm %sn", msg->shm_name);
printf("hmm %ldn", msg->shm_s);
//res = mq_receive(fd, (char*) &msg, sizeof(cache_request), NULL);
int res = mq_send(fd, (const char*) &msg, sizeof(cache_request), 0);
if (res < 0) {
printf (" Error %d (%s) on server mq_send.n",
errno, strerror (errno));
mq_close(fd);
mq_unlink("/mq");
exit (1);
}
sleep(10);
ret = mq_close(fd);
if (ret != 0) {
printf("open failedn");
exit(EXIT_FAILURE);
}
printf("close okn");
sleep(20);
mq_unlink("/mq");
return 0;
}
这是接收器:
#include <stdio.h>
#include <mqueue.h> // for message queue
#include <sys/stat.h>
#include <stdlib.h> // for EXIT_FAILURE
#include <string.h>
#include <errno.h>
#include <unistd.h>
/*
gcc [file] -lrt
*/
typedef struct {
char path[2048];
char shm_name[50];
size_t shm_s;
char sem_send_name[50];
char sem_recv_name[50];
} cache_request;
static void showAttr(mqd_t fd)
{
struct mq_attr attr;
mq_getattr(fd, &attr);
printf("maxmsg = %ldn", attr.mq_maxmsg);
printf("msgsize = %ldn", attr.mq_msgsize);
printf("curmsgs = %ldn", attr.mq_curmsgs);
}
int main()
{
mqd_t fd;
int ret;
mq_unlink("/mq");
struct mq_attr attr;
int flags = O_RDWR;
attr.mq_flags = 0;
attr.mq_maxmsg = 3; // ***
attr.mq_msgsize = 141;
attr.mq_curmsgs = 0;
while((fd = mq_open("/mq", O_RDWR)) == -1){
printf("Couldnt connect to message queue in cachen");
sleep(2);
}
if (fd < 0) {
printf("open failed %dn", fd);
exit(EXIT_FAILURE);
}
printf("open okn");
//sleep(5);
showAttr(fd);
char* rsp_msg = (char*)malloc(2216);
int res = mq_receive(fd, (char*) &rsp_msg, 2216, NULL);
printf("recieved: %dn", res);
printf("should be %ldn", sizeof(cache_request));
if (res < 0) {
printf (" Error %d (%s) on server mq_receive.n",
errno, strerror (errno));
mq_close(fd);
mq_unlink("/mq");
exit (1);
}
cache_request* msg = (cache_request*)rsp_msg;
printf("shm_s: %ldn", msg->shm_s); //THIS is where the seg fault happens
printf("shm_name: %sn", msg->shm_name);
ret = mq_close(fd);
if (ret != 0) {
printf("close failedn");
exit(EXIT_FAILURE);
}
printf("close okn");
return 0;
}
int res = mq_send(fd, (const char*) &msg, sizeof(cache_request), 0);
应为msg
,而不是&msg
。您希望发送msg
指向的数据,而不是指针本身。
int res = mq_receive(fd, (char*) &rsp_msg, 2216, NULL);
同样。
此外,最好使用sizeof(cache_request)
而不是硬编码2216
。