What is process management in linux

How Linux Works: Chapter2 Process Management (Part1)

The line (1) is the header line that indicates the meaning of the output in the following lines, and after that, one process is displayed per line. Among them, the COMMAND field represents the command name. We won’t go into detail here, but you can see that the sshd ssh server (PID=19261) started bash (PID=19262) , which then executed ps aux . You can remove the header line of the ps command output by using the —no-header option. Now let’s check the number of processes in my environment.

$ ps aux --no-header | wc -l 216 $ 

There were 216 processes. What are each of these processes doing? How are they managed? In this chapter, we will explain the process management system that Linux uses to manage these processes.

Process Creation

  • a. Divide the processing of the same program into multiple processes (e.g., handling multiple requests by a web server).
  • b. Generate a different program (e.g., creating various programs from bash ).

To achieve these, Linux uses the fork() function and the execve() function 1 . Internally, they call the clone() and execve() system calls, respectively. For the case a, only the fork() function is used, while for the case b, both the fork() function and the execve() function are used.

The fork() function that splits the same process into two

When the fork() function is issued, a copy of the process that issued it is made, and both the original and the copy return from the fork() function. The original process is called the parent process, and the generated process is called the child process. The flow at this time is as follows:

  1. The parent process calls the fork() function.
  2. Allocate memory space for the child process and copy the parent process’s memory into it.
  3. Both the parent and child processes return from the fork() function. Since the return values of the fork() function differ for the parent.

Image description

However, in reality, the memory copy from the parent process to the child process is done at a very low cost thanks to a feature called Copy-on-Write, which will be explained in the following chapter. As a result, the overhead of dividing the processing of the same program into multiple processes in Linux is small.

Читайте также:  Linux форматировать флешку через командную строку

Let’s take a look at how the fork() function generates processes by creating the following python.py program with the following specifications:

#!/usr/bin/python3 import os, sys ret = os.fork() if ret == 0: print("child process: pid=<>, parent process's pid=<>".format(os.getpid(), os.getppid())) exit() elif ret > 0: print("parent process: pid=<>, child process's pid=<>".format(os.getpid(), ret)) exit() sys.exit(1) 
  1. Call the fork() function to branch the process flow.
  2. The parent process outputs its own process ID and the child process’s process ID and then exits. The child process outputs its own process ID and then exits.

In the fork.py program, when returning from the fork() function, the parent process gets the child process’s process ID, while the child process gets 0. Since the pid is always 1 or more, this can be used to branch the processing after the fork() function call in the parent and child processes.

./fork.py parent process: pid=132767, child process's pid=132768 child process: pid=132768, parent peocess's pid=132767 

You can see that a process with process ID 132767 has branched and created a new process with process ID 132768, and that after issuing the fork() function, the processing branches according to the return value of fork() .

The fork() function can be quite difficult to understand at first, but please try to master it by repeatedly reading the content and sample code in this section.

The execve() function for launching a different program

After creating a copy of the process with the fork() function, the execve() function is called on the child process. As a result, the child process is replaced with another program. The flow of processing is as follows:

  1. Call the execve() function.
  2. Read the program specified in the arguments of the execve() function, and read the information required to place the program in memory (called «memory mapping»).
  3. Overwrite the current process’s memory with the new process’s data.
  4. Start executing the process from the first instruction to be executed in the new process (the entry point).

In other words, while the fork() function increases the number of processes, when creating an entirely different program, the number of processes does not increase; instead, one process is replaced with another.

Image description

To express this in a program, it would look like following fork-and-exec.py program. Here, after the fork() function call, the child process is replaced with the echo Hello from command by the execve() function.

#!/usr/bin/python3 import os, sys ret = os.fork() if ret == 0: print("child process: pid=<>, parent process's pid=<>".format(os.getpid(), os.getppid())) os.execve("/bin/echo", ["echo", "hello from pid=<> ".format(os.getpid())], <>) exit() elif ret > 0: print("parent process: pid=<>, child process's pid=<>".format(os.getpid(), ret)) exit() sys.exit(1) 

