Forking a process prints differently each time

I have a piece of code to print out PID's once Forked, and just a basic user input thing, but each time I run the script, sometimes it runs as expected and it all prints out within one command, then the next time the child prints it's PID, it does so on a next line and leaves the terminal in a state in which you need to press enter for it to continue. I've researched this website about people flushing at what not, which I've added literally everywhere but I still can't get it to work.

My code:

#include <unistd.h>
#include <stdio.h>
#include "sys/types.h"
int main(){
int pid;
int userInput;

    printf("Please enter a number: ");
    userInput = getchar();

    printf("\nYou entered: ");
    putchar(userInput);
    printf ("\n");
    fflush(0);

    pid = fork();
    if (0 == pid)
    {
        printf ("I am the child process, my PID is %d \n", getpid());
        fflush(0);
    }
    else
    {
        printf ("I am the parent process, my PID is %d \n", getpid());
        fflush(0);
    }
return 0;
}

And this is what happens when I run it a few times:

Could someone point out where I've gone wrong and how to correct it?

Thanks!

2 answers

  • answered 2018-04-14 15:45 selbie

    Because there is no guarantee of scheduling and priority between the parent process and child process.

    The usual case as you are observing is the following events:

    • Parent process prints its line
    • The child process, which runs in the background, prints its line
    • Command prompt returns (as a result of the parent process exiting)

    But what you are observing on occasion is this:

    • Parent process prints its line
    • Command prompt returns (as a result of the parent process exiting)
    • The child process, which runs in the background, prints its line - on top of the command prompt

    Although not shown in your example series of runs, the following sequence is also valid and possible.

    • The child process, which runs in the background, prints its line first.
    • Parent process prints its line after the child process
    • Command prompt returns (as a result of the parent process exiting)

    The only causal ordering that is guaranteed is the command prompt appearing after the parent process has printed its line and exited. How the OS schedules the forked child process to run relative to the parent process is non-deterministic. The child process can print first, last, or in between the events of the parent process.

    If you want to guarantee an ordering of printed statements between child and parent process, you'll need to use a cross-process synchronization primitive to coordinate between child and parent process.

  • answered 2018-04-14 15:52 achal

    Forking a process prints differently each time ? when you do fork() 1st parent will run and it runs in foreground and then child will run and its run in background. Now reason you are getting differently because if foreground process completed background process comes to foreground & displayss its output & vice versa.

    If command prompt is free i.e if parent completed then child will come to foreground & displays its output, shell won't stop child process being executed on foreground.

    Note that sequence of execution of child & parent process may defer as its OS dependent.