Skip to main content

What happens from Keyboard to Kernel ?

Brief introduction to Linux kernel and shell

Users new to Linux will want to familiarise themselves with the following:
  • terminal is a program that opens a window and lets you interact with the shell
  • shell is a program that takes commands from the keyboard and gives them to the operating system to perform. Bourne-Again shell, usually referred to as bash, which is the default shell for most Linux distributions. When you first login to a server, you will be dropped into the command prompt, or shell prompt, which is where you can issue commands to the server. The shell will forward its input (usually, from your own key presses) to the running program’s stdin, and it will forward the program’s output (stdout and stderr) to its own output (usually displayed on your screen). The shell is an interface that translates commands into some low-level calls to the kernel.
  • kernel is the essential center of a computer operating system, the core that provides basic services for all other parts of the operating system. A kernel is the lowest level of easily replaceable software that interfaces with the hardware in your computer. The kernel is the part of the operating system that runs in privileged mode. It does all sorts of things like interact with hardware, do file I/O, and spawn off processes. The shell is a specific program which runs in user-space (unprivileged mode). Whenever you try to start a process with the shell, the shell has to ask the kernel to make it. In Linux, it would probably do this with the system calls fork and execve.
  • command prompt looks like this user_name@hostname:~$
* ~: is the current directory,
* $: is the prompt symbol
* # is symbol that ends the command prompt if you are logged in as root
Commands can be issued at the command prompt by specifying the name of an executable file, which can be a binary program or a script. To execute a command simply type in the name of the command and hit RETURN.
  • ls lists the files and directories and it can take different options and arguments or it can be used on its own in which case it will just list files in current directory.
In our case if we do ls -l it will print a “long listing”, which includes extra details such as permissions, ownership, file sizes, and timestamps.
And if we give it an argument like /usr (ls -l /usr)it will print long listing of files in /usr directory. But let’s just see what happens if we type only ls -l

Let’s look at the steps:

1. type ls -l into command line first

user_name@hostname:~$ ls -l

2. getline system call stores user input into a buffer

getline reads an entire line from standard input, storing it into a buffer. When user enters command into shell, getline will store the input once the newline has been processed and stored into buffer for later use.

3. break the command into tokens

The input line gets tokenized or separated into individual tokens (pieces or words) so in this case we would have ls and -l (space would be a delimiter we are separating them by)

4. check for aliases

bash alias is essentially nothing more than a keyboard shortcut, an abbreviation, a means of avoiding typing a long command sequence (example typing ll instead of ls -l could be an alias. So if there is an alias shell will replace it with a real command that alias stands for.

5. check for built-ins

Builtin commands are contained within the shell itself. When the name of a builtin command is used as the first word of a simple command, the shell executes the command directly, without invoking another program.

6. find command in PATH

If the command is not found in built-ins shell will next try to find it in the PATH environment variable. Every shell has environment list of all the environment variables. All the environment variables are stored in a form of NAME=value for PATH that looks like this: To print out the value of the PATH variable, you may use the echo command
user_name@hostname:~$ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

7. build the absolute path of the command

Colons separate all the directories that can hold the command the user typed in a form of binary executable. In our case ls. The shell will use one of the system calls like access system call to check if the command is binary executable and access would actually have to take in absolute path of the command (in our case /bin/ls) and if it is it would invoke execve system call that would execute our command.

8. create a process using “fork” system call

If the program is found and the shell now has the absolute path of the command means we can call execve system call but before we do that we have to create another process using “fork” system call. So we do that so that we get the command prompt back after execve is done executing. If we don’t wait for it to complete it will exit our shell. We do that by using system call wait in the process that created a new process. New process is called child process and process that created it is called parent process.

9. execute the command using the execve system

Once we are in child process we can call our execve system call to execute our command ls -lexecve takes in as parameters our command, list of all the arguments and environment list so it would look like this:
execve(“/bin/ls”,{“ls”, “-l”}, environ)
Parent process in our case would be the shell which called ls -l command and child process would be the one that executes that command so if we want to continue inputting new commands we need the shell to print the prompt again and that will happen only after ls -l is done executing in execve and child process is done. Then the parent process will stop waiting and show us command prompt again.

10. print the prompt using PS1 and wait for new command to be entered

At the point, shell is waiting for us to enter a new command to process.

Comments

Popular posts from this blog

Sexy C#

Download samples   Table of Contents   1.   Introduction  2.   Background    3.   Sexy Features 3.1.   Extension Methods   3.2.   Anonymous Type   3.3.   Delegate   3.4.   Lambda Expression 3.5.   Async-Await Pair   3.6.   Generics   4.   Conclusion   1. Introduction     C#  is a very popular programming language. It is mostly popular in the .NET arena. The main reason behind that is the C# language contains so many useful features. It is actually a multi-paradigm programming language. Q.   Why do we call C# a muti-paradigm programming language? A.  Well, C# has the following characteristics:  Strongly typed   Object Oriented  Functional  Declarative Programming  Imperative Programming   Component based Programming Dynamic Programming ...

10 Core Concepts for network administration

The 10 core concepts that every Windows network admin must know. These are the things that you not only need to know in your day to day job as a Windows Network Admin but for anyone who is interviewing as a network admin. Introduction Recently a relative of mine went for a job interview as a security analyst. She was asked a number of technical questions in the interview but the ones that she struggles with the most were the networking questions (as she had not used or studies networking in some time). I thought that this article might be helpful for Windows Network Admins out there who need some "brush-up tips" as well as those who are interviewing for network admins jobs to come up with a list of 10 networking concepts that every network admin should know. So, here is my list of 10 core networking concepts that every Windows Network Admin (or those interviewing for a job as one) must know: 1.     DNS Lookup The domain naming system (DNS) is a cornersto...

Banyan VINES

Banyan VINES Banyan Virtual Integrated Network Service (VINES) implements a distributed network operating system based on a proprietary protocol family derived from the Xerox Corporation's Xerox Network Systems (XNS) protocols. VINES uses a client/server architecture in which clients request certain services, such as file and printer access, from servers. This article provides a summary of VINES communications protocols. The VINES protocol stack is illustrated in  Figure: The VINES Protocol Stack Consists of Five Separate Levels . Guide Contents Internetworking Basics LAN Technologies WAN Technologies Internet Protocols Bridging and Switching Routing Network Management Voice/Data Integration Technologies Wireless Technologies Cable Access Technologies Dial-up Technology Security Technologies Quality of Service Networking Network Caching Technologies IBM Network Management Multiservice Access Technologies Contents   [ hide ] 1   Figure:...