Neural Nets

Linux Process Management

/ 9 min read

Download As PDF

Linux process management is at the heart of how the operating system handles tasks. Processes are the basic units of execution in Linux, and managing them efficiently is vital for system performance, resource allocation, and stability. This guide explores the lifecycle of processes, tools to manage them, advanced techniques, and best practices.

What is a Process in Linux?

A process is a program in execution, consisting of its executable code, runtime environment, and allocated resources such as memory, open files, and CPU time. Each process in Linux is uniquely identified by a Process ID (PID), which the kernel uses to track and manage the process.

Process Categories

1. Foreground Processes

Foreground processes run interactively, tied to the terminal or graphical user interface (GUI). For example:

  • Running a script using bash myscript.sh executes it as a foreground process.
  • A foreground process occupies the terminal and can be interrupted using keyboard shortcuts like Ctrl+C.

2. Background Processes

Background processes run independently of user interaction and are ideal for long-running tasks. They can be initiated by appending an & to the command:

Terminal window
ping google.com > output.txt &

These processes do not block the terminal and can be monitored or controlled using commands like jobs, fg, and bg.

3. Daemon Processes

Daemon processes are system services that run in the background, typically started at boot time. Examples include cron for scheduling tasks and sshd for secure shell access.

The Lifecycle of a Process

The lifecycle of a Linux process involves several distinct stages:

1. Creation

Processes are created using the fork() system call, which duplicates an existing process (the parent). This creates a child process that inherits the parent’s attributes. After creation:

  • The child process can replace its memory space with a new program using exec().
  • The parent process can either wait for the child to complete or continue executing in parallel.

Detailed Steps:

  • fork(): Creates a new process by duplicating the existing process. The new process, known as the child process, is a copy of the parent process.
  • exec(): This family of functions replaces the process’s memory space with a new program. Common variants include execl, execv, execle, execve, execlp, and execvp.
  • clone(): Creates a new process with shared resources, useful for thread creation. It allows more fine-grained control over what is shared between the parent and child processes.

2. Execution and Scheduling

Once created, a process is scheduled for execution. The kernel determines when and for how long a process can use the CPU based on:

  • Priority: Higher-priority processes are executed sooner.
  • Policy: Scheduling policies like SCHED_NORMAL and SCHED_RR define execution behavior.

Scheduling Policies: Linux uses the Completely Fair Scheduler (CFS) to allocate CPU time to processes. CFS aims to provide fair CPU time to all processes, ensuring that no single process monopolizes the CPU. Scheduling policies include:

  • SCHED_OTHER: The default policy for normal processes. It uses a time-sharing approach.
  • SCHED_FIFO: First-in, first-out real-time scheduling. Processes are run until they exit or block.
  • SCHED_RR: Round-robin real-time scheduling. Processes are run in a round-robin manner with a fixed time slice.
  • SCHED_BATCH: For batch processes that are not interactive. It provides a fair share of CPU time but with a longer time slice.
  • SCHED_IDLE: For processes with the lowest priority. They run only when no other processes are runnable.

3. Termination

Processes terminate in one of two ways:

  • Normal Termination: The process completes its task and calls exit().
  • Forced Termination: The process is terminated using signals like SIGKILL or SIGTERM.

Signals: Signals are used for inter-process communication (IPC) and process control. Common signals include:

  • SIGKILL (9): Forcefully terminates a process. It cannot be caught or ignored.
  • SIGTERM (15): Requests a process to terminate gracefully. It can be caught or ignored.
  • SIGSTOP (19): Stops a process. It cannot be caught or ignored.
  • SIGCONT (18): Continues a stopped process.
  • SIGHUP (1): Hangup signal, often used to reload configuration files.
  • SIGINT (2): Interrupt signal, typically sent by pressing Ctrl+C.
  • SIGSEGV (11): Segmentation fault, indicates an invalid memory reference.
  • SIGCHLD (17): Sent to a parent process when a child process terminates.

Process States

A process’s state defines its current activity and is dynamically updated by the kernel. Common states include:

  • Running (R): Actively executing on the CPU.
  • Sleeping (S): Waiting for resources or I/O.
  • Stopped (T): Paused by user action, often via SIGSTOP.
  • Zombie (Z): Completed execution but not yet reaped by its parent process.
  • Orphan: Parent has terminated; adopted by the init process.

Detailed States:

  • Running (R): The process is either executing on the CPU or waiting in the run queue.
  • Interruptible Sleep (S): The process is waiting for an event to complete, such as I/O operations. It can be interrupted by signals.
  • Uninterruptible Sleep (D): The process is waiting for I/O operations that cannot be interrupted. This state is often seen during direct hardware access.
  • Stopped (T): The process has been stopped, typically by a signal like SIGSTOP or SIGTSTP.
  • Zombie (Z): The process has finished execution but still has an entry in the process table. It is waiting for its parent process to read its exit status.
  • Dead: The process has been terminated and removed from the process table.

These states can be viewed using commands like ps or top.

Tools for Process Management

Linux provides a range of utilities to monitor, control, and manage processes effectively.

1. ps (Process Snapshot)

The ps command provides a static snapshot of running processes. Common usage includes:

Terminal window
ps aux
  • a: Shows processes of all users.
  • u: Displays detailed information like CPU usage.
  • x: Includes processes without a controlling terminal.
  • ps -eo pid,ppid,cmd,%mem,%cpu —sort=-%mem: Customizes the output to show PID, PPID, command, memory usage, and CPU usage, sorted by memory usage.

2. top and htop

The top command is a dynamic, real-time tool for monitoring processes. It displays CPU, memory usage, and process activity. Example:

Terminal window
top

Interactive Commands:

  • Press k to kill a process.
  • Press r to renice a process.
  • Press q to quit.

Sorting:

  • Press P to sort by CPU usage.
  • Press M to sort by memory usage.
  • Press N to sort by PID.

For a more user-friendly interface, htop offers enhanced navigation and visualization.

Features of htop:

  • Color-Coded Output: Easy to read and navigate.
  • Interactive Commands:
    • Use arrow keys to navigate.
    • Press F9 to kill a process.
    • Press F7 and F8 to increase or decrease priority.
    • Press F2 to set up custom columns.

3. kill and pkill

The kill command sends signals to processes, with common signals being:

  • SIGTERM (15): Requests a graceful termination.
  • SIGKILL (9): Forcefully terminates a process.

Examples:

Terminal window
kill 1234 # Sends SIGTERM
kill -9 1234 # Sends SIGKILL

Additional Commands:

  • kill -SIGSTOP : Stops a process.
  • kill -SIGCONT : Continues a stopped process.

pkill is similar but targets processes by name:

Terminal window
pkill firefox

4. nice and renice

These commands adjust the priority of a process:

  • nice: Starts a process with a specified priority (default is 0, range is -20 to 19).
  • renice: Modifies the priority of an existing process.

Example:

Terminal window
nice -n 10 long_task.sh
renice -n -5 -p 1234

5. jobs, fg, bg

Used for managing shell jobs:

  • jobs: Lists all background jobs.
  • fg %1: Brings job number 1 to the foreground.
  • bg %1: Moves job 1 to the background.

6. pgrep and pkill

  • pgrep: Searches for processes based on name and other attributes.
    • pgrep -u <process_name>: Searches for processes owned by a specific user.
    • pgrep -f : Searches for processes matching a pattern.
    • pkill: Sends signals to processes based on the same criteria.
    • pkill -9 -u <process_name>: Forcefully terminates processes owned by a specific user.
    • pkill -f : Sends a signal to processes matching a pattern.

Advanced Process Management Techniques

1. Using strace for Debugging

strace traces system calls made by a process. It’s invaluable for debugging:

Terminal window
strace -p 1234

2. Process Resource Limitation

Control resources using:

  • ulimit: Limits resources like memory or open files for processes.
  • cgroups: Allocates CPU, memory, and I/O for grouped processes.

cgroups (Control Groups): Control groups (cgroups) are a Linux kernel feature that limits, accounts for, and isolates the resource usage (CPU, memory, disk I/O, etc.) of a collection of processes. Key features include:

  • Resource Limiting: Prevents processes from consuming more than a specified amount of resources.
    • cgcreate -g cpu:/mygroup: Creates a new cgroup for CPU limiting.
    • cgset -r cpu.shares=512 mygroup: Sets the CPU shares for the cgroup.
    • cgclassify -g cpu:mygroup : Adds a process to the cgroup.
  • Accounting: Tracks resource usage for billing or monitoring purposes.
    • cgget -r memory.usage_in_bytes mygroup: Gets the memory usage of the cgroup.
  • Isolation: Ensures that processes in one cgroup do not affect processes in another cgroup.
    • cgexec -g cpu:mygroup : Runs a command within the cgroup.

3. Scheduling Policies

Processes can follow different scheduling policies:

  • SCHED_NORMAL: Default time-sharing policy.
  • SCHED_FIFO: First-In, First-Out real-time scheduling.
  • SCHED_RR: Real-time round-robin scheduling.

Change scheduling policies using chrt:

Terminal window
chrt -r -p 10 1234

4. Monitoring with systemd

Systemd manages system services and processes:

Terminal window
systemctl status sshd

systemd: systemd is a system and service manager for Linux operating systems. It provides a standard process for controlling what programs run when a Linux system boots up. Key features include:

  • Unit Files: Define services, sockets, devices, mounts, and other system components.
    • /etc/systemd/system/myservice.service: Example unit file for a service.
  • Dependencies: Manage the order in which services are started.
    • Requires=, Wants=, After=, Before=: Directives in unit files to manage dependencies.
  • Journal: Logs system and service events.
    • journalctl -u myservice: Views the logs for a specific service.

5. systemctl Command

The systemctl command is used to manage systemd services. Common operations include:

  • systemctl start <service_name>: Starts a service.
  • systemctl stop <service_name>: Stops a service.
  • systemctl enable <service_name>: Enables a service to start at boot.
  • systemctl disable <service_name>: Disables a service from starting at boot.
  • systemctl status <service_name>: Displays the status of a service.
  • systemctl restart <service_name>: Restarts a service.
  • systemctl reload <service_name>: Reloads the configuration of a service without restarting it.

Best Practices for Linux Process Management

  1. Monitor Regularly: Use tools like top, htop, and ps to regularly monitor system performance and identify resource-intensive processes.
  2. Prioritize Critically: Use nice and renice to prioritize critical processes and ensure they receive adequate CPU time.
  3. Limit Resources: Use cgroups to limit resource usage for non-critical processes and prevent them from affecting system performance.
  4. Automate Management: Use systemd to automate the management of services and ensure they start in the correct order.
  5. Handle Signals Carefully: Use signals to control processes, but be cautious with SIGKILL, as it forcefully terminates processes without allowing them to clean up.
  6. Use Logs for Troubleshooting: Combine strace and log files for effective debugging and troubleshooting.
  7. Leverage Automation: Use cron jobs or systemd timers to automate repetitive tasks.
  8. Optimize Background Jobs: Prioritize CPU-intensive processes with nice to ensure smooth system operation.
  9. Monitor with Tools: Use monitoring tools like Nagios or Zabbix for continuous monitoring and alerting.

Conclusion

Linux process management is a vast topic, critical to the operation of any Linux-based system. From the simple ps and kill commands to advanced tools like strace and cgroups, mastering these utilities empowers administrators and developers to maintain smooth and efficient systems. By understanding the lifecycle, states, and tools available, you can gain fine-grained control over process behavior, ensuring optimal performance and reliability.