c - 如何通过管道将多个字符串从父进程发送到子进程?
问题描述
我正在尝试通过创建具有 3 个子进程的父进程来编写 C 代码,其中父进程通过管道发送五行文件,所有 3 个子进程在屏幕上打印接收到的字符串。
我知道关于这个主题有几个问题,但我无法解决我的问题,在那里寻找解决方案。
我的问题是只有第一个孩子接收字符串,打印它们然后程序停止。
这是代码:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#define MAX_CHILDREN 3
int main( void )
{
pid_t pid;
int fd[2];
FILE *f;
int num_process;
for(num_process = 0; num_process < MAX_CHILDREN; num_process++)
{
if(pipe(fd) == -1)
{
perror( "pipe Failed" );
continue;
}
pid = fork();
if(pid < 0)
{
perror("fork failed");
exit(1);
}
if(pid == 0)
{ //child code
char buff[256];
printf("Child %i (pid= %i)\n", num_process, getpid());
close(fd[1]);
while(read( fd[0], buff, sizeof(buff))>0)
{
printf("Read child = %s\n", buff);
}
exit(0);
}
else{//parent
printf("Im parent %i\n",getpid());
close(fd[0]);
int i;
int str_len=256;
char str[str_len];
f=fopen("input.dat","r");
for(i=0;i<5;i++)
{
fgets(str,str_len,f);
write(fd[1], str,strlen(str));
printf("Parent send %s\n", str);
}
wait(NULL);
}
}
fclose(f);
return 0;
}
我得到的输出如下所示:
Im parent 65090
Parent send apple
Parent send banana
Parent send cherry
Parent send cat
Parent send dog
Child 0 (pid= 65091)
Read child = apple
banana
cherry
cat
dog
为什么程序在第一个孩子之后停止?
解决方案
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#define MAX_CHILDREN 3
int main(void)
{
pid_t pid;
int fd[2];
FILE *f;
int num_process;
for (num_process = 0; num_process < MAX_CHILDREN; num_process++)
{
if (pipe(fd) == -1)
{
perror("pipe Failed");
return 0;
}
pid = fork();
if (pid < 0)
{
perror("fork failed");
exit(1);
}
if (pid == 0)
{ //child code
sleep(1); // for a nice output
char buff[256];
printf("Child %i (pid= %i)\n", num_process, getpid());
close(fd[1]);
while (read(fd[0], buff, sizeof(buff)) > 0)
{
printf("Read child = %s\n", buff);
}
close(fd[0]); // neccessary
exit(0);
}
else{//parent
printf("Im parent %i\n", getpid());
close(fd[0]);
int i;
int str_len = 256;
char str[str_len];
f = fopen("input.dat", "r");
for (i = 0; i < 5; i++)
{
fgets(str, str_len, f);
close(fd[0]); // neccessary
write(fd[1], str, strlen(str));
printf("Parent send %s\n", str);
}
close(fd[1]); // neccessary
wait(NULL);
}
}
fclose(f);
return 0;
}
You forgot to close the pipe array, where I wrote the comments: neccessary. That's why your program looks like stopped. I hope this help you a bit.
Output:
Im parent 398270
Parent send apple
Parent send bananana
Parent send cherry
Parent send cat
Parent send dog
Child 0 (pid= 398271)
Read child = apple
bananana
cherry
cat
dog
Im parent 398270
Parent send apple
Parent send bananana
Parent send cherry
Parent send cat
Parent send dog
Child 1 (pid= 398482)
Read child = apple
bananana
cherry
cat
dog
Im parent 398270
Parent send apple
Parent send bananana
Parent send cherry
Parent send cat
Parent send dog
Child 2 (pid= 398484)
Read child = apple
bananana
cherry
cat
dog
推荐阅读
- python - 有没有办法通过 python 使用 git username-password 克隆(或下载)私有远程存储库?
- javascript - 如何使用 AJAX 调用收到的 VueJS 中的变量呈现 HTML 内容?
- mysql - 浮点值为 0.0 的 MySqlParameter
- javascript - selenium driver.execute_script 中的 Python var
- typescript - 是否可以注释一个函数来告诉打字稿编译器已经检查了类属性的类型?
- c - C中int数组的初始值 - 为什么?
- powershell - Powershell中的Outlook电子邮件ReceivedTime乱序
- sql - 如何使用 SQL 将时间划分为每小时时段(可以使用视图、存储过程或函数)
- python-3.x - 如何使用 python/selenium 根据工作簿中的值动态更新 xpath
- linq - Linq 查询抛出 NullReferenceException