To answer this question, I wrote a tiny "sh.py" shell in Python that simulates the core function of Bash, which is to provide an environment where we can run programs based on commands typed.
do parent and child processes both write to the same fd1,fd2 concurrently? sorry, not an expert here. let me ask that little differently after exec() and replacing current shell program with some other program like echo with args "foo" echo writes $1 to stdout. so do I start to see "foo" getting displayed on terminal right here even before child exits?
Yeah! Try it and see. Both programs will have the same fd0, 1, and 2. One thing bash handles is redirection, by modifying the file descriptors for the child before exec() is called.
@@tashian Why wouldn't the computer just reserve memory for the program and then run it? What if you were running a big process and need to call a simple one? It's going to copy the whole process first? That seems wasteful.
@@sweetness583 It's a great question. Forking a process is efficient and requires very few resources, due to the way memory management works on most computers. Look up "copy-on-write memory management" for more details.