c - 使用消息队列无法发送和接收消息
问题描述
我正在尝试从孩子向父母发送消息(它有 2 个孩子)。但是当我在 Xcode 中运行它时它返回错误“无效参数”,当我在 vim 中运行它时它只是挂起并且什么都不做。我想从第二个孩子向父母发送消息。
- 我已经检查了这些,它们工作正常:
- 父子id匹配
msgrcv
和的论点msgsnd
- 分叉和消息队列创建
(我添加了适当的标题)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
void signalHandler(int signumber,siginfo_t* info,void* nonused){
printf("Signal with number %i has arrived\n",signumber);
switch (info->si_code){
case SI_USER:
printf("Process (PID number %i) sent the signal \n",info->si_pid); break;
case SI_QUEUE:
printf("It was sent by a sigqueue, sending process (PID %i)\n",info->si_pid);
default: printf("");
}
}
struct messg {
long mtype;
char mtext[1024];
};
int sendMsg (int mqueue){
const struct messg m = {5,"Test message"};
int status;
status = msgsnd(mqueue, &mqueue, strlen(m.mtext) + 1, IPC_NOWAIT);
if (status < 0){
perror("Message send error!");
}
return 0;
}
int receiveMsg(int mqueue){
struct messg m;
int status;
status = msgrcv(mqueue, &m, 1024 - sizeof(long), 5, 0);
if (status < 0){
printf("Errno : %d\n",status);
perror("Message recieve error!");
} else {
printf("Received msg : %s",m.mtext);
}
return 0;
}
int main(int argc, const char * argv[]) {
struct sigaction sigact;
sigact.sa_sigaction=signalHandler;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags=SA_SIGINFO;
sigaction(SIGTERM,&sigact,NULL);
key_t key = ftok(argv[0],1);
int messg = msgget(key, 0600 | IPC_CREAT);
pid_t parent_co;
int pid_co_parent[2];
if (messg < 0){
perror("Message creation error!");
return 1;
}
pid_t child_nhp;
int pid_child_nhp[2];
char sz[250];
if(pipe(pid_co_parent) == -1){
perror("Pipe");
exit(0);
}
if(pipe(pid_co_parent) == -1){
perror("Pipe");
exit(0);
}
parent_co = fork();
if (parent_co == 0) {
sleep(3);
printf("(Police Lieutenant-colon) Child process : child_id : %d, parent_id : %d\n",getpid(),getppid());
kill(getppid(), SIGTERM);
read(pid_co_parent[0], sz, sizeof(sz));
printf("(Communication Officer) : %s\n" ,sz);
char answer[250];
strcpy(answer, "Yes, it is compulsory to wear the mask when you leave your flat!");
write(pid_co_parent[1], answer, strlen(answer) + 1);
close(pid_co_parent[0]);
return 0;
} else {
printf("(Communication Officer) Parent process_id : %d\n",getpid());
pause();
char ques[250];
strcpy(ques, "Is it compulsory to wear a mask in the shops?");
write(pid_co_parent[1], ques, strlen(ques) + 1);
close(pid_co_parent[1]);
read(pid_co_parent[0], sz, sizeof(sz));
printf("(Police Lieutenant-colon) : %s\n",sz);
close(pid_co_parent[0]);
child_nhp = fork();
if (child_nhp < 0){
perror("fork error for child nhp!");
exit(0);
}
if (child_nhp == 0) {
printf("(National Head Physician) child_id : %d, parent_id : %d\n",getpid(),getppid());
receiveMsg(messg);
return 0;
} else {
sendMsg(messg);
wait(NULL);
// parent process
}
fflush(NULL);
wait(NULL);
}
return 0;
}
解决方案
推荐阅读
- opencv - 如何从给定图像验证相机失真值(和内在函数)
- javascript - 如何更改反应钩子中的对象值?
- flutter - 展开的小部件不占用整个宽度或高度
- wso2 - 无法检查 WSO2 Dev Portal-3.1.0 上启用双向 ssl 的 API 输出
- php - 如何在 PHP 中从包含数字和文本的字符串中过滤出文本
- amazon-web-services - 如何检查我的 EC2 是否可以访问同一账户中的 s3 存储桶?
- tensorflow - numpy中等效的keras顺序方法
- typescript - 打字稿泛型类型比较不起作用
- flutter - 错误:在初始化程序中只能访问静态成员
- mongodb - 找不到版本“libssl.so.10”(mongo 需要)