gpt4 book ai didi

从消息队列接收后无法使用shm_open

转载 作者:行者123 更新时间:2023-11-30 18:14:46 24 4
gpt4 key购买 nike

我尝试使用以下步骤在同一代码中实现多个 IPC 方法:

  • 1 - Process1 打开消息队列并读取 Process2 发送的消息

  • 2 - Process1 关闭并取消链接消息队列

  • 3 - Process1 向共享内存写入内容。

在第 3 步,任何 shm_open()memset()memcpy() 系统调用都会失败,Eclipse 将卡在一个新窗口中对于我调用的任何函数,都显示“无法在 *** 处找到源文件”。

当我禁用队列接收操作并仅执行共享内存时,一切正常。

所以我怀疑队列中发生了一些未完成的事务,这会阻止进程中的任何进一步调用

这是代码中的问题:(刚刚添加了相关部分)

...

static int receiveFromQ(char *msgQName)
{
int msgQFD;
char buffer[33];

/* Create and open the communication message queue */
msgQFD = mq_open(msgQName, O_RDONLY | O_CREAT, 0660, NULL);

/* Read the message from the queue and store it in the reception buffer */
memset(buffer, 0, sizeof(buffer));
mq_receive(msgQFD, buffer, sizeof(buffer), NULL); // This is a blocking point until a message is received

/* Use the data received ... */

/* Close the queue */
mq_close(msgQFD);

/* Remove the message queue */
mq_unlink(msgQName);

return 1;
}

int main(void)
{
char *key = SM_KEY;
int shmFD;

/* Receive the data from the queue */
int ret = receiveFromQ(MSGQ_NAME);

/* Creates a shared memory object in a kernel space, with size = 0 */
shmFD = shm_open(key, O_CREAT | O_RDWR | O_TRUNC, 0660); //The software stops here!
...
}

软件没有创建共享内存,而是卡在 shm_open() 处,并声称找不到 shm_open 的源。

最佳答案

正如@Petesh提到的,问题出在/* use the data returned */部分,我从代码中省略了它,因为我认为它不相关!澄清一下:在这一部分中,我在使用 memcpy() 复制的数据大小方面犯了一个错误,这导致了缓冲区溢出。这没有显示任何错误,但以某种方式阻止了程序的进一步处理。谢谢大家的有用评论。

关于从消息队列接收后无法使用shm_open,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55687442/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com