What happens when you type `ls -l *.c` in the shell
What is the shell?
The shell is the Linux command line interpreter. It provides an interface between the user and the kernel and executes programs called commands. For example, if a user enters?ls?then the shell executes the?ls?command. The shell can also execute other programs such as applications, scripts, and user programs.
What is the kernel?
The kernel is the essential center of a computer?operating system. It is the core that provides basic services for all other parts of the operating system. It is the main layer between the OS and hardware, and it helps with process and memory management, file systems, device control and networking.
Command
A?command?is a directive to a computer program to perform a specific task. ?It may be issued via a?command-line interface, such as a?shell, or as input to a network service as part of a?network protocol. The command ls lists the contents of your directory, for example:
领英推荐
How does this work?
The commands are read in the shell by the function getline, which is a standard function of C, and save it as a string into the buffer. Then this string is tokenized by the function split. This function split separate the string with a delimiter. To print the prompt we use the write function.
At first the shell checks if the command is a built-in or an alias. If it's a built-in it will execute the function and return to the prompt. To execute the function the shell uses a syscall fork to create a new process and execve to execute it. Until the exceve is runing the main function is in wait state with the syscall wait. The syscall wait is a function that waits for the first child of the main finish the process.
If the command is an alias, the shell searches for the executable file in the PATH variable. In this variable contains the directories of the differents command executable files. To do this first it tries to execute the command tokenized, if this fails then the command is concatenated different directories of path to see if it matches any of the standard executable files, with the function buscopath. If the directory is not found, it will print an error:
Authors: Santiago Negreyra and Camila Abdala
12/07/2021