linux 简单的父子进程无名管道通信 父进程传字符串给子进程 子进程做变换传给父进程 显示
问题:运行结果 位置4和位置5 为什么打印不出来
如果去掉 位置3中的\n 则位置1-5都不打印 这是为什么呀?
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
int main()
{
int pipe_fd[2], read_bytes, i = 0;
pid_t pid;
char string[100];
char buf[100];
printf("Please input string:");
scanf("%s",string);
if(pipe(pipe_fd) < 0)
{
printf("pipe create error!\n");
exit(1);
}
if((pid = fork()) > 0)
{
printf("fffffffffffffffff");
close(pipe_fd[1]);
if((read_bytes = read(pipe_fd[0],buf,sizeof(string))) > 0)
{
printf("this is parent progress,after change string is %s\n",buf);
}
close(pipe_fd[0]);
waitpid(pid,NULL,0);
}
else if(pid == 0)
{
printf("1111111111");/*位置1*/
printf("2222222222");/*位置2*/
sleep(2);
printf("3333333333\n");/*位置3*/
close(pipe_fd[0]);
while(string[i] != '\0')
{
string[i] = string[i] - 32;
i++;
}
printf("4444444444");/*位置4*/
if(write(pipe_fd[1],string,sizeof(string)) < 0)
{
printf("write error");
exit(1);
}
printf("5555555555");/*位置5*/
close(pipe_fd[1]);
_exit(0);
}
else
{
printf("fork error!\n");
exit(1);
}
}