-
Command Input:
- The user types
ls -l
in the terminal, and the shell reads this input.
- The user types
-
Command Parsing & PATH Variable:
- The shell parses the command and its arguments.
- The shell searches for the
ls
binary in the directories listed in the PATH environment variable.
-
Permission & Access Check:
- The kernel checks if the current user has execute permissions on the
ls
binary. - If the user doesn’t have the appropriate permissions, the shell returns a permission denied error.
- The kernel checks if the current user has execute permissions on the
-
Forking:
- The shell performs a
fork()
system call to create a new child process.
- The shell performs a
-
Child Process & Executing
ls
:- The child process replaces its image with the
ls
command using anexec()
family system call, such asexecve()
. - The kernel loads the
ls
binary into memory and parses the binary’s header to set up the memory image.
- The child process replaces its image with the
-
Loading Shared Libraries:
- The
ls
binary is linked to shared libraries (like libc), and the kernel, with the help of the dynamic linker/loader, maps these libraries into the process’s address space. - Dependencies of the shared libraries are also loaded if necessary.
- The
-
Running
ls
& System Calls:- The
ls
command starts executing and makes various system calls to read the contents of the current directory and retrieve information about files/directories. - The kernel handles these system calls and performs operations on behalf of the
ls
command. - The kernel also checks file and directory permissions to determine if the
ls
command can access and read the information.
- The
-
Gathering & Outputting Information:
- The
ls
command formats the gathered information and writes the output to standard output (stdout) using thewrite()
system call. - The kernel writes the data to the terminal device.
- The
-
Returning Control:
- After the
ls
command completes its execution, it exits, and control is returned to the kernel. - The kernel cleans up resources and signals the parent shell process about the completion of the child process.
- The shell becomes active again and is ready to accept the next command.
- After the
Created
September 25, 2023 05:58
-
-
Save baurzhan-konurbayev/506fbb79632c7fd57986c7414f53c669 to your computer and use it in GitHub Desktop.
Understanding the Execution of `ls -l` in Linux
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment