简体   繁体   中英

Why wouldn't this doesn't execute the second printf?

So this mate of mine comes asking for help with some fork/pipe stuff, and his code didn't work.
Starting off I just attributed to it being a mess, but then I got to reading some more, I started stripping away all the stuff that might have been wrong, ended up with this.

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

With as the output:
在此处输入图片说明
It ofc holds after, but what's up with printf here? If you place a newline in the end of the last string, it works.

Writes to stdout are line buffered by default. That means that text written to stdout won't be flushed until a newline character is written.

If you don't write a newline, the text sits in the buffer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM