I want to send strings from child process to another child process with pipes. Could someone explain me why it works with "printf("NOW IT WORKS\n")"
but doesn't work then printf
is deleted ?
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;
}