如何在C语言中实现消息传递IPC?
我正试图编写一个程序来实现父进程和子进程之间的基本消息传递。我以前从未使用过C,所以在过去的两天里我一直在跌跌撞撞地阅读教程,但我似乎无法让它工作。我最多能做的就是创建消息队列而不出错。 以下是我的代码,对我所做的事情有着最好的理解:如何在C语言中实现消息传递IPC?,c,ipc,C,Ipc,我正试图编写一个程序来实现父进程和子进程之间的基本消息传递。我以前从未使用过C,所以在过去的两天里我一直在跌跌撞撞地阅读教程,但我似乎无法让它工作。我最多能做的就是创建消息队列而不出错。 以下是我的代码,对我所做的事情有着最好的理解: #include <sys/types.h> #include <stdio.h> #include <unistd.h> #include <sys/wait.h> #include <sys/ipc.h&g
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/msg.h>
//this is the structure of the message i'm sending to the queue
struct message
{
long messagetype;
char text[10];
};
int main()
{
key_t key = 2222;
int msqid = msgget(key, IPC_CREAT); //create a message queue with the key
pid_t parentpid = getpid();
pid_t childpid = fork(); //these are mostly unused for now
if(childpid < 0) //fork failed
{
printf("fork failed\n");
return 1;
} else if(childpid == 0) //in child process
{
struct message sndmsg; //create a message to be send to the queue
printf("input message\n");
scanf("%s", sndmsg.text); //get the messages text fro input
printf("Sending message to queue: %s\n", sndmsg.text);
sndmsg.messagetype = 1; //set message type to 1
if(msgsnd(msqid, &sndmsg, sizeof(sndmsg.text), 0) < 0 ) // no idea what the last parameter really means here. check if message send fails
{
printf("error sending message\n");
} else
{
printf("sent message with text: %s\n",sndmsg.text);
}
printf("child process\n");
} else
{
wait(NULL); //wait until child process is done
struct message rcvmsg; //create a message to recieve the test from the queue
rcvmsg.messagetype = 1; //matching the message type here
if(msgrcv(msqid, &rcvmsg, sizeof(rcvmsg.text), 1, 0) < 0) //again, no idea what the last parameter does here. Checking to see if message recieve fails.
{
printf("error recieving message\n");
} else
{
printf("recieved message text : %s\n", rcvmsg.text);
}
printf("Parent process\n");
}
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
//这是我要发送到队列的消息的结构
结构消息
{
长消息类型;
字符文本[10];
};
int main()
{
键=2222;
int msqid=msgget(key,IPC_CREAT);//使用该键创建消息队列
pid_t parentpid=getpid();
pid_t childpid=fork();//目前大部分都未使用
if(childpid<0)//fork失败
{
printf(“fork失败\n”);
返回1;
}else if(childpid==0)//在子进程中
{
struct message sndmsg;//创建要发送到队列的消息
printf(“输入消息\n”);
scanf(“%s”,sndmsg.text);//从输入中获取消息文本
printf(“将消息发送到队列:%s\n”,sndmsg.text);
sndmsg.messagetype=1;//将消息类型设置为1
if(msgsnd(msqid,&sndmsg,sizeof(sndmsg.text),0)<0)//不知道最后一个参数在这里的真正含义。检查消息发送是否失败
{
printf(“发送消息时出错”);
}否则
{
printf(“发送了文本为:%s\n的消息”,sndmsg.text);
}
printf(“子进程”);
}否则
{
wait(NULL);//等待子进程完成
struct message rcvmsg;//创建从队列接收测试的消息
rcvmsg.messagetype=1;//匹配此处的消息类型
如果(msgrcv(msqid,&rcvmsg,sizeof(rcvmsg.text),1,0)<0)//再次,不知道最后一个参数在这里做什么。检查消息接收是否失败。
{
printf(“接收消息时出错\n”);
}否则
{
printf(“收到的消息文本:%s\n”,rcvmsg.text);
}
printf(“父进程”);
}
返回0;
}
当我运行时,消息发送和接收都会失败。我完全不知道IPC_CREATE是什么意思,IPC_NOWAIT,等等。通常来说,最后一个参数在msgsnd和msgrcv中的作用是什么。在示例中,我看到人们使用0660 | IPC|u CREAT,但没有解释0660是什么。有谁能解释一下我在代码中犯了什么错误,或者为通常使用C#和Java的人解释一下在C中传递的消息,因为现在这基本上是黑魔法。到目前为止,我找到的每一个资源或教程,一旦达到某一点,都会让我不知所措。谢谢。您没有用纯标准C实现,因为C11标准(read)不知道它们,而且任何IPC工具都是操作系统特定的(特别是Windows上的IPC工具与Linux上的IPC工具非常不同)
然而,一些操作系统提供进程间通信设施。然后您可以使用它们(无需实现它们),当然可以以特定于操作系统的方式使用它们
在Linux上,希望使用POSIX消息工具(而不是中介绍的旧SystemV IPC),请参阅。还要注意,,,(因为有许多方法可以进行进程间通信)
我完全不明白IPC_CREATE的意思
你需要先阅读,上面写着:
IPC\u创建
如果密钥不存在,则创建条目
然后你问:
有人能解释一下我在代码中犯了什么错误吗
您忘记了测试失败(另请参见&&)。您使用的每个函数都有文档记录(您应该阅读文档),并且可能会失败(您需要以某种方式处理失败案例)。先阅读代码,然后添加适当的检查,至少:
int msqid = msgget(key, IPC_CREAT);
if (msgid<0) { perror("msgget"); exit(EXIT_FAILURE); }
int-msqid=msgget(键,IPC_-CREAT);
if(msgid您不使用纯标准C实现,因为C11标准(read)不知道它们,而且任何IPC工具都是操作系统特定的(特别是Windows上的IPC工具与Linux上的IPC工具非常不同)
但是,一些操作系统提供进程间通信设施,然后您可以使用它们(无需实现它们),当然可以使用特定于操作系统的方式
在Linux上,更喜欢使用POSIX消息工具(而不是中介绍的旧SystemV IPC),请参阅。还要注意,,,(因为有许多方法可以进行进程间通信)
我完全不明白IPC_CREATE的意思
你需要先阅读,上面写着:
IPC\u创建
如果密钥不存在,则创建条目
然后你问:
有人能解释一下我在代码中犯了什么错误吗
您忘记了针对失败进行测试(另请参见&)。您使用的每个函数都有文档记录(您应该阅读文档),并且可能会失败(您需要以某种方式处理失败情况)。请先阅读,然后通过添加适当的检查来编写代码,至少:
int msqid = msgget(key, IPC_CREAT);
if (msgid<0) { perror("msgget"); exit(EXIT_FAILURE); }
int-msqid=msgget(键,IPC_-CREAT);
if(msgid)作为C新手,您确定要这样做而不是使用MPI吗?作为C新手,您确定要这样做而不是使用MPI吗?