The result of the execution looks like this:

Читайте также:  Linux доступ через прокси

Источник

What is Process Management in Linux?

Process management in Linux is an essential skill for Linux administrators and developers.

It involves controlling and monitoring the processes running on a Linux system, including managing process resources, scheduling processes to run on the CPU, and terminating processes when necessary. Understanding the different types of processes, their states, and the available commands for process management, such as ps , top, kill, nice, and renice , are important for managing processes effectively.

Introduction to Process

A process is an instance of a program currently running on a computer system. In Linux, processes are managed by the operating system’s kernel, which allocates system resources and schedules processes to run on the CPU. Understanding and managing processes is a critical skill for Linux administrators and developers.

Types of Processes

In Linux, processes can be categorized into two types:

Foreground Processes

Foreground processes are the kinds of processes that require input from the user and are characterized by their interactivity. For instance, a foreground process would be like you are running an Office application on the Linux system.

Background Processes

On the other hand, background processes are non-interactive operations carried out in the background and do not call for any participation from the user. Antivirus software is an example of a Background Process.

Additionally, processes can be system processes or user processes. System processes are initiated by the kernel, while users initiate User processes.

ps ux command on terminal

Process States in Linux

In Linux, a process can be in one of five states:

  • Running:
    The process is currently executing on the CPU.
  • Sleeping:
    The process is waiting for a resource to become available.
  • Stopped:
    The process has been terminated by a user
  • Zombie:
    The process has completed execution but has not yet been cleaned by the system.
  • Orphan:
    The parent process of the current process has been terminated.

top command on the terminal

What is Process Management in Linux?

Process management is the task of controlling and monitoring the processes that are running on a Linux system. It involves managing process resources, scheduling processes to run on the CPU, and terminating processes when required.

Commands for Process Management in Linux

Linux provides several commands for managing processes, which include:

Читайте также:  Kaspersky endpoint security linux debian
Commands Description
ps Displays information about the processes running currently.
top Provides real-time information about system processes and their resource usage.
kill Terminates a process by sending a signal to it.
nice Adjusts the priority of a process.
renice Changes the priority of a running process.
ps PID Shows the state of an exact process.
pidof Shows the Process ID of a process.
df Shows Disk Management of your system.
free Shows the status of your RAM.
bg For sending a running process to the background.
fg For running a stopped process in the foreground.

Practically Managing the Processes

We have explained some common commands below which are required to manage processes. Let’s check them out:

Identifying and Terminating Processes in Linux.

Identification of processes and termination of processes can be done by using the below-mentioned commands in Linux.

The ps command is used to find the process ID (PID) of the process you want to manage. As instance, if it’s required to kill a process you need to know the process ID (PID) of that exact process.

ps command on terminal

The kill command in Linux is used to terminate processes by their process IDs (PIDs).

kill command on the terminal

The killall command in Linux is used to terminate processes by their names. It sends a signal to all processes with a specified name, effectively killing them.

killall command and its options

Managing Priority of Processes in Linux

Managing the priority of a running process is done using “nice” and “renice” commands in Linux.

The nice command in Linux is used to modify the priority of a process. It assigns a lower priority to a process to reduce its resource usage.

The renice command is used to modify the priority of an already running process. It can increase or decrease the priority of a process, depending on the specified value.

Analyzing Resource Usage in Linux.

The top command in Linux is used to display real-time information about processes running on a system, including CPU and memory usage. It provides an interactive interface that allows users to monitor and manage processes.

Conclusion

  • Processes are instances of programs that are currently running on a Linux system.
  • Processes can be categorized into foreground, background, and system and user processes.
  • Processes can be in one of five states: running, sleeping, stopped, zombie, or orphan .
  • Process management involves controlling and monitoring the processes running on a Linux system.
  • Linux provides several commands for managing processes, including ps, top, kill, nice, and renice .
  • Practically managing processes involves using these commands to identify and terminate processes, adjust process priorities, and monitor system resource usage.

Источник

Оцените статью
Adblock
detector