Linux Process 101


Linux processes are instances of running applications or programs within the operating system. Every process has its own unique identifier, process ID (PID), and is isolated from other processes. In this tutorial, we will explore how Linux processes work, the different types of processes, and how they are managed.

  1. Process Creation: In Linux, processes are created through the fork system call. The fork system call creates a new process by copying the parent process. The child process starts its execution from the next line of code after the fork call. The parent process continues its execution and both the parent and child processes run simultaneously.

  2. Types of Processes: There are several types of processes in Linux, including:

  • Interactive Processes: These processes are run by users and can interact with the terminal. For example, a terminal shell is an interactive process.

  • Daemon Processes: These processes run in the background and are not associated with a terminal. They are typically used to provide services, such as web servers and database servers.

  • Kernel Processes: These processes run within the kernel and are not directly visible to users. They are used to manage system resources, such as scheduling processes, managing memory, and managing devices.

  1. Process Management: Linux processes are managed by the kernel. The kernel schedules processes for execution and allocates system resources to them. The kernel also manages the process table, which is a list of all processes that are currently running on the system.

  2. Process States: A process can be in one of several states, including:

  • Running: The process is currently executing.
  • Sleeping: The process is waiting for an event, such as a user input or completion of a task.
  • Stopped: The process has been stopped and is not executing.
  • Zombie: The process has completed its execution but its exit status has not been retrieved by its parent process.
  1. Process Scheduling: The Linux kernel uses a process scheduler to manage the execution of processes. The scheduler assigns CPU time to processes and determines which process should run next. The scheduler uses different algorithms, such as the Completely Fair Scheduler (CFS) and the Round Robin Scheduler, to determine the order in which processes are executed.

  2. Process Priority: Each process has a priority value, which determines its priority for execution by the scheduler. A process with a higher priority will be executed before a process with a lower priority. The priority of a process can be changed using the nice and renice commands.

In conclusion, understanding how Linux processes work and how they are managed is an important aspect of Linux administration. With this knowledge, you can optimize system performance, manage processes effectively, and diagnose and troubleshoot problems related to processes.

Usage of PS command

In Linux, when a process is started, it can either run in the foreground or the background. The foreground processes are those that are executing and have control over the terminal, while background processes are those that are executing in the background without having control over the terminal.

A foreground process can be started by simply running the command in the terminal, for example:

ls

This process will run in the foreground and will not return the terminal prompt until the process has completed.

A background process can be started by adding an ampersand & at the end of the command, for example:

$ ls &

This process will run in the background and will return the terminal prompt immediately, allowing the user to continue using the terminal while the process runs. The status of background processes can be checked using the jobs command.

It is important to note that background processes can also be suspended and resumed just like foreground processes. This can be done using the ctrl + Z keyboard combination to suspend the process and the fg command to resume it in the foreground, or the bg command to resume it in the background.

Background processes provide a convenient way to run multiple commands simultaneously in the terminal and can be managed using the jobs, fg, and bg commands.

The ps command in Linux is used to interact with processes and retrieve information about them. Here are some examples of how to use the ps command:

  1. Basic usage: ps To retrieve a list of all processes running on the system, you can simply run the ps command without any options:
$ ps
  PID TTY          TIME CMD
  1234 pts/0    00:00:00 bash
  1235 pts/0    00:00:00 ps

  1. Display all processes: ps aux To display all processes running on the system, including those run by other users, use the ps aux command:
$ ps aux
  USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
  root         1  0.0  0.0  42928  1048 ?        Ss   09:25   0:00 /sbin/init
  root         2  0.0  0.0      0     0 ?        S    09:25   0:00 [kthreadd]
  root         3  0.0  0.0      0     0 ?        S    09:25   0:00 [ksoftirqd/0]
  root         4  0.0  0.0      0     0 ?        S    09:25   0:00 [kworker/0:0]
  ...

  1. Filter processes by user: ps -u USER To display all processes running under a specific user, use the ps -u USER command:
$ ps -u root
  PID TTY          TIME CMD
    1 ?        00:00:00 init
    2 ?        00:00:00 kthreadd
    3 ?        00:00:00 ksoftirqd/0
    4 ?        00:00:00 kworker/0:0
    ...

  1. Show detailed information: ps -ef To show detailed information about processes, including the full command line, use the ps -ef command:
$ ps -ef
  UID        PID  PPID  C STIME TTY          TIME CMD
  root         1     0  0 09:25 ?        00:00:00 /sbin/init
  root         2     0  0 09:25 ?        00:00:00 [kthreadd]
  root         3     2  0 09:25 ?        00:00:00 [ksoftirqd/0]
  root         4     2  0 09:25 ?        00:00:00 [kworker/0:0]
  ...

These are just a few examples of how to use the ps command. For more information and options, you can refer to the man ps manual page.

Zak's AI.Assist

Session only - not saved