linux 父子进程无名管道通信 printf部分位置打印不出来

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);
    }
}

img

你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,技术专家团超时未为您做出解答


本次提问扣除的有问必答次数,将会以问答VIP体验卡(1次有问必答机会、商城购买实体图书享受95折优惠)的形式为您补发到账户。


​​因为有问必答VIP体验卡有效期仅有1天,您在需要使用的时候【私信】联系我,我会为您补发。