What happens when you type ls -l in the shell

Each program executed by the shell starts in a new process, this one is in charge of a complete task, unloading to the main process of secondary tasks. What does this mean? Well, we need to understand a few main topics to understand what that means.
First, the core: The Kernel
The term Kernel is often used as a synonym to refer to the central software that manages and allocates computer resources (i.e., CPU, RAM, and devices), with complete control over everything in the system, that means the core of a computer’s operating system.
Tasks performed by the Kernel
Among other things, the kernel performs the following tasks:
- Process scheduling: is the part of the kernel that decides which task to run next, manages which processes receive use of the CPU and for how long. This makes it a core component.
- Memory management: the Kernel manages memory allocation both for internal structures and user space, resource that it must share among processes in a equitable and efficient way.
- Provision of a file system: the Kernel provides a file system on disk, allowing files to be created, retrieved, updated, deleted, etc.
- Creation and termination of processes: the Kernel can load a new program into memory, providing it with the resources that it needs in order to run and be terminated after the completion of the execution.. Such an instance of a running program is termed a process
- Access to devices: To perform useful functions, processes need access to the peripherals connected to the computer, which are controlled by the kernel through device drivers, this provides the operating system with information of how to control and communicate with a certain piece of hardware.
- Networking: the Kernel transmits and receives network messages on behalf of user processes
- Provision of a system call application programming interface (API): processes can request to the Kernel to perform various tasks using kernel entry points known as system calls
Summarizing, every time that we run a program we are asking to the Kernel if we can run it.

Second, the Shell
the Shell is a special-purpose program designed to read commands typed by a user and execute appropriate programs in response to those commands. Such a program is sometimes known as a command interpreter.
Whereas on some operating systems the command line is an integral part of the Kernel, on Unix systems, the Shell is a user process. So, when we run the Shell we are asking to the Kernel if we can run it. Are you getting?
Many Shells have appeared over time: Bourne shell (sh), C shell (csh), etc. The history of these will be aboard in another time. In this time we will be using the bash (Bourne again shell) Shell.
The shells are designed not merely for interactive use, but also for the interpretation of shell scripts, which are text files containing shell commands. For this purpose, each of the shells has the facilities typically associated with programming languages: variables, loop and conditional statements, I/O commands, and functions.
Command line Shell
Sometimes, in order to interact with the Shell, we need another program called a terminal emulator. This program allows us to type command lines as input for the Shell. There are a bunch of different terminal emulators you can use. Most Linux distributions supply several, such as: gnome-terminal, konsole, xterm, rxvt, kvt, nxterm, and eterm.
Third, programs
Programs normally exist in two forms. The first form is source code, human-readable text consisting of a series of statements written in a programming language such as C. To be executed, source code must be converted to the second form: binary machine-language instructions that the computer can understand.
As we said before, Shell is program that reads commands and executes programs in response to these commands. But, we need to clarify that some commands are builtin commands and other commands are programs (executable files). Builtin commands are contained within the Shell itself. So, 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.
Until now, we know that when we run a program we are asking to the Kernel to allocate the enough resources to this program, and we know that the Shell is a program that interprets commands in order to run new programs.
How do the programs get its arguments?
First at all, we need to know that commands are written in C programming language. In C, programs can access the command-line arguments, through the main() function of the program when it is declare as follows:
int main(int argc, char *argv[])
The argc variable contains the total number of command-line arguments, and the individual arguments are available as strings pointed to by members of the array argv (Arrays is a kind of data structure that can store a fixed-size sequential collection of elements of the same type), the first of these strings, identifies the name of the program itself.

Filter : It is a program that reads its input from stdin, performs its respective input transformation and returns them written in the stdout output. some examples include cat, grep, tr, sort, wc, sed, and awk.
Fourth, processes:
A process is an instance of an executing program. When a program is executed, the Kernel loads the code of the program into virtual memory, allocates space for program variables, and sets up kernel bookkeeping data structures to record various information about the process.
From a Kernel point of view, processes are the entities between which the kernel must share computer resources. When a program is executed, the kernel assigns the program’s code in virtual memory as variables, data structures and various information about the process. when the processes finish, all the resources are released to be used later by any other program. a process is divided into several segments:
Text: the instructions given to the program.
Data: static variables used by the program.
Heap: it is a swap area for the allocation of additional memory.
Stack: it is an ordered list or data structure that allows storing and retrieving data, being the mode of access to its elements of type LIFO (last in, first out).
Then, every-time that we run a program, a process is created by the Kernel and is assigned to the program a process ID (PID) , a unique integer process identifier. Each process also has a parent process identifier (PPID) attribute, which identifies the process that requested the kernel to create this process. Then, we can run a program several times and always we have a different PID but may be the same PPID.
Process creation and program execution
To create a process called by another process, the fork () system call is used, in which the main process is called the parent and the new process created the child, in nucleus it makes a duplicate of the parent in order to create the child. The child inherits copies of the parent’s data that can later be modified as separate copies.
The child process is generated in an environment independent of the parent, using the execve() system call to load a totally new program, destroying the data, stack and replacing it with new segments.
Last but not least, System calls:
A system call is a controlled entry point into the Kernel, allowing a process to request that the Kernel perform some action on the process’s behalf.
A system call changes the state from the processor to the kernel, in order to access the memory protected by the kernel. A kernel-controlled entry point that allows a process to perform an action on the kernel’s name. These services include the creation of new processes and communication between them through pipelines. each call is identified by a unique id.
Services Provided by System Calls :
- Process creation and management.
- Main memory management.
- File Access, Directory and File system management.
- Device handling(I/O).
- Protection.
- Networking, etc.
Types of System Calls :
- Process control: end, abort, create, terminate, allocate and free memory.
- File management: create, open, close, delete, read file etc.
- Device management.
- Information maintenance.
- Communication.
Then, what happens when you type ls -l?
Now, we know that in order to executes commands we need a command interpreter, the Shell. Now, the Shell becomes the parent of the commands that we type and we know that every command is program.
Then, every-time that we type a command, the shell looks for this command in the environment variable called PATH (PATH is a environment variable that contains the pathname or routes of every command that we install in our computer system) and executes the program, asking to Kernel for allocate resources.
Next, the Kernel creates a new process, allocating resources for the program that the command represents.
Next, the program takes the command line as argument in its main() function and performs all the code.

Authors: Oscar Morales & luiscarvajal
References:
- The Linux Programming Interface. 2010. Michael Kerrisk.
- The Linux Command Line. 2008–2013. William E. Shotts, Jr.
- geeksforgeeks.org
- wikipedia.org