Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

UNIX process management


May 23, 2021 UNIX Getting started


Table of contents


Process management

If a user executes a program on the UNIX operating system, the operating system creates a specific environment for the program to run it. T his environment contains all the resources that the system needs to run the program, making it as if no other programs are running in the system.

If the user enters an instruction in the UNIX operating system, the operating system creates (starts) a corresponding process. F or example, if a user wants to use the ls directive to list files in a directory, a process is started to accomplish this task. S imply said, a process is an example of an executable program.

The operating system tracks a process by a 5-bit ID number, often referred to as a pid or process ID. E ach process in the operating system has a unique pid.

Because all process IDs are recycled, pids are repeated. However, in the operating system, there is no case where two processes have a unified process ID.

Create a process

If a user creates a process (executes an instruction), there are two ways to run it.

  • The fore desk process
  • Background process

The fore desk process

By default, any user-created process executes on the front end. T he process can get input information from the keyboard and can feedback the execution results to the display.

We can use the ls instruction to observe this process. I f the user wants to list all the files in the current directory, the user needs to type the following instructions on the terminal command line:

    $ls ch*.doc

This instruction will show all files with a file name that begins with ch .doc of the file.

    ch01-1.doc   ch010.doc  ch02.docch03-2.doc 
    ch04-1.doc   ch040.doc  ch05.docch06-2.doc
    ch01-2.doc   ch02-1.doc

The process corresponding to the instruction is in the fore desk, the output is displayed directly on the display, and if ls execution requires input, the process waits for input from the keyboard.

When a program is executed in the fore desk, the user cannot execute other instructions (creating other processes) because other processes are prompted that they cannot be created until the current process is executed.

Background process

Background processes can be executed without information entered by the keyboard. I f a background process requires input from peripherals such as a keyboard, it waits.

The advantage of background processes is that users can execute other instructions. U sers do not need to wait for the process to end to execute other processes at this time.

The easiest way to start a background process is to add an identifier at & the instruction.

    $ls ch*.doc &

This instruction also indicates that all file names start with ch and .doc the file.

    ch01-1.doc   ch010.doc  ch02.docch03-2.doc 
    ch04-1.doc   ch040.doc  ch05.docch06-2.doc
    ch01-2.doc   ch02-1.doc

If the ls instruction here wants to get input, it transitions to a stop state until the user takes him to the fore desk and gets input from the keyboard.

The first line shows information about the background process - the job number and the process ID, and the user needs to use the job number to complete the switch between the foreground and the background.

If the user presses the enter key, you can see the following information:

    [1]   +   Done ls ch*.doc &
    $

The first line indicates that the background process of the ls instruction has been successfully executed. T he second line indicates that other instructions can be executed.

Lists the processes that are in the execution state

You can use ps instructions to display processes that are currently executed by the operating system, with the following results:

    $ps
    PID     TTY     TIMECMD   

    18358   ttyp3   00:00:00sh
    18361   ttyp3   00:01:31abiword
    18789   ttyp3   00:00:00ps

When using ps instructions, the -f option is usually selected. This option can display more detailed content.

    $ps -f
    UID      PID  PPID C STIME       TTY     TIME CMD
    amrood   6738 3662 0 10:23:03    pts/6   0:00 first_one
    amrood   6739 3662 0 10:22:54    pts/6   0:00 second_one
    amrood   3662 3657 0 08:10:53    pts/6   0:00 -ksh
    amrood   6892 3662 4 10:51:50    pts/6   0:00 ps -f

Here's an explanation of what's listed in the -f option.

The name of the column Significance
Uid The user ID who executed the process
Pid The process number
PPID The parent process number of the process
C CPU utilization at which the process is located
STIME The process execution time
Tty The terminal type associated with the process
TIME The CPU time used by the process
Cmd The instruction to create the process

Here are the other options for matching ps instructions:

Options Significance
-a Displays information for all users
-x Displays information about processes that do not have terminals
-u Displays additional information similar to -f
-e Show extension information

Stop process execution

Users can stop a process in a variety of ways. T ypically, this can be done by terminal instructions, for example, by pressing the CTRL-C key at the same time to stop the currently executed instructions. T his works only if the program is executed in a fore-of-the-front manner.

If a process is executed in a back-to-back manner, the user first needs to get its job number through the ps instruction, and then the user can kill the process using the kill instruction. As follows:

    $ps -f
    UID  PID  PPID C STIMETTY   TIME CMD
    amrood   6738 3662 0 10:23:03 pts/6 0:00 first_one
    amrood   6739 3662 0 10:22:54 pts/6 0:00 second_one
    amrood   3662 3657 0 08:10:53 pts/6 0:00 -ksh
    amrood   6892 3662 4 10:51:50 pts/6 0:00 ps -f
    $kill 6738
    Terminated

The kill instruction here receives the termination instruction first_one corresponds to. I f a process ignores the regular kill instruction. The user can terminate the instruction using kill -9 followed by the process number, as follows:

    $kill -9 6738
    Terminated

The parent and child processes

Each process in the UNIX system has two ID numbers: process ID (pid) and parent process ID (ppid).

Most instructions executed using shells have their own parent processes. U sing ps -f instruction, you can show the corresponding process ID and its parent process ID for each process.

Zombie processes and orphan processes

Typically, when a child process is killed, its parent process is notified by the SIGCHILD signal. T he parent process then does what is necessary or starts a new child process. H owever, sometimes the parent process is killed before the child process. I n this case, the init process, known as the parent process of all processes, is called the parent process of the child process. T hese sub-processes are also known as orphan processes.

When a process is killed, the ps instruction list shows the process flag bit Z status. I t's a zombie process. T he process is dead and will not be used again. T hese processes are different from orphan processes. They are processes that have completed the task, but still have an entry in the process table.

Daemon

Daemons are operating system-related background processes that are typically executed with root permissions and are requested by other processes.

The daemon does not have a control terminal. I t also does not /dev/tty If the user uses the "ps -ef" instruction to view the tty domain, all daemons are displayed in that domain?

More specifically, a daemon is a process that executes in the background, and it waits for an event to occur, thus matching the event. For example, the printer daemon has been waiting for instructions to print.

If a user's program takes a long time to execute, you can design it as a way for the daemon to start.

Top instruction

Top instructions are instructions that are used to display processes sorted under different conditions.

It is an interactive diagnostic tool that is frequently updated and dynamically displays the following information about the following and related processes: physical memory, virtual memory, CPU utilization, load rate.

Here's a simple example of executing the top directive and looking at CPU usage for different inheritances.

    $top

Job number and process number

Background and blocked processes typically use job numbers for maintenance. The job number is different from the process number.

In addition, a job can contain multiple processes that can be executed serially or in parallel, so it is easier to use the job number than to track a single process.