What happens when you type ls -l in the shell
Before we get started let's first understand some basics of Linux Operating System.
WHAT IS THE SHELL ?
The shell is a user interface that allows to user to communicate with the hardware through the kernel.
WHAT IS A KERNEL ?
A kernel is the core of the operating system.
it's main purpose is the resources management like memory management and cpu scheduling.
it permits also the communication between the user and the machine .
SO NOW WE GET SOME KNOWLEDGE LET'S DIVE INTO THE HEART OF THE MATTER.
HOW THE SHELL WORKS ??
- THE GETLINE FUNCTION :
When you open the shell prompt the executable waits for user to type commands
using getline function.
ssize_t getline(char **buffer, size_t *size, FILE *stream);
- CHECK THE ALIASES :
An Alias is a shortcut to reference a command .
- CHECK THE PATH
The path is an environment variable that tells the shell which directory to search for an executable .
- SYSCALL : execve()
int execve(const char *pathname, char *const argv[], char *const envp[]);
The execve is a system call in c language that permits to execute an executable file.
it takes the pathname of the executable, an array of string containing the options and the flags and also an array of strings containing the environment variables.
for instance when we type ls -l in the shell terminal the pathname of ls will be passed as first argument of execve , an array of strings null terminated containing the pathname of the command as the first element, the next array's element contains the options of the command for instance -l
The ls command is used to list the containing of the working directory.
-l option permits to display the long format (permission , etc)
the execve syscall will be executed in child process
a child process is a sub-process executed inside the process of the program.
- HOW TO CREATE A CHILD PROCESS
Fork() is a system call to create a copy of the current process
it returns -1 in case of failure , 0 if the current process is child process and the id of the child process if the current process is the father
Wait() permits to stop the father process until one of its children finished.