所以我的这个同伴来寻求一些叉子/烟斗方面的帮助,但他的代码无法正常工作。
一开始,我只是将原因归结为一团糟,但后来我又读了一些,开始剥离所有可能出错的内容,最后得到了答案。
#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);
}
答案 0 :(得分:1)
对stdout
的写操作默认是行缓冲的。这意味着写入stdout
的文本只有在写入换行符后才会被刷新。
如果您不写换行符,则文本将位于缓冲区中。