Writing a Shell in C vs C#

If you’re someone who has spent time using a computer you have probably spent a lot of time using a shell. A shell is one of the most central parts of an operating system on a computer, but most people are not familiar with this term. So what is a shell? A shell is a computer program that takes in user input and allows the user to access all of the different tools provided by the operating system. These shells generally fall under one of two categories, command-line shells, and graphical shells. A command line shell is a text based shell that allows the user to do things like type commands and enter data. Command line shells have been around for longer than graphical shells, and are usually designed to be used with a keyboard and do not require a mouse. A graphical shell serves the same purpose as a command line shell, but instead of relying on the keyboard and typing in all commands, a graphical shell makes use of a mouse and provides a graphical user interface (GUI) for users to interact with the operating system. Most modern operating systems use a graphical shell, including Windows and MacOS. In both of these operating systems the desktop, start menu, task bar, and file explorer are all part of the graphical shell. In the end both of these approaches to a shell achieve the same thing, and both have their pros and cons. Graphical shells are generally more intuitive and easier to learn. Instead of having to memorize and type a large number of commands, you can navigate with a mouse and click on the commands that you want the operating system to execute. The downside of this is that it can be significantly more time consuming to do certain tasks in a graphical user interface environment. If you’re willing to take the time to learn the commands in a command line shell, you can accomplish many tasks quickly and efficiently.

When building a shell there are many different decisions that have to be made along the way. One of the most important decisions is choosing a programming language for the project. The chosen language will have a big impact on the way that you approach making the shell, and also the performance of the end product. In this article we’ll be examining the differences between building a command-line shell in C vs C#.

Although the names are very similar, there are some big differences between C and C#. The most major difference between these two languages is the programming paradigm. C is a procedural programming language and C# is an object oriented programming language. This results in C having better performance than C#. Another key difference between these languages is garbage collection. Being a low-level language C does not have any garbage collection, and requires the programmer to manage all memory manually. C# on the other hand does have built in garbage collection, meaning the programmer doesn’t have to waste time worrying about memory allocation and freeing. Overall C is a much smaller language than C#, containing only 32 keywords in comparison to C# with 86.

When building a command line shell in these languages there will be some similarities and some differences. First let’s look at some of the similarities between the two. For both languages the program will start out by printing out the prompt, which can be anything, but in a lot of shells it is a dollar sign. The program will then wait for input from the user, which will be very similar in both languages. After the user enters their command the shell will have to parse the input, checking for things like pipes, semicolons, and conditional statements. This process will also be very similar, differing mostly in syntax. After the input is parsed the next step is to execute the commands. This is where some of the major differences between the languages begin.

One of the big differences between these two languages when building a command line shell is the way that they manipulate processes and handle system calls. The main way of manipulating processes in C is to use a function called fork(). The fork() function works by duplicating the current process, creating a child process that can be used to do other things while the first process continues to run. This method works great on Unix based operating systems because fork is a Unix system call, but it doesn’t work on Windows. There are some different ways to get around this problem, but they come at a cost. You can use something like Cygwin which has a working implementation of fork() on Windows, but emulating this feature is complex and comes at a pretty big performance cost. On the other hand C# was originally designed specifically for Windows and has its own built in ways of handling processes and system calls in that operating system. There are also some workarounds that allow you to access Unix system calls using C#. An open source project called Mono brings cross-platform support to C# and also gives Unix developers access to these system calls. In both languages the shell will start a new process and use this process to execute a system call or run a program.

The decision between using C and C# to build a shell will come down to a few different things. If your decision is being made based on the amount of time and effort involved, C# is probably going to be the better choice. Because C# is an object-oriented, garbage-collected language it will allow the developer to avoid some of the more tedious aspects of C, like memory management. If your main goal is to get the best performance possible, you’re probably better off choosing C, especially if your target platform is a Unix based operating system.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store