Linux: Process Management

syllabus:

1. Basic Concept of Process

2. Basic Command for Process Management

3. Scheduled Tasks

Basic Concept of Process:

1. The difference between process and program

1> Program is a segment of code, its a static concept.

     Program can be stored as resource files in file system.

2> Process is the process of program execution, its a dynamic concept.

     Process has its own lifecycle and only lives in MEM.

There is no one-to-one relationship between program and process.

A single program segment can be used by multiple processes, and a process can execute multiple programs sequentially.

 

2. Relationship between parent process and child process

1> Child process is a process(C) that created by another process(P).

2> In Linux, we use fork to create a process. Fork's mechanism is copy data and stack/heap segment and env that parent process is running.

3> If parent process dies, then all its children process should die. 

 

3. Foreground Process & Background Process

[root@helen ~]# find / -name init

If we execute command above, we have to wait for the finish of this command and cannot do anything else.

We are blocked to this process. This is called foreground process.

1> Foreground Process:

     When we type command in Shell, then OS will create a child process to execute this command.

     At the same time, Shell is waiting for the finish of this command and return.

     This command is asynchronize with Shell and its called foreground process.

     User cannot execute any another command before the finish of previous foreground process.  

[root@helen ~]# find / -name init > log.txt &

If we execute command above, we don't have to wait for the finish of this command and nonblocked to process above.

This is called background process.

2> Background Process:

     When we type a command and then add a "&" at the end of it, Shell will create a child process to execute this command,

     but Shell don't wait for the finish of this command.

     This command runs synchronious with Shell and it's called background process.

     We have to make sure that the background process must be non-interactive process.

     That means the command should not shown prompt for user type in and confirm. 

 

4. Status of Process

1) Ready: The process has acquired resources that need to be executed.

                 But as the CPU is now used by another process, so it has to wait.

2) Wait: The process is waiting for a certain event or a certain resource.

3) Runing: The process has been allocated to CPU, and CPU is now processing it.

 

Basic Command for Process Management:

1) w --> User details

JCPU: Distinguish process by terminal number. The time consumption of all the processes executed in this terminal will displayed here.
PCPU: Time consumption of CPU
WHAT: The command the user is now executing.
load average: The average load of system in past 1, 5, 15 minutes. If the number is less than 0.8, that means the system operates well. 
FROM: Shows the ip the user login to the system. ":0" means user are now at X Window runlevel and open the terminal.
IDLE: The idle time for specific user. This is a timer, whenever the user execute any command, the timer will be reseted to zero.

2) w username --> Specific user's details

3) ps

a: show all the processes of all users.
u: show username and command start time
x: show processes that started without a terminal.
    As every user login, there should be a terminal, but there are a lot of system processes that startup without terminal.
e: show all the processes including that started without a terminal
l : show process details
w: widen the output, we can use multiple w to increase the output display width.
#ps: will only display processes that belong to current user
#ps -u or -l: will display processes details that belong to current user
#ps -el or -aux: will display processes details of all users
#ps -aux --sort pid or --sort uid: will display processes order by pid/uid 

PID: The ID of process
PPID: The ID of parent process
TTY: The terminal of process
STAT: The status of process
          S: Sleeping; 
          D: Uninterruptable Sleeping.
          R: Running
          Z: Zombie
          T: sToped
NI: The priority of process
TIME: The CPU Time consumption of process since it started
COMMAND/CMD: The command name that started this process
USER: Username
%CPU: The percentage of CPU time consumption
%MEM: The precentage of MEM consumption

 ps command is usually used combining with grep command-name or grep user-name

# ps -el mark user as UID
[root@helen ~]# ps -el | grep httpd

# ps -aux mark user as username
[root@helen ~]# ps -aux | grep administrator
# inspect the processes of specific user
[root@helen ~]# ps -u administrator

# inspect the processes of specific user
[root@helen ~]# ps -aux | grep administrator

 4) pstree --> show all the processes as the presentation of tree.

 5) kill --> stop specific processes

1> Why should we kill process?

     1) This process occupys too much CPU time

     2) This process is a foreground process and locked a terminal and other process have to wait for it.

     3) The running time is too long and result is unsatisified. 

     4) Too much out for terminal and file

     5) This process has some errors and cannot exit normally. 

 2> Commands for kill process

# kill PID: kill process marked as PID
# kill -9 PID: force kill process marked as PID
# kill -1 PID: restart process marked as PID
# xkill: kill x window process
# killall: kill all processes
# pgrep service-name: find PID whose service name is service-name
# pkill process-name: kill process whose name is process-name

Example for kill process:

If we want to kill processes that started by apache:

[root@helen ~]# kill -9 3575

# Will kill process whose PID=3575
# If we want to kill all processes started by apache server.
# We can kill process 3567 because it is the parent process for all other processes

[root@helen ~]# kill -9 3567
 
# Then all its children processes will close naturally.

[root@helen ~]# killall httpd
# All the processes marked as httpd will be closed


 6) pgrep & pkill

# As Linux and Unix evolve, there have been changes:

[root@helen ~]# ls /proc
...
...

# The directory is not stored on HD, it is stored in MEM instead.
# We can see a lot of directories whose name are numbers
# each number represents the PID of the running process
# and inside the folder stores the information of the the process

# We can also see a lot of files whose name are alphabets,
# and in that file, stores the useful system information there
[root@helen ~]# cat /proc/cpuinfo
...
...
# Will display cpuinfo in detail
[root@helen ~]# cat /proc/meminfo
....
....
# Will display memory info in detail

[root@helen ~]# cat /proc/partitions
....
....
# Will display hd partition info in detail

As the mechanism provided above, there are a lot of commands started with 'p' which are prettey useful.

[root@helen ~]# pgrep httpd
3681
3683
3684
3685
3686
3687
3688
3689
3690

[root@helen ~]# kill -9 'pgrep httpd'
# force stop all httpd processes

[root@helen ~]# pkill httpd
# force stop all httpd processes

[root@helen ~]# kill -1 'pgrep httpd'
# force restart all httpd processes

7) nice & renice

# Scope of process priority in Linux is [-20, 19]
# The smaller the number is, the higher the priority is.
# The default priority is 0

# nice --> to set the priority of a specific process when the process is starting
# syntax: nice -n command
# set the httpd command priority as -5
[root@helen ~]# nice --5 /etc/rc.d/init.d/httpd start
# set the httpd command priority as 5
[root@helen ~]# nice -5 /etc/rc.d/init.d/httpd start


# renice --> to set the priority of a specific process when the process is running
# syntax: renice n pid
# There is no '-' in renice command
[root@helen ~]# ps -el | grep httpd
[root@helen ~]# renice -30 3739
# Command above will set the priority of process 3739  as -20.
[root@helen ~]# renice -5 3739
# Command above will set the priority of process 3739 as -5

8) nohup

# As convention, when user logout the terminal, all processes started by the user would be stoped.
# Command nohup make processes still running even if user logout.
# And will store all the output information and error information into nohup.out file.
# Syntax: nohup program &
[root@helen ~]# find / -name init* > /root/find/init.log &
# After typing the command above, when we logout the terminal, the process will terminate immediately. It is because although the find process is synchronious with the terminal process, it the child process of terminal process, when the terminal process stops, then all its child processes stops naturally.

[root@helen ~]# nohup find / -name init* > /root/find.init.log &
# After typing the command above, we can logout the terminal, the process will still running until it found all the required information and stored the output to the log file(/root/find.init.log).

[root@helen ~]# nohup find / -name init*
# After typing the command above, we can logout the terminal, the process will still running until it found all the required information and stored the output to the log file(currentPath/nohup.out).
# Using default output file is depreciated.

nohup command is especially useful for time consuming operations like 'find' or making backup.

 

Useful shortcuts for process management:

1) Ctrl + c --> Kill current foreground process immediately.

2) Ctrl + z --> Suspend current foreground process immediately.

3) jobs --> There are two kinds of process that we cannot see the result on terminal.

            --> The first one is background process which taged with &

            --> The second one is process which has been suspended.

            --> We can use command 'jobs' to inspect all the processes which belongs to the categories above.

[root@helen ~]# jobs
[1]+ Stopped        find / -name initddd
[2]- Done           find / -name abcddddd > /test/find.log
[3]+ Running        find / -name init* > /test/find2.log   

 4) fg & bg

          --> As we can see, when we use command jobs, there will be id listed as [1], [2], [3]...

          --> That id is useful when we want to resume a process which has been stopped/suspended.

# fg --> Resume a process, and make it as a foreground process
# bg --> Resume a process, and make it as a background process

# This command will resume process 'find / -name initddd' and make it as foreground process 
[root@helen ~]# fg 1

# This command will make the process 'find / -name init* > /test/find2.log' as foreground process

Q: How can we make a running foreground process as a background process?

A: It seems we have to stop/suspend the process first so that we can have its jobs id, and its status is Stopped.

    Then we can use command "bg jobsid" to resume the process in background. 

            But is there a better way to make it a background process without stopping it?

5) top

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

猜你喜欢

转载自davyjones2010.iteye.com/blog/1972365
今日推荐