What is a Process in Linux/Unix?

Topics Covered

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.

Types of Processes in Linux

There are various types of processes in Linux:

1. Parent and Child Process:

A child process is a Linux process that is created by another process known as the parent process. A child process can be created using one of two methods. The first method is the fork system call, which is often used in Unix-like operating systems, whereas the second method is the spawn method, which is preferred in the modern NT kernel of Microsoft Windows. A process is considered a parent process if it creates one or more child (subprocess) processes.

2. Zombie and Orphan process:

A zombie process is a Linux process that has finished its execution but still has an entry in the process table. Zombie processes usually get created when a child process completes its execution but the parent process has yet to read its exit status.

An orphan process is a process that continues to run even after the parent process has been completed or terminated. An orphan process can be intentionally or unintentionally created.

3. Daemon process:

Daemon processes are system-related background processes. These processes often run the permissions of root and service requests from other processes. Daemon processes often run in the background, waiting for specified events or tasks to occur. Daemon processes, unlike regular processes, do not require a controlling terminal to operate.

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:

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

    Syntax:

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

    Syntax:

  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:

    To check the status of a single Linux process.

  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:

  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:

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

    Syntax:

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

How to Initialize a Linux Process?

There are two ways to run a process:

Method - 1: Foreground processes:

When a user starts a Linux process, it runs in the foreground by default. The foreground process accepts command-line input and outputs it to the computer screen.

Example:

using-foreground-processes-to-initialize-a-linux-process

When a command or process runs in the foreground and takes a long time, it prevents the start or execution of other processes. This is because the command prompt will not be available until the currently running program completes its processing and comes out.

Method - 2: Background processes:

These processes are non-interactive and do not require keyboard input. While one process is running in the background, it is possible to start another process from the terminal.

To start a background Linux process, add an ampersand (&) to the end of the command.

Example:

Since the pwd command doesn’t want any keyboard input, it enters the stop state until it moves to the foreground and provides data input. As a result, when you hit Enter:

using-background-processes-to-initialize-a-linux-process

How to Track Ongoing Process in Linux?

Listing Running Processes:

In Linux, We can use the ps command to display the list of the running processes.

how-to-track-ongoing-process-in-linux

The -f (f for full) option is one of the most commonly used options for the ps command, and it offers extra information, as shown in the following example.

how-to-track-ongoing-process-in-linux-1

The following is a description of all the fields displayed by the ps -f command:

  • UID:
    The user ID of the process owner.
  • PID:
    The process ID (PID) is a unique identifier assigned to each active process.
  • PPID:
    The parent process ID, or the ID of the process that started the current process.
  • C:
    The CPU utilization of the process.
  • STIME:
    The start time of the process.
  • TTY:
    The terminal associated with the process.
  • TIME:
    The CPU time consumed by the process.

There are other options available when using the ps command:

OptionsDescription
-aThis option is used to show information about all users.
-xThis option is used to show information about processes without terminals.
-uThis option is used for additional information like the -f option.
-eThis option is used to display extended information.

Stopping a Process:

If a command is running in the foreground, you can stop it by hitting Ctrl + C. However, if a process is running in the background, you can get its Job ID using the ps command. Once you have the Job ID, you can kill the process using the kill command.

how-to-track-ongoing-process-in-linux-2

In the above example, the kill command terminates the process with Job ID 2915. If the process ignores the regular kill command, use kill -9 followed by the process ID as shown below.

how-to-track-ongoing-process-in-linux-3

In the preceding example, we used the pgrep command to get the process ID (PID) of the Firefox browser. After getting the process ID, we kill it with the kill -9 11258 command.

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.
  • Various commands such as bg, fg, top, df, and free provide extra functionality for controlling and monitoring processes, such as resuming suspended jobs, changing process priorities, checking disk space usage, and viewing memory statistics.
  • In Linux, the ps command provides a list of currently running processes. One of the most often used ps command options is -f, which displays additional process information.
  • If a Linux process ignores the regular kill command, use kill -9 followed by the process ID.