When for the first time I had to program my own shell I realized that I had to address several concepts that at that time were totally unknown to me, which I had to investigate and read a lot, even so, this did not avoid facing some troubles during the process, in this blog, I do not intend to go into depth about it but I do want to clarify some concepts behind the creation of a basic Shell and some points of comparison when doing the process in C or C #.
What is a shell?
A shell is a term used in computing to refer to the command interpreter of operating systems based on Unix and the like, such as GNU / Linux, and that is its traditional user interface. Through the instructions provided by the interpreter, the user can communicate with the kernel and, by extension, execute these commands, as well as tools that allow him to control the operation of the computer. For this reason, it was called thus, because it is the visible envelope of the computer system.
The commands provided by the interpreters can be used as a script if they are written in executable files called shell-scripts, in this way, when the user needs to make use of several commands or combinations of commands with tools, they write in a text file, marked as executable, the operations that later, line by line, the interpreter will translate to the kernel for it to perform. Without being a shell strictly a programming language, the process of creating shell scripts is called shell programming or in English, shell programming or shell scripting.
what is its purpose?
A command interpreter is a computer program that accepts text commands interactively, from the keyboard; or reads them from a file, and executes them. The purpose of a shell, therefore, is to execute commands, such as defining dynamic variables that contain information about the system environment, known as “environment variables”, or to load and execute other files.
How does a shell work?
As we already mentioned, the Shell is a command interpreter. It is simply an alternative way of controlling a computer based on a text interface. We can ask the computer to run a program or provide us with information by means of the mouse cycling in different places on the desktop or we can write an order to achieve the same objective. For example, to ask the computer to give us a list of the files present in a directory, we can open a file browser or we can write in the shell:
Neither of the two ways of communicating with the computer is better than the other, although on certain occasions it may be more convenient to use one or the other, although some individuals with long beards comment that real men use the command line. The advantages of the command line are:
Flexibility. Graphics programs are often well suited for the task for which they were created, but they are difficult to adapt for other tasks. On the contrary, the Unix command line is very flexible since it is made up of small tools that we can combine according to our needs.
Reproducibility. Documenting and repeating the process followed to perform an analysis with a graphic program is very expensive since it is difficult to describe the sequence of clicks and double clicks that we have made. On the contrary, the processes carried out through the command line are very easy to document since we only have to save the text that we have entered on the screen.
Reliability. Basic Unix programs were created in the 1970s and have been tested by countless users and have become remarkably reliable pieces of code.
Need. There are applications that can only be used through the command line.
Velocity. Graphical interfaces are often resource-intensive, while command-line programs are often extremely light and fast.
What about the manipulation of processes and system calls?
First of all, I guess it important to take a brief concept about those topics:
What is a Process?
A process is an environment in which an application program runs.
A process includes virtualized resources that its program can use: — one (or more) threads — virtual memory, used for the program’s code and data — other resources, e.g., file and socket descriptors
Processes are created and managed by the kernel.
Each program’s process isolates it from other programs in other processes.
What is a System Call?
System calls are the interface between processes and the kernel.
A process uses system calls to request operating system services.
(create,destroy,manage processes) — fork,execv,waitpid,getpid
(create,destroy,read,write files) — open,close,remove,read,write
(manage file system and directories) — mkdir,rmdir,link,sync
(interprocess communication) — pipe,read,write
(manage virtual memory) — sbrk
(query,manage system) — reboot, time
Although C and C# share many similarities, they have many differences, many of which involve process manipulation and system call handling.
Processes in C and C# are managed differently, although they both use pointers, if you use them in C# you probably use it in an insecure and not recommended way, it is important to point this out as pointers are one of the most common concepts. important things you will need to know when creating a shell.
One of the most important differences 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 performing better than C#.
Another key difference between these languages is the garbage collector. Being a low-level language, C has no garbage collector and requires the programmer to manage all memory manually. C#, on the other hand, has a built-in garbage collector, which means that the developer doesn’t have to waste time worrying about memory allocation and freeing. In general, C is a much smaller language than C#, containing only 32 keywords compared to C # with 86.
Are there pros and cons of creating a shell in C vs C#?
According to what we have been explaining in this blog, it is likely that you are already clear that if there are differences not only between the two languages, which also brings with it the fact that when creating a shell in C it will have differences when creating it in C #.
As we mentioned, C is a low-level programming language with no garbage collection, therefor developers must take this into account when managing memory.
C# instead is a higher-level programming language and it has a garbage collector built-in, removing one less thing a developer has to worry about. However, being an object-oriented language a program developed in C# could be larger, and less efficient, in terms of speed, when compared to C.
On the other hand, although C# as a language does not incorporate a Garbage Collector, its famous .NET framework does and whose garbage collector support is included in the .NET CLR (Common Language Runtime). Allowing for example to write C# code in an object-oriented way (encapsulation, polymorphism, and inheritance) or benefit from certain features that the language provides.
Another difference is that C# was primarily conceived to power software on Windows operating systems. Which differs from C and C++ as they are both cross-platform and cross-device.
Having said this and from my purely personal point of view I think that by the definition of shell and its conceptualization as such I would use C to program a shell since although some tasks may arise that may not be easily approachable with C, it would be light and quickly software and it would have great scalability, but it would always leave the door open to alternatives with the use of C# to enhance certain functionalities and, as is popularly said, not to reinvent the wheel.
Unix shell - Wikipedia
A Unix shell is a command-line interpreter or shell that provides a command line user interface for Unix-like operating…
syscalls(2) - Linux manual page
The system call is the fundamental interface between an application and the Linux kernel. System calls and library…