How different is to create a shell program in C vs C# ?
I’m going to start off by introducing myself. I’m an American and Colombian. I’m a full-stack engineer currently specializing in augmented and virtual reality, two types of tech which I’m very passionate about. Learned most of my software engineering skills through a project-based, peer learning curriculum, which enriched as well my soft skills. Open to new challenges everyday!
This blog will be divided in the following sections:
- What is a shell and what is its purpose.
- How does a shell work
- How are processes manipulated in C vs C#
- How are system calls managed in C vs C#
- What are the pros and cons of creating a shell in C vs C#
What is a shell and what is its purpose
As the website Linux Command says: “Simply put, the shell is a program that takes commands from the keyboard and gives them to the operating system to perform.” — https://linuxcommand.org/. This basically means that a shell is an interpreter of a series a keywords or commands that executes something and achieves a desired action. Each of these commands have specific jobs, each one of them triggers different actions and are used for different purposes.
The shell is usually taking the input commands interactively from the user, but it can be also entered with the use of shell scripts. When you enter a command you are looking for a response of your system, which can be a computer, a PC, a server, etc. and perform an output of your request. There are several commands and they more or less change depending on the operating system your utilizing. The most common operating systems used when using a shell are Linux and Mac, but Windows is also used by some.
In my opinion, its main purpose is to help humans operate with a machine in a more effective and fun way. But here are some other purposes:
- Offering the operator to perform harder tasks. Tasks that without a shell would be much more difficult and time consuming.
- Quickly perform small or big changes in your system or programs that you’re executing in real time.
- Go through directories and files in a more easier way. This allows the human operator fully understand the file navigation system of the technological equipment that he’s using.
- When there is an intermediate understanding, you can start combing the different commands and perform more complex requests in less time, optimizing your time and your technological device.
- It also helps you save time doing repetitive jobs that takes up your time, while you can do other things that are more important. These repetitive jobs can be: cleaning up your files and removing automatically what you don’t need on a daily basis, update your software whenever it’s possible or run a script that says hello whenever you open your computer.
How does a shell work
As I was saying the basic function of a shell is to perform actions based on the commands a user types in, but… how does it work ? What’s happening in the inside of your computer ? What processes are taking place when you type a command ? I’m going to use an example to explain it. So what happens when you type “ls *.c” in your shell program in your Linux or Mac computer.
Before kicking off on what this line of command does, we have to review some basic concepts first, in order to understand fully this topic.
Some of these concepts are: Linux, Ubuntu, Unix, Bash, and Linux Terminal. Linux is the best-known operating system, not only because it’s an open source but also because of all of the components that this software has to offer. Ubuntu is a complete Linux operating system which is also available to all of the community; it’s principles encourage people to work on this software so that everybody can freely make changes in their local machines. Unix is a command-line interpreter that provides every user an interface for Unix-like operating systems. Bash is a Unix shell that usually runs in a text window where Unix users can run commands. The Linux terminal is one of the Linux is one of the system consoles provided in the Linux kernel and acts as a medium between the input and output operations for a Linux system.
Taking into account what we just read, it’s important to highlight that “ls” is a command that lists computer files in Unix and Unix-like operating systems. When you enter “ls” into your terminal without any options nor arguments, it lists all of the files in the directory that you’re working on. When you add the argument * (which is a wildcard) it means that it’s going to list everything that matches any character that you add after or before it, depending if you’re looking that the characters start or finish in the name of the files. In this case we added .c after the asterisk meaning that it will list every file that ends with “.c”.
So if you add all of these pieces of the line of command, it makes your terminal look for all the files that you have in your current working directory that ends in “.c” and lists them all. It’s very important to fully understand this command because it’s one of the most used ones of them all. You will need to list what files you have in a directory very often when manipulating files and directories. This is a basic example of what can be accomplished by typing this command.
How are processes manipulated in C vs C#
Processes in C and C# are each managed differently, here are some examples explaining this thought. Although both use pointers, if you use them in C# you will be using it in an unsafe and not recommended way, this is important to point out since pointers is one of the most important concepts that you will need to know when you are creating a shell.
But what’s a pointer ? Wikipedia defines it as the following: “In computer science, a pointer is an object in many programming languages that stores a memory address. This can be that of another value located in computer memory, or in some cases, that of memory-mapped computer hardware. A pointer references a location in memory, and obtaining the value stored at that location is known as dereferencing the pointer.” — https://wikipedia.com
Another difference among the processes in C and C# is that C does not support or have a garbage collector. In C#, garbage collection is managed by Common Language Runtime or CLR. When you’re planning to create a shell in C# you will need to know that it only runs within the .NET Framework, this is a important aspect to take into mind when comparing processes with C because it would affect your program.
Processes in C# are optimized as much as possible. I would like to point out a thought about memory and optimization that I had, which is the following: “Not all strings are stored in the intern pool, by default only string literals are stored there, for optimization purposes, even though there is no GC. The compiler optimizes string usage by storing string literals in PE metadata and that the CLR adds into the intern pool making sure they are not duplicated. When we’re using the intern pool, we’re increasing the CPU usage because it’s looking if the string already exists, otherwise we’re just consuming memory. Interned strings no longer appear on the heap and are allocated on the intern pool instead. So not all of the strings are allocated in the managed heap basically because there should a balance between CPU and memory usage, which makes it very optimized.”. This thought reflects just how optimized memory allocation is in C#, by just understanding how one type of data, in this case a string, is stored and removed by the CLR and when.
What are the pros and cons of creating a shell in C vs C#
Some of the advantages of creating a shell in C is that it is highly customizable, it offers an environment totally free in order for you to design your shell however you want. It also allows you to have abbreviate commands, which are more like aliases of the keywords you input in the shell when you make the request. It also allows you to manage the history of the commands you have used, it remembers the ones you typed before.
An interesting advantage as well is job control, which allows the programs that will be executed when you enter a command, to run in the background or foreground. Shell scripting is allowed and achieved when using both programming languages, so that’s an important aspect to take in mind.
The majority of what I mentioned above can also be made with C#, but one of the main differences among one and the other is memory optimization and performance. These are two factors that affect significantly the use of the shell made in C and C#.
This blog was made for educational purposes for Holberton School, a project-based, peer learning full stack software engineering school located in: United States, Colombia, France, Lebanon, Mexico, Puerto Rico, Tunisia and Uruguay. Thank you for reading this blog and I hope I helped you out understanding this abstract topic.