`
DavyJones2010
  • 浏览: 148005 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 大小: 39.7 KB
  • 大小: 38.1 KB
  • 大小: 57.2 KB
  • 大小: 110.2 KB
分享到:
评论

相关推荐

    Process Management in Linux

    Process Management in Linux Process Management in Linux

    linux kernel -from I/O ports to process management

    依然是英文资料,有兴趣的可以考虑

    Understanding.Operating.Systems.7th.Edition.128509655X

    Ch 2: Memory Management: Simple Systems Ch 3: Memory Management: Virtual Memory Systems Ch 4: Processor Management Ch 5: Process Management Ch 6: Concurrent Processes Ch 7: Device Management Ch 8: ...

    linux-process-management.zip_linux进程管理

    Linux 进程管理剖析。在本文中,了解 Linux 进程的生命周期,探索用户进程创建、内存管理、调度和销毁的内核内幕。

    linux_process_mgt

    slides about linux kernel porcess management.

    Principles.of.Operating.Systems.0198082878

    Case Study II: Process Management in UNIX/Solaris/Linux/Windows PART III Memory Management Chapter 10. Basic Memory Management Chapter 11. Virtual Memory Case Study III: Memory Management in UNIX/...

    Wrox.Professional.Linux.Kernel.Architecture.

    Professional Linux® Kernel Architecture Introduction xxvii Chapter 1: Introduction and Overview 1 Chapter 2: ProcessManagement and Scheduling . 35 Chapter 3: Memory Management 133 Chapter 4: ...

    3Process Management.docx

    This chapter introduces the... It defines the process, as well as related concepts such as threads, and then discusses how the Linux kernel manages each process: how they are enumerated within the kernel

    Beginning.the.Linux.Command.Line.2nd.Edition.14302683

    Chapter 9: Process and System Management Chapter 10: System Logging Chapter 11: Configuring the Network Chapter 12: Configuring a File Server Chapter 13: Working with the Kernel Chapter 14: ...

    The Linux Command Line: A Complete Introduction, 2nd Edition(Linux命令行大全,第二版)

    Administer your system, including networking, package installation, and process management Use standard input and output, redirection, and pipelines Edit files with Vi, the world's most popular text ...

    Linux系统编程

    This chapter continues the treatment with a discussion of advanced process management, including real-time processes. Chapter 7, Threading This chapter discusses threads and multithreaded programming....

    Linux System Programming

    and the family of system calls for basic process management, including the venerable fork. Chapter 6, Advanced Process Management This chapter continues the treatment with a discussion of advanced ...

    linux kernel development 3rd edition

    Specific topics covered include process management, scheduling, time management and timers, the system call interface, memory addressing, memory management, the page cache, the VFS, kernel ...

    The Linux Kernel Primer A Top-Down Approach for x86 and PowerPC Architectures

    It then follows up with an in depth analysis of the major subsystems including process management, memory management, scheduling, I/O, and filesystems. This book also provides information necessary ...

    Linux Kernel Development, 3rd Edition.pdf

    Specific topics covered include process management, scheduling, time management and timers, the system call interface, memory addressing, memory management, the page cache, the VFS, kernel ...

    the linux command line

    and process management * Use standard input and output, redirection, and pipelines * Edit files with Vi, the world's most popular text editor * Write shell scripts to automate common or boring tasks ...

    Linux Bible 9th

    With a focus on RHEL 7, this practical guide gets you up to speed quickly on the new enhancements for enterprise-quality file systems, the new boot process and services management, firewalld, and the...

    GNU Linux Application Programming

    <br/>Focuses on GNU/ Linux, not only the Linux APIs, but the GNU tools and libraries that make Linux programming possible Covers a variety of useful APIs for process management, shared ...

    Linux System Programming, 2nd edition.pdf

    The family of system calls for basic process management Advanced process management, including real-time processes Thread concepts, multithreaded programming, and Pthreads File and directory ...

    Systems Programming in Unix/Linux 1st Edition

    Covering all the essential components of Unix/Linux, including process management, concurrent programming, timer and time service, file systems and network programming, this textbook emphasizes ...

Global site tag (gtag.js) - Google Analytics