A Closer View of the Shell. What Happens When You Type ls -l?
Firts you have to know what a shell is:
The Shell
is a user interface for access to an operating system’s services. In general, operating system shells use either a command-line interface (CLI) or graphical user interface (GUI), depending on a computer’s role and particular operation. It is named a shell because it is the outermost layer around the operating system kernel.
Commands
is a directive to a computer program to perform a specific task.
What is the Process to Execute a Command?
Prompt:
The firts step is print to the user a specific character that indicates the user can type the commands.
Get the User Command:
Next the shell receive the user’s input with the command requested and they arguments, the system divide that string every whitespace to work wiht it and differentiate between the command and the argument.
Look For the File:
When the system identify the command name, the shell search for the file in the currend directory and every directory listed in the environment variable PATH to determinate if its posible to execute.
Execute the Command:
In order to execute the program the shell need to create another proccess to continue with the shell after that, to do this te shell uses the system calls fork, execve and wait in this way:
- Fork: This system call creates a new process which is a copy of the colling process except that it has its own process id and its own pointers to shared kernel entities such as file descriptors. After fork has been called, two processes will execute the next statement after the fork in their own address spaces — the parent and the child. If the call succeeds in the parent process, fork returns the process id of the newly created child process, and in the child process, fork returns a zero value
- Execve: This system call is used to change the program that the process is currently executing. It has the form
- Wait: This system call is used by a process to block itself until the kernel signals the process to execute again — for example, because on of its child processes has terminated. When the wait call returns as a result of a child process terminating, the status of the terminated child is returned as a parameter to the calling process.
once doing this and the command finish their execution the prompt is displayed and the cycle is repited.