Search code examples
cforkinfinite-loopwait

The statements before wait() aren't being executed, even though the loop isn't infinite (but the children are). Why is that?


#include<stdlib.h>
#include<stdio.h>
  
  
int main()
{
    int j = 0;
    int jump = 1;
    while(1)
    {
        pid_t pid[4];
        for (int i = 0; i < 4; i++)
        {
            if (fork() == 0)
            {
                switch(i)
                {
                    case 0:
                        pid[i] = getpid(); 
                        while(1) {//printf("Parent Process: %d\nChild Process 1: %d\n", getppid(), getpid());
                         sleep(5);}
                        break;
                    case 1:
                        pid[i] = getpid(); 
                        while(1) {//printf("Child Process 2: %d\n", getpid());
                         sleep(5);}
                        break;
                    case 2:
                        pid[i] = getpid(); 
                        while(1) {//printf("Child Process 3: %d\n", getpid());
                         sleep(5);}
                        break;
                    case 3:
                        pid[i] = getpid(); 
                        while(1) {printf("Child Process 4: %d and pid[%d] is %d\n", getpid(), i, pid[i]);
                         sleep(5);}
                        break;
                    default:
                     printf("Something went wrong..");
                }
            }
        }
        printf("why doesn't this print");
        //pid_t killedPid =
        wait(NULL);
        //printf("Child %d Died", killedPid);
        //for (int i = 0; i < 4; i++)
        //{
            //printf("\nTesting against%d: %d\n", i, pid[i]);
            //if (killedPid == pid[i])
            //{
               // j = i;
               // printf("\n\nProcess %d (Child %d) Killed. Recreating Child.\n\n", pid[i], j);
            //}
        //}
        //sleep(4);
        //jump = 4;
    }
    return 0;
}

Sorry if the code or question format is messy (I'm new to C and it's my first time posting here).

The print statement right before wait() does not print, if I remove wait() it does. I know the child loop is infinite, but from what I've gathered, statements before wait should be executed normally by the parent process.


Solution

  • I'm not myself an expert, but I have the impression that nothing is being printed because the text is not being flushed to stdout. In some programming languages when you print something, the text is not directly printed but instead it is saved in memory (also known as buffer in some contexts like this one). This is for performance reasons. Sometimes it is more performant to print lots of text to stdout at once than it would be to print small amounts of texts many times.

    When you "flush" the text you are removing it from the buffer and printing it to stdout. This is usually done automatically for instance before the process ends or when the buffer is full. If I recall correctly you can also flush the text by printing a line break "\n" because (I am not 100% sure if this is the actual reason, I'm speculating here) in some systems like linux some programs process input line by line and this way the program doesn't have to wait the buffer to be filled or the process finished to start working on the input.

    The problem is then that the text is being stored in memory but not printed because it is not being flushed. Try the following:

    1. Printing a newline "\n" after the text
    2. Printing a really long string that wouldn't fit in the buffer.

    If I'm right in both cases the text should be displayed in screen. You can also use the fflush() function @pmg suggested in the comment section to explicitly flush the text.