为什么这样不执行第二个printf呢?

时间:2018-11-12 18:36:23

标签: c linux ubuntu gcc posix

所以我的这个同伴来寻求一些叉子/烟斗方面的帮助,但他的代码无法正常工作。
一开始,我只是将原因归结为一团糟,但后来我又读了一些,开始剥离所有可能出错的内容,最后得到了答案。

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <wait.h>

typedef void (*tFunction)();

pid_t CreateProcess(tFunction toExecute){
    pid_t pid = fork();
    if(pid)return pid;
    else  {toExecute();exit(0);}
}

void Producer_1(){
    printf("IM PROCESS 1\n");
    printf("Why I no print");
    while(1){}
}
int main(){
    CreateProcess(Producer_1);
    wait(0);
}

以作为输出:
enter image description here
它通常保持不变,但是这里的printf是怎么回事? 如果将换行符放在最后一个字符串的末尾,它将起作用。

1 个答案:

答案 0 :(得分:1)

stdout的写操作默认是行缓冲的。这意味着写入stdout的文本只有在写入换行符后才会被刷新。

如果您不写换行符,则文本将位于缓冲区中。