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 -l. execve 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

Python Subprocess Module

Subprocess A running program is called a  process . Each process has its own system state, which includes memory, lists of open files, a program counter that keeps track of the instruction being executed, and a call stack used to hold the local variables of functions. Normally, a process executes statements one after the other in a single sequence of control flow, which is sometimes called the main thread of the process. At any given time, the program is only doing one thing. A program can create new processes using library functions such as those found in the os or subprocess modules such as  os.fork() ,  subprocess.Popen() , etc. However, these processes, known as  subprocesses , run as completely independent entities-each with their own private system state and main thread of execution. Because a subprocess is independent, it executes concurrently with the original process. That is, the process that created the subprocess can go on to work on other thing...

INTERNETWORKING TUTORIAL

Contents   [ hide ] 1   Creating a PDF of the Internetworking Technology Handbook 2   Internetworking Basics 3   LAN Technologies 4   WAN Technologies 5   Internet Protocols 6   Bridging and Switching 7   Routing 8   Network Management 9   Voice/Data Integration Technologies 10   Wireless Technologies 11   Cable Access Technologies 12   Dial-up Technology 13   Security Technologies 14   Quality of Service Networking 15   Network Caching Technologies 16   IBM Network Management 17   Multiservice Access Technologies Internetworking Basics An internetwork is a collection of individual networks, connected by intermediate networking devices, that functions as a single large network. Internetworking refers to the industry, products, and procedures that meet the challenge of creating and administering internetworks. The following articles provide information about in...

How to install and setup Docker on RHEL 7/CentOS 7

H ow do I install and setup Docker container on an RHEL 7 (Red Hat Enterprise Linux) server? How can I setup Docker on a CentOS 7? How to install and use Docker CE on a CentOS Linux 7 server? Docker is free and open-source software. It automates the deployment of any application as a lightweight, portable, self-sufficient container that will run virtually anywhere. Typically you develop software on your laptop/desktop. You can build a container with your app, and it can test run on your computer. It will scale in cloud, VM, VPS, bare-metal and more. There are two versions of docker. The first one bundled with RHEL/CentOS 7 distro and can be installed with the yum. The second version distributed by the Docker project called docker-ce (community free version) and can be installed by the official Docker project repo. The third version distributed by the Docker project called docker-ee (Enterprise paid version) and can be installed by the official Docker project repo.  This page shows...