Skip to content

Instantly share code, notes, and snippets.

@baurzhan-konurbayev
Created September 25, 2023 05:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baurzhan-konurbayev/506fbb79632c7fd57986c7414f53c669 to your computer and use it in GitHub Desktop.
Save baurzhan-konurbayev/506fbb79632c7fd57986c7414f53c669 to your computer and use it in GitHub Desktop.
Understanding the Execution of `ls -l` in Linux

Understanding the Execution of ls -l in Linux

  1. Command Input:

    • The user types ls -l in the terminal, and the shell reads this input.
  2. 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.
  3. 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.
  4. Forking:

    • The shell performs a fork() system call to create a new child process.
  5. Child Process & Executing ls:

    • The child process replaces its image with the ls command using an exec() family system call, such as execve().
    • The kernel loads the ls binary into memory and parses the binary’s header to set up the memory image.
  6. 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.
  7. 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.
  8. Gathering & Outputting Information:

    • The ls command formats the gathered information and writes the output to standard output (stdout) using the write() system call.
    • The kernel writes the data to the terminal device.
  9. 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment