2022-08-10
I wrote part of a shell in Python today, with the os library to make system calls. I'm using a couple of really nice tutorials as well as the shell source code (with the help of this summary).
My shell accepts input and forks to create a child process. It doesn't actually execute commands successfully yet, I might continue working on that another day!
One thing I found interesting was how fork works. When fork is called, the current process is cloned, and both those processes continue stepping through the code. The way to tell the two processes apart is by the return value of fork: in the child process it returns 0, while in the parent process it returns the child's process id.
For example:
import os
fork_return_value = os.fork()
print(f"\n fork return value: {fork_return_value} current process id: {os.getpid()}")
Results in the following being printed:
fork return value: 37625 current process id: 37615
fork return value: 0 current process id: 37625
The first line is printed from the parents process, the second line is printed from the child process. This is useful because it means that in the parent process, we can wait for the child process to finish as we have its id.
I also met a few more people today and had some great chats :)