`
DavyJones2010
  • 浏览: 151825 次
  • 性别: 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
分享到:
评论

相关推荐

    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: ...

    Process Management in Linux

    - **`wakeup_process` 函数**:当某个进程的状态发生变化时,比如从等待状态变为就绪状态,这个函数会被调用来更新调度器的状态。 #### 四、中断处理 中断处理是操作系统中一个极其关键的部分,它负责响应外部事件...

    linux-process-management.zip_linux进程管理

    在Linux操作系统中,进程管理是核心功能之一,它关乎系统的性能和稳定性。本文将深入剖析Linux进程管理,探讨其生命周期,以及与用户进程创建、内存管理、调度和销毁相关的内核机制。 首先,Linux进程的生命周期...

    linux_process_mgt

    slides about linux kernel porcess management.

    3Process Management.docx

    此外,进程间通信(IPC,Inter-Process Communication)也是进程管理的重要部分。在Unix和Linux系统中,进程可以通过多种方式交换信息,如管道(pipe)、套接字(socket)、共享内存、信号量和消息队列等。这些通信...

    linux_ProcessCommunication.rar_j2me

    在Linux操作系统中,进程间通信(IPC,Inter-Process Communication)是系统中多个并发执行的进程之间交换数据的重要机制。本文将深入探讨Linux内核中的几种主要进程间通信方式,并结合J2ME(Java 2 Micro Edition)...

    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操作系统性能调优_了解系统.pdf

    2. 进程管理(Process Management) Linux 操作系统提供了多种进程管理机制,包括 fork()、exec()、wait() 等。fork() 函数用于创建一个新的进程,exec() 函数用于执行一个新的程序,wait() 函数用于等待一个进程的...

    Beginning Linux Programming, 4th Edition

    “Chapter7:Data Management”(第7章:数据管理)讲解了数据库编程的基础知识,主要以MySQL为例子,展示了如何在Linux系统中进行数据库操作。 “Chapter8:MySQL”(第8章:MySQL)则深入讲述了MySQL数据库的安装、...

    Linux程序设计 第四版 英文版 经典linux书籍 819页 pdf Beginning Linux Programming

    13. **第十三章:Inter-Process Communication: Pipes(进程间通信:管道)** - **主要内容**:介绍管道通信的基本原理和应用场景。 - **知识点**: - 管道的工作机制 - 命令行管道的使用 - 命名管道(FIFO) ...

    linux 0.01源代码

    - **进程管理(Process Management)**:Linux 0.01包含了基本的进程创建、调度和通信功能。进程是操作系统中并发执行的程序实例,而调度则是决定哪个进程应该获得CPU的时间片。 - **内存管理(Memory Management)...

    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