The Linux Newbie Guide  ⇒    Fundamentals     Advanced     Supplement   Command Index   ENG⇒中
All rights reserved, please indicate the source when citing
 

 Process

1.0 Introduction to Processes & Jobs
1.1 job
        job control
        Foreground Program
        Terminating a Program <CTRL+C>
        Pausing a Program <CTRL+Z>
        Background Execution with &
        jobs : Display Background Jobs
            Managing Jobs
        bg : Resuming Suspended Jobs in the Background
        fg : Bringing Jobs to the Foreground
        kill : Terminate Process
            killall : Massacre Processes
1.2 process
        PID (Process ID)
        Parent Process and PPID
        Child Processes
            pstree : Display Process Relationship Tree
        Process Group & PGID
            Process Group Leader
        Session and SID
            session leader
        init : Ancestral Process
        Zombie Processes
        fork and exec
            exec
        Daemon
1.3 Process Monitoring
        ps : Display Process Status
        top : Process and Performance Monitoring
        nice : Adjust Process Priority
            renice
1.4 Named Pipe (FIFO)
         mkfifo
1.5 Process Substitution

ENG⇒中ENG⇒中
  1.0 Introduction to Processes & Jobs

In our daily lives, our thinking is multitasking, but many tasks often go unnoticed. For instance, I might walk while listening to music and using my smartphone, which involves three concurrent tasks. While it seems I can consciously control all these tasks – like pausing the smartphone while crossing the street – there are underlying processes that I cannot control. When a sudden stimulus, such as an attractive person, triggers physiological responses like a racing heartbeat or rapid breathing, I can't entirely govern these reactions. While self-discipline might help restrain some responses, certain background processes like digestion and blood circulation remain beyond my control.

Contemporary operating systems, much like humans, possess multitasking capabilities. They can run numerous applications simultaneously, and each active program at a given time is a "process." Operating systems, similar to human control, can partially or completely govern some processes, while others are beyond control.

Just like human thoughts, Linux processes never truly halt; there's a constant stream of processes, large and small, running at all times. In the realm of multitasking operating systems (such as Linux/Windows), processes theoretically can't be entirely shut down – there's always some process running.

Generally defined, any running (unfinished) program, including system daemons or user-entered commands, qualifies as a process. Processes generated from commands within a user's logged-in shell are referred to as "jobs." Barring shutdown or a sleep state like Suspend to RAM, processes cannot come to a complete halt.



^ back on top ^



1.1 Job

Processes generated from commands within a user's logged-in shell are referred to as "jobs."

Let's take the familiar example of Windows: The screenshot below shows the input of "taskmgr" in the Windows text interface, which executes the "Task Manager." Under the "Applications" column, the user's jobs are listed.

Windows Task Manager:



windows Task Manager



^ back on top ^



  1.2 process

During the runtime of Unix/Linux, any program that is currently executing and utilizing system resources, including user jobs and system daemon programs, is referred to as a process.

Given that Unix-like systems are multi-user and multi-tasking environments, processes are more intricate compared to jobs. There are specific terms related to processes that should be understood.







^ back on top ^



^ back on top ^


1.3 Process Monitoring
Linux provides several convenient tools for observing running processes, with two common ones being ps and top.

ps : Display Process Status
The ps command is commonly used to view information about processes. It can be used in combination with other commands, such as kill, to manage processes. However, the ps command has many options due to compatibility with various Unix-like systems. Here are some commonly used options for Linux:

Syntax:ps [option][-option]
Command name/function/command user option function
ps/
process status/
Any
-a List all processes associated with terminals (tty) except session leaders and terminal processes.
a List all processes associated with terminals (tty) or jobs, when used with the "-x" option it lists all processes, including daemons.
-A List all processes. Similar to ps ax, but without the "STAT" column.
-f List full-format information about processes. Similar to "-a", but with additional columns like "UID","STIME", etc.
f Display process hierarchy using a tree-like format. Only shows processes from the same session ID as the login user.
o FORMAT Specify the output format with specific fields, such as "pid","ppid","pgid","sid","tty","time",、"nice","comm" and more.
-t [N] Display processes associated with a specific terminal (tty number "N").
-u [USER] Display processes for a specific user.
-x but only displays processes associated with the controlling terminal. Often used with -a to show all processes.

When used without any options, ps output is similar to jobs and displays processes associated with the same session ID as the user.

Example:
$ ps
PID TTY          TIME CMD
3627 pts/0    00:00:00 bash
3656 pts/0    00:00:00 vim
3657 pts/0    00:00:00 ps

The ps command is used to display information about processes. In the above example, it shows the Process ID (PID), Terminal (TTY), Time, and Command (CMD) of the running processes.

Example:

