-- Leo's gemini proxy

-- Connecting to moddedbear.xyz:1965...

-- Connected

-- Sending request

-- Meta line: 20 text/gemini

Tiny Shell in C


Been working on a project for a class that has me trying to build a basic command line shell in C.


It's pretty interesting stuff. When I started using Linux I didn't fully realize that bash was just another program installed until I started learning about alternatives like zsh and fish. It's been a lot of fun learning how the system works on such a low level.


For example, running a program through a shell isn't quite as straightforward as you might think. First the shell forks itself, so for a moment you have two shell processes running. Each process then has to figure out if it's the parent or the child, do any prep work like setting the child process group id, then the child executes the program that was specified and that program completely takes over the child process.


One of the requirements for the shell that I had to build was to implement support for piping and redirection. These were both slightly complicated on paper just because you have to think about which output has to get sent to which input and then edit your file descriptors accordingly. In code it was more simple than I expected because there are already functions in C that handle a lot of the heavy lifting.


Another requirement was job control, so being able to run multiple child processes at once with the ability to foreground and background any one of them. This was also surprisingly simple to implement. The main "gotcha" was making sure the shell handled SIGINT and SIGSTOP signals correctly and reaped processes that had finished.


All in all, fun project. This lower level stuff usually scares me off and I thought I would hate C, but it's been neat to build a simple version of a tool I use every day.


Links

Home

-- Response ended

-- Page fetched on Fri Apr 26 15:10:50 2024