Understanding Process and Its Key Commands

Understanding Process and Its Key Commands

Day 9: Navigating Linux Processes

1. What is a Process?

  • A process is a program in execution, which includes the program code (text section), current activity (stack), and the process's memory (data section).

  • A process refers to the active execution of a program.

  • It consists of several components, including data retrieved from files, user input, program instructions, etc.

  • In Linux, a process is initiated whenever an application is launched, a program is run, or a command is executed.

  • Each program or command in Linux creates only one process, but an application on the other hand can initiate multiple processes to fulfill different tasks.

  • In Linux systems, processes are often created by forking off from an existing process known as the parent process.

2. Process States

A process can be in one of several states during its lifecycle:

All You Need To Know About Processes in Linux [Comprehensive Guide]

  • Created : A new process is normally created when an existing process makes an exact copy of itself in memory.

  • Running: The process is currently being executed by the CPU.

  • Waiting: The process is waiting for some event to occur (like I/O completion).

  • Stopped: The process has been stopped, usually by a signal.

  • Zombie: The process has completed execution but still has an entry in the process table to report its exit status to the parent process.

3. Process Lifecycle

The lifecycle of a process generally follows these steps:

  1. Creation: A process is created using the fork() system call. This creates a child process that is a duplicate of the parent process.

  2. Execution: The child process may execute the same program or a different one using exec() family of functions.

  3. Waiting: The parent process may wait for the child process to complete using wait().

  4. Termination: Once the process finishes execution, it can terminate normally or abnormally.

4. Process Identification

Every process in Linux has a unique identifier known as the Process ID (PID). Additionally, processes can be associated with:

  • Parent Process ID (PPID): The PID of the parent process.

  • User ID (UID): The ID of the user who owns the process.

  • Group ID (GID): The ID of the group associated with the process.

5.Linux Process Commands

Linux has many commands for handling and interacting with processes. Here are some popular Linux process commands:

  1. top:
    This command is used to show all the ongoing Linux processes within the working environment of Linux.

    Syntax:

     top
    
  2. bg:
    This is a job control command that resumes a stopped job while keeping it running in the background.

    Syntax:

     bg jobname
    
  3. fg:
    This is a job control command that resumes a stopped job while keeping it running in the foreground.

    Syntax:

     fg jobname
    
  4. ps:
    The ps command shows information about Linux processes that are currently running. This command is similar to the top command, but the output is different.

    Syntax:

     ps ux
    

    To check the status of a single Linux process.

     ps PID
    
  5. nice:
    The nice command is used to start a new process while specifying a priority (nice) value. The nice value goes from -20 to 19, with -20 being the highest priority.

    Syntax:

     nice [-nice value]
    
  6. kill:
    This command is used to end Linux processes or transmit signals to them. You can stop a process gracefully or forcefully by specifying the process ID (PID).

    Syntax:

     kill PID
    
  7. df:
    This command is used to display the free disk space(Hard Disk) on all the file systems.

    Syntax:

     df
    
  8. free:
    This command is used to display the total amount of free and used memory (RAM) on the Linux system. Syntax:

     free
    
  9. top Command

    The top command provides a real-time, dynamic view of the system’s processes, including CPU and memory usage. It continuously updates, allowing you to monitor system performance in real-time.

    Syntax:

     top
    
  10. ps Command

The ps (process status) command displays information about the running processes. By default, it shows processes running in the current terminal session. It's useful for viewing process IDs (PIDs), CPU usage, memory usage, and more.

Syntax:

ps [options]

Commonly Used Options:

  • ps: Shows processes for the current shell session.

  • ps aux: Displays all running processes from all users.

    • a: Includes all users’ processes.

    • u: Displays detailed information (user, CPU, memory, etc.).

    • x: Includes processes that are not attached to a terminal (like services).

Example:

ps aux

This command lists all processes, including their PID, user, CPU and memory usage, start time, and the command that started the process.

Conclusion :

A process refers to the active execution of a program. It consists of several components, including data retrieved from files, user input, program instructions, etc.In Linux systems, processes are often created by forking off from an existing process known as the parent process.In Linux, processes can run in the foreground or background. Foreground processes accept command-line input and output it to the computer screen, whereas background processes are non-interactive and do not require keyboard input.