$ ps aux | grep 'Z'
USER    PID %CPU %MEM    VSZ  RSS TTY    STAT START   TIME COMMAND
chi    3458  0.0  0.0      0    0 ?      Z    00:46   0:00 [xinitrc] <defunct>
chi    3459  0.0  0.0      0    0 ?      Z    00:46   0:00 [xinitrc] <defunct>
chi    3691  0.0  0.0      0    0 pts/0  Z    00:59   0:00 [myprog] <defunct>

The ps aux command displays detailed information about all processes. In the example above, it is used in combination with grep to find zombie processes. Zombie processes are indicated by the "STAT" column showing "Z" (defunct).

Example:
$ ps -o pid,ppid,pgid,nice,tty,comm
PID  PPID  PGID  NI TT       COMMAND
3627  3624  3627   0 pts/0    bash
3656  3627  3656   0 pts/0    vim
3727  3627  3727   0 pts/0    ps

The ps -o command is used to customize the output format. In the above example, it specifies the columns to display, including PID, Parent PID (PPID), Process Group ID (PGID), Nice value (NI), Terminal (TTY), and Command (COMM).

Other commonly used options include:
$ ps -t 1 ←Display processes on tty1
$ ps f -u obama ←Display process tree for user "obama"

^ back on top ^

top : Process and Performance Monitoring
top is a commonly used process and performance monitoring tool in Linux, similar to Windows Task Manager. It provides real-time information about various processes' resource usage in the system.

By default, when you run top, it will continuously update real-time information every 5 seconds (with the most CPU-intensive process at the top for easy identification) until you press q to exit. top is an interactive tool, and you can perform actions such as renicing or killing processes using r or k, toggling task/CPU information with t, toggling memory information with m, and more. For a complete list of key commands, you can press h.

$ top
top - 00:42:30 up 36 days, 1:08,  2 users,  load average: 0.16, 0.02, 0.01
Tasks: 130 total,   4 running, 125 sleeping,   1 stopped,   0 zombie
Cpu(s):  0.2%us,  2.6%sy,  0.0%ni, 97.2%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:    515156k total,   438756k used,    76400k free,    11520k buffers
Swap:  1020088k total,        0k used,  1020088k free,   253064k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND             
 2295 root      20   0 32496  12m 5936 S  1.2  2.4   0:15.29 X
2571 aaa       20   0 64484  19m  10m S  0.9  3.9   0:04.20 gnome-terminal
2113 root      20   0  3388 1148 1012 S  0.2  0.2   0:00.20 hald-addon-inpu
2414 aaa       20   0 31500 3616 2940 S  0.2  0.7   0:01.01 exe
... (more processes)

When running the top command, in addition to displaying process information, the first five lines also provide crucial information about system performance:

Example:
$ top -p 61,162 ←Displays processes with PID 61 and 162 onlys

Common options for the top command include "-b" for batch mode (non-interactive), "-d" to specify the refresh interval in seconds, "-n" to set the number of refresh cycles, and "-o" to define sorting criteria (e.g., by CPU or RAM usage).

Example:
$ top -b -o +%CPU | head ←Displays the top 3 CPU-intensive processes
$ top -b -o +%MEM | head 17 > file ←Stores the top 10 memory-intensive processes in a file named "file"

For more detailed top operations, please refer to the link.

^ back on top ^

nice : Adjust Process Priorit
The term "nice" represents friendliness, but what does it have to do with processes? Well, when you lower the priority (niceness value) of a process, other processes get a higher share of the CPU resources relative to it. In this way, you make other processes "nicer" by allowing them more CPU usage.

The nice command can be used to set the priority (niceness value) of a process, ranging from -20 to 19, where lower values indicate higher priority. Only the root user can set negative values. If no specific niceness value is assigned using nice, it defaults to 0. Additionally, the parent process's priority is inherited by its child processes.

To view the current niceness value, you can use the nice command without any options. You can also observe the niceness value of processes using commands like ps and top.

Example:
$ nice ←Display the current niceness value
0 ←Current value is 0
$ sleep 1000 & ←reate a background process and observe its niceness value
[1] 2733
$ ps -o pid,ni,comm ←Use "ps" to check the niceness values
PID NI COMMAND ←"NI" is nice priority value
2578 0 bash
2632 0 sleep
2634 0 ps

To set the niceness value of a process, use the syntax: nice -n # COMMAND (# represents the niceness value, ranging from -20 to 19). However, only the root user can set values smaller than 0.

Example:
$ seq 90000000000 > /dev/null & ← Run two processes in the background that generate a huge number sequence
$ seq 90000000000 > /dev/null &
$ time tar -Jcvf file.txz /etc &> /dev/null ← Run a tarball compression process in the background and measure its execution time

real 0m27.590s ← Took 27.59 seconds
user 0m21.163s
sys 0m0.187s
$ time nice -n 5 tar -Jcvf file.txz /etc &> /dev/null ←ncrease the niceness value to 5 and measure the execution time
real 1m24.405s ← Took 1 minute and 24.405 seconds
user 0m21.529s
sys 0m0.211s

In the above example, increasing the niceness value (lowering the priority) resulted in a longer execution time. However, this also resulted in more CPU time being allocated to other processes (such as the seq command, which completed its action faster).



^ back on top ^


1.4 Named Pipe (FIFO)
In addition to using signals for simple communication between processes, more complex data transfer can be achieved using "named pipes," also known as FIFOs (First In First Out). Named pipes allow processes to communicate, and their advantage lies in the ability to transfer information without the need for intermediate media like disks. This not only speeds up communication but also saves disk space.

mkfifo
A named pipe, similar to a pipe in plumbing, works on the principle of First In First Out (FIFO) data transfer. To create a FIFO data type, the command mkfifo (Make FIFO) is used.

Example:
$ mkfifo MyPipe ←Create a named pipe called "MyPipe"
$ ls -l MyPipe ←Verify using ls
prw-rw-r-- 1 aaa aaa 0 2015-03-20 15:23 MyPipe ←The leading 'p' indicates a named pipe
$ file MyPipe ←Verify using file
MyPipe: fifo (named pipe)

In Unix-like systems, the philosophy is that "everything is a file," and this includes the FIFO data type. However, it's important to note that the size of a FIFO file is always 0, as it doesn't occupy physical disk space.

How can we use this FIFO "file"? The following example demonstrates a simple use case, using a named pipe to transform numbers into letters.

Example (continued):
$ tr '0-9' 'a-j' < MyPipe > out.txt & ←Transform numbers to letters using the named pipe
[1] 3031
$ seq 0 10 > MyPipe ←Write numbers to the named pipe
[1]+  Done                    tr '0-9' 'a-j' < MyPipe > out.txt
$ cat out.txt ←Verify the output in "out.txt"
a
b
c
d
e
. . .

This example illustrates communication between different processes using a FIFO data stream, without the need for intermediate media.

Another practical example involves using named pipes as a medium for transferring compressed data:

Example (continued):
$ bzip2 -c < MyPipe > out.bz2 & ←Compress using bzip2 and write to "out.bz2"
[1] 3088
$ seq 1 1000 > MyPipe ←Write numbers to the named pipe
$ bzcat out.bz2 ←Verify the decompressed outpu
1
2
3
. . .

Named pipes can be manipulated similarly to regular files and can be removed using the rm command.

Example (continued):
$ rm MyPipe

The mkfifo command can also be used to set permissions for the named pipe file, as shown in the following example:

Example:
$ mkfifo --mode=0666 /tmp/name_pipe



^ back on top ^


1.5 process substitution

"Process substitution" is a technique that allows you to treat the output/input of different processes as if they were files. This is particularly useful when you want to compare or manipulate the output of commands in a file-like manner. One common use case is using process substitution with the diff command to compare the results of two commands, where regular file comparison wouldn't work.

The syntax for process substitution is "< (COMMAND)", and it creates a temporary named pipe to pass the output of "COMMAND" to another command as if it were a file.Note that there should be no space between "<" and "(".

Here's an example that demonstrates process substitution by comparing the uppercase versions of two files using the diff command:

Example:
$ diff <(tr a-z A-Z < file1) <(tr a-z A-Z < file2) ←Compare the uppercase versions of file1 and file2

Without process substitution, the above comparison would require multiple commands:
$ cat file1 | tr a-z A-Z > file1.tmp
$ cat file2 | tr a-z A-Z | diff - file1.tmp
$ rm file1.tmp

In reality, process substitution uses named pipes behind the scenes to facilitate communication between different processes.

Example:
$ ls -gGl <(echo) ←Creates a named pipe (/dev/fd/63) for process substitution
lr-x------ 1 64 2015-04-10 22:54 /dev/fd/63 -> pipe:[15358]
$ file <(true)
/dev/fd/63: broken symbolic link to `pipe:[15478]'


In this example, the process substitution creates a named pipe ("/dev/fd/63") in the background.

Here's a more concise example to illustrate the concept:

Example:
$ diff file1 <(uniq file2) ←Compare the contents of file1 and the unique lines of file2

In this example, process substitution is broken down into the following steps:
  1. Create a named pipe (/dev/fd/63).[Note 1.5]
  2. Run diff file1 /dev/fd/63 & in the background.
  3. Execute uniq file2 > /dev/fd/63
  4. Delete the named pipe ("/dev/fd/63") after processing is complete.

^ back on top ^





[Note1.3]:Reference Source: http://unix.stackexchange.com/questions/18918/in-linux-top-command-what-are-us-sy-ni-id-wa-hi-si-and-st-for-cpu-usage

[Note1.5]Reference Source: Advanced Bash-Scripting Guide: http://tldp.org/LDP/abs/html/process-sub.html#FTN.AEN18244
&
wikipedia
: http://en.wikipedia.org/wiki/True_and_false_(commands)