1
#include <stdio.h>
#include <unistd.h>

int main()
{
    pid_t pid;  

    pid = fork();
    printf("pid : %d\n", getpid());

    if( pid == 0)
    {
        printf("child: pid : %d \n", getpid());
        while(1);
    }
    else
    {
        printf("parent: pid : %d \n", getpid());
        //while(1);
    }
}

In the above code snippet inside if statement if we put while(1), it doesn't remains blocked and when enter key is pressed program is exited, but in case of parent if we put while(1), parent remains blocked until we give ctrl+c. Please clarify this behaviour of child.

P.P
  • 117,907
  • 20
  • 175
  • 238
emb
  • 33
  • 1
  • 6

1 Answers1

0

In the above code snippet inside if statement if we put while(1), it doesn't remains blocked

The child process doesn't exit actually; it just becomes an orphan process because its parent exits. The orphaned chuld process will be adopted by the init process of your system. You can see it via ps command.

But if you put the while(1); in the parent process it remains blocked.

Basically whichever process has while(1); infinite loop, it's still running. When parent exits you get the prompt back and the child becomes orphan. But the child process is still running.

In general, you need to wait(2) for the child process in the parent process to reap child processes.

P.P
  • 117,907
  • 20
  • 175
  • 238
  • Thanks. I agree to that, technically that's fine. If you execute the program, with while(1) in parent, even after entering the "enter key" multiple times parent gets stuck in the while loop and the [linux ~$] prompt is not displayed but that's not the case with the child. With one enter key, prompt is displayed. I am quite not understanding this behavior. – emb Dec 08 '17 at 09:52
  • Your shell started the parent process, say X. So when it dies, you get the prompt back. It doesn't know/care about other processes started by X. But if you put the loop in the parent process, the shell waits for it. In other words, the shell waits for its child processes (not for processes started by its child processes). – P.P Dec 08 '17 at 09:58
  • Great! Thanks I got it. Do you recommend any book or online material from where you got this knowledge. I too want to improve myself on these things . – emb Dec 08 '17 at 10:02
  • For Linux system programming, I'd suggest the book "The Linux Programming Interface" by Michael Kerrisk. – P.P Dec 08 '17 at 10:06
  • @user9071725 [The Definitive C Book Guide and List](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) – Stargateur Dec 08 '17 at 10:10
  • @Stargateur I don't think OP was asking for a recommendation for learning C. So that's not quite relevant. "The Linux Programming Interface" is not a "C book" either. – P.P Dec 08 '17 at 10:16
  • @usr Oh you right, I didn't read correctly, well whatever this list can't do harm ;). – Stargateur Dec 08 '17 at 10:19
  • @Stargateur "can't do harm" - that's true. OP might still find your link useful! – P.P Dec 08 '17 at 10:22