Mastering Linux Processes: A Comprehensive Guide to Understanding, Managing, and Optimizing Processes in Linux

Mastering Linux Processes: A Comprehensive Guide to Understanding, Managing, and Optimizing Processes in Linux

·

6 min read

Linux operating systems are renowned for their robustness and multitasking capabilities, empowering users to seamlessly run multiple processes concurrently. In this comprehensive exploration, we unravel the complexities of Linux processes, delving into core concepts, diverse process types, their states, and indispensable command-line tools for proficient process management and monitoring.

Introduction

Processes in Linux serve as the fundamental units of execution, orchestrating tasks within the operating system. Think of a process as any live program or application on your system—an instance of a program in action, utilizing system resources like CPU, memory, and storage.

Types of Processes

Linux processes come in various flavors, each with its unique role:

  • User Processes: Launched by regular users like you and me, these processes operate in user space, independently and without disrupting other tasks.

  • Daemon Processes: Think of daemons as silent guardians. They run in the background, managing essential services without bothering users. Daemons kick into action as soon as your system boots up, ensuring everything runs smoothly behind the scenes.

  • Kernel Processes: Operating exclusively within the kernel space, these processes have unparalleled power, accessing core system data structures. Although incredibly potent, they lack the flexibility of user and daemon processes.

Lifecyle of Processes

  • Process is forked (process graduated from college).

  • Process is in running state (process got job)

  • Process is waiting (process is waiting to get married).

  • Process dies (Finally process dies in happiness!)

Process States

Linux processes have two primary states:

  • Running State: Imagine a process as an athlete sprinting on the CPU track, actively executing instructions and keeping your system alive and kicking.

  • Not Running State: Processes in this state are like performers waiting backstage. They could be in various sub-states, like waiting for input/output operations to complete or dealing with the aftermath of child processes.

Tools for Process Management

Now, let's talk tools! Mastering Linux processes is all about knowing the right commands. Here are some gems every Linux enthusiast should have in their toolkit:

1. ps Command:

The ps command provides a snapshot of the current processes. Key options include:

  • ps aux: Display detailed information about all running processes.

  • ps -e | grep process_name: Find a specific process by name.

2. top Command:

top is an interactive command-line tool that dynamically displays real-time information about system processes. It highlights resource-intensive processes, helping users identify and manage them efficiently.

3. kill Command:

The kill command terminates processes based on their Process ID (PID). Common usage:

  • kill PID: Terminate a specific process using its PID.

  • killall process_name: Terminate all instances of a specific process by name.

4. nice and renice Commands:

The nice command sets the priority of a process when launching it:

  • nice -n 10 command_name: Launch a process with lower priority.

  • renice priority PID: Change the priority of an existing process using its PID.

5. bg and fg Commands:

  • bg %job_number: Puts a running job into the background.

  • fg %job_number: Brings a background job to the foreground.

Reality of Not-Runnable States:

1. Runnable State:

Processes in this state are ready to run, waiting for their turn to use the CPU. They're waiting for I/O operations to complete or for their scheduled time on the CPU.

2. Sleeping State:

Processes in this state are waiting for an event to occur before they can resume. This event could be I/O completion or a signal from another process.

3. Uninterrupted Sleep (Uninterruptible Sleep) State:

Processes here are similar to sleeping processes but can't be interrupted by signals. They're usually waiting for essential system resources, like disk I/O.

4. Defunct (Zombie) State:

Zombie processes have finished execution but still have an entry in the process table. They exist because their parent process hasn't read their exit status yet.

5. Zombie State:

Zombie processes are essentially defunct processes that haven't been cleaned up. They're dead but still have a process table entry.

Output display

💡
PS command Output Display
  1. USER: The user who started the process.

  2. PID: Process ID, a unique identifier for each running process.

  3. %CPU: The percentage of CPU usage by the process.

  4. %MEM: The percentage of RAM (memory) used by the process.

  5. VSZ: Virtual memory size of the process in kilobytes (KB).

  6. RSS: Resident Set Size, the portion of a process's memory held in RAM, in kilobytes (KB).

  7. TTY: Terminal type associated with the process.

  8. STAT: Process status. Common statuses include:

    • R: Running

    • S: Sleeping

    • Z: Zombie (terminated, but not yet reaped by its parent)

    • D: Uninterruptible sleep (usually waiting for disk I/O)

    • T: Stopped (by job control signal)

  9. START: The start time or date of the process.

  10. TIME: Total CPU time consumed by the process since it started.

  11. COMMAND: The command that started the process.

EXAMPLE:

USER       PID  %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.1  23456  3456 ?        Ss   Sep01   0:03 /usr/sbin/init
john       123  2.5  1.2 789012 123456 pts/0    Sl   Sep01   1:30 /usr/bin/python3 script.py
alice      456  0.0  0.5  65432  5432 pts/1    S    Sep01   0:10 /bin/bash
  • The first process is owned by the root user and has a PID of 1.

  • The second process is owned by the user "john" with a PID of 123. It is using 2.5% of the CPU and 1.2% of the memory.

  • The third process is owned by the user "alice" with a PID of 456.

💡
TOP command output display
  1. PID: Process ID, a unique identifier for each running process.

  2. USER: The user who started the process.

  3. PR: Priority of the process.

  4. NI: Nice value of the process, indicating its priority.

  5. VIRT: Virtual memory used by the process in kilobytes (KB).

  6. RES: Resident Set Size, the portion of a process's memory held in RAM, in kilobytes (KB).

  7. SHR: Shared memory size used by the process in kilobytes (KB).

  8. S: Process status. Common statuses include:

    • R: Running

    • S: Sleeping

    • Z: Zombie (terminated, but not yet reaped by its parent)

    • D: Uninterruptible sleep (usually waiting for disk I/O)

    • T: Stopped (by job control signal)

  9. %CPU: The percentage of CPU usage by the process.

  10. %MEM: The percentage of RAM (memory) used by the process.

  11. TIME+: Total CPU time consumed by the process since it started.

  12. COMMAND: The command that started the process.

top - 15:42:30 up 10 days,  2:32,  2 users,  load average: 0.25, 0.17, 0.12
Tasks: 201 total,   1 running, 200 sleeping,   0 stopped,   0 zombie
%Cpu(s):  2.0 us,  1.5 sy,  0.0 ni, 96.5 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :  7943.6 total,  1455.3 free,  5371.2 used,  1117.1 buff/cache
MiB Swap:  2048.0 total,  1875.9 free,   172.1 used.  2176.8 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
 1234 john      20   0  300000  50000  20000 R   5.0   0.6   0:10.45 process1
 5678 alice     20   0  150000  30000  10000 S   2.5   0.4   0:05.20 process2
 9101 bob       20   0  200000  40000  15000 S   1.2   0.5   0:02.15 process3
  • The top command displays system information at the top, including uptime, number of tasks, CPU usage, memory usage, and swap usage.

  • The processes section lists running processes, including their PID, user, priority, memory usage, CPU usage, and command.

Understanding Linux processes is crucial for efficient system management. By grasping the knowledge of process types, states, and employing essential command-line tools, users can effectively manage, monitor, and optimize their Linux systems. These skills are invaluable for system administrators, ensuring smooth operations and optimal resource utilization in Linux environments.

Did you find this article valuable?

Support Farhan's Scripted Explorations by becoming a sponsor. Any amount is appreciated!