我想通过管道将子进程中的字符串发送到另一个子进程。有人可以解释一下为什么它与"printf("NOW IT WORKS\n")"
一起使用但不起作用然后会删除printf
吗?
int main()
{
char bufp1[100];
char bufp2[100];
char bufp2i;
int pipe1[2],pipe2[2];
int id_p1, id_p2;
if(pipe(pipe1)<0)return 1;
if(pipe(pipe2)<0)return 1;
switch(id_p1=fork())
{
case 0:
{
close(pipe1[0]);
printf("PP1 %d\n",getpid());
for(;;)
{
scanf("%s",&bufp1);
write(pipe1[1],bufp1,100);
}
break;
}
case -1:
printf("error 1\n");
exit(1);
}
switch(id_p2=fork())
{
case 0:
{
close(pipe1[1]);
printf("PP2 %d\n",getpid());
for(;;)
{
//printf("NOW IT WORKS\n");
read(pipe1[0],bufp2,100);
bufp2i=strlen(bufp2);
printf("%i",bufp2i);
}
break;
}
case -1:
printf("error 2\n");
exit(1);
break;
}
for(;;);
return 0;
}