N 个子项向父级发送消息
N children send message to parent
本文关键字:消息 更新时间:2023-10-16
我所做的是我创建了n个孩子,然后父级使用n个管道向他们发送消息"start"。每个孩子一根烟斗。现在我正在努力做的是把每个孩子的号码发回给父母。
这是我到目前为止的代码:
int main()
{
int n=5;
int p[n-1][2];
int i;
for(i=0;i<n;i++){
if(pipe(p[i])>0){
perror("pipe error");
exit(1);
}
}
for(i=0;i<n;i++){
pid_t pid=fork();
if(pid<0){
perror("fork error");
exit(1);
}
if(pid==0){
int j;
for(j=0;j<n;j++){
close(p[j][1]);
}
for(j=0;j<i;j++){
close(p[j][0]);
}
char msg[256];
int h;
read(p[i][0],&h,sizeof(int));
read(p[i][0],msg,h*sizeof(char));
cout<<i<<"_"<<msg<<endl;
close(p[i][0]);//here I would like to send the number i to the parent
for(j=i+1;j<n;j++){
close(p[j][0]);
}
exit(0);
}
}
char ms[256];
strcpy(ms,"start");
int ho=strlen(ms);
for(i=0;i<n;i++){
if(write(p[i][1],&ho,sizeof(int))==-1){
perror("write error");
exit(1);
}
if(write(p[i][1],ms,ho*sizeof(ms))==-1){
perror("write error");
exit(1);
}
close(p[i][1]);
}
for(int j=0;j<n;j++)
close(p[j][0]);//then read the number of each child and print it
while(wait(NULL)>0){};
exit(0);
}
这是输出:
0_start
2_start
1_start
4_start
3_start
所以我成功地向每个孩子发送了信息开始。但是我不知道父母将如何收到孩子发送的号码。
您可以执行类似的过程 但是在这里,父级具有管道的读取端,子项具有写入端。扩展了上面的示例,使其仅包含一个管道。您可以为每个孩子使用多个管道。
int main()
{
int n=5;
int p[n-1][2];
int pw[2]; // pipe child writes into
int i;
for(i=0;i<n;i++){
if(pipe(p[i])>0){
perror("pipe error");
exit(1);
}
}
if(pipe(pw)>0){
perror("pipe error");
exit(1);
}
for(i=0;i<n;i++){
pid_t pid=fork();
if(pid<0){
perror("fork error");
exit(1);
}
if(pid==0){
int j;
for(j=0;j<n;j++){
close(p[j][1]);
}
close(pw[0]);// close read end - child
for(j=0;j<n;j++){
if ( i!= j ) close(p[j][0]);
}
char msg[256];
int h;
read(p[i][0],&h,sizeof(int));
read(p[i][0],msg,h*sizeof(char));
cout<<i<<"_"<<msg<<endl;
close(p[i][0]);//here I would like to send the number i to the parent
write(pw[1],&i,sizeof(int)); // send i
close(pw[1]);
exit(0);
}
}
char ms[256];
strcpy(ms,"start");
int ho=strlen(ms);
int value;
for(i=0;i<n;i++){
if(write(p[i][1],&ho,sizeof(int))==-1){
perror("write error");
exit(1);
}
if(write(p[i][1],ms,ho*sizeof(ms))==-1){
perror("write error");
exit(1);
}
close(p[i][1]);
close(pw[1]); //close write end
if(read(pw[0],&value,sizeof(int))==-1){ // read from child process
perror("write error");
exit(1);
}
cout << " in main "<<value<<endl; // display number
}
for(int j=0;j<n;j++)
close(p[j][0]);//then read the number of each child and print it
while( wait(NULL) > 0 ){;}
exit(0);
}
相关文章:
- boost::进程间消息队列引发错误
- 在线编译器中的分段C++没有打印消息
- C++错误消息*成员参考.**初学者*
- 在createdialog创建的窗口中捕获用于编辑控件的OnMouseMove消息
- 要与"if constexpr"一起使用的编译时消息(在预处理器之后)
- 如何通过参数抛出错误消息
- 从服务器传输到客户端的消息不会出现
- ROS2 动态消息模板
- C++秘密消息学校作业
- glad 导致 glfwSwapBuffers 返回错误消息
- C++入门 5 版:类消息和文件夹
- FindPackageHandleStandardArgs.cmake:137 的 CMake 错误(消息):找不到 Boost (缺少:正则表达式)(找到合适的版本"1.72.0",
- 如何处理从一个对象传递到另一个在C++中具有公共抽象类的对象的消息
- 如何接受 [ENTER] 键作为无效输入并发送错误消息
- 由于无效的 ValidateRgn() 子窗口不会收到WM_PAINT消息
- "string.h"在构建适用于iOS的qt应用程序中找不到消息
- 如何将 Firebase 与基于 Linux 的客户端应用配合使用,以便与服务器进行双向消息通信
- 重新定义预定义的 errno 错误消息 (E2BIG)
- Libmosquitto publish 不会将所有消息传递到 Azure IoT Hub
- 在 capnp FlatArrayMessageReader 的对齐内存缓冲区中接收 zmq 消息