fork and printf

1 min read

fork() is non-deterministic, and sometime this can be a real problem. To solve this kind of problem, you may need to rely on your previous experience.

One day when experimenting on fork(), I got a strange print message.

I was surprised and could not figure out why for a while. Since I am using CLion and WSL 2, I soon realized that maybe it was the fault of CLion. So I ran the executable myself. A different print message came to my eyes.

No wonder.

After a long time of thinking, I related to procedures of how shells work. When you type in a command and hit enter, the shell calls fork() to create a new child process to run the command. The control soon returns to the shell after the command program is finished. However, if the command program creates a subprocess, and the subprocess is still running even if the main process has already returned, this can happen.

In order to check my assumption, I decided to wait() for the child process to finish. And this time it worked perfectly.

Be careful of the environment you are using. Search the database in your brain.

CC BY-NC 4.0 2023 © donaldnevermore.RSS