Well, there are a lot of things. I will try to explain this step by step.
When starting the terminal, the terminal creates a special file with the path /dev/pts/<some number> . Then it launches your shell (which in this case is bash ) and associates the STDIN , STDOUT and STDERR the bash process with this special file. This file is called a special file because it does not actually exist on your hard drive. Instead, no matter what you write for this file, it goes directly to the terminal, and the terminal displays it on the screen. (Similarly, whenever you try to read from this file, read blocks until someone types something on the terminal).
Now, when you run your program by typing ./main , bash calls the fork function to create a new process. The child process exec is an executable file, and the parent process, wait is for shutting down the child process. Then your program calls fork twice, and we have three processes trying to read their STDIN s, that is, the same file /dev/pts/something . (Remember that calling fork and exec duplicates and saves the file descriptors accordingly).
Three processes are in a race state. When you enter something on the terminal, one of the three processes will receive it (99 out of 100 times it will be the parent process, since children have to do more work before reaching the scanf instruction).
So, the parent process prints the number and exits first. The bash process, which expected the parent to finish, resumes and puts the STDIN into a so called "non-canonical" mode , and calls read to read the next command. Now again, three processes (Child1, Child2, and bash) are trying to read STDIN.
As children try to read STDIN for a longer time, the next time you enter something, it will be adopted by one of the children, not bash. So you are thinking of typing, say, 23 . But oops! As soon as you press 2 , you will get Your number is: 2 . You didnβt even press Enter! This was due to this so-called "non-canonical" regime. I will not go into what and why. But now, to simplify the task, use the sh program instead of bash , since sh does not put STDIN in noncanonical mode. This will make the image clear.
TL DR
No, the parent process closing it with STDIN does not mean that its children or another process will not be able to use it.
The strange behavior you see is due to the fact that when a parent exits, bash puts pty (pseudo-terminal) in non-canonical mode. If you use sh , you will not see this behavior. Read pseudo-terminals and linear discipline if you want a clear understanding.
The shell process will resume after the parent exits.
If you use wait to ensure that parents leave last, you will not have any problems, since the shell will not be able to work with your program.
Usually bash ensures that two foreground processes are not read from STDIN at the same time, so you do not see this strange behavior. It does this by either sending the STDOUT of one program to another, or by creating one process of a background process.
General information. When the background process tries to read its STDIN , it sends a SIGTTIN signal, which stops the process. Although, this is not particularly relevant to this scenario.