Linux 进程及作业管理

时间:2016-09-09 18:52:23   收藏:0   阅读:939

Linux 进程及作业管理



概述:


一、进程的相关概念:


1.相关定义:



2.进程创建  


3.进程优先级


        100-139:静态优先级,使用nice值来调整(数字越小,优先级越高);
       


4.进程内存  



5.IPC: 进程间通信 Inter Process Communication 



6.Linux内核抢占式多任务


7.进程类型:     


根据进程与终端的关系可以分为:

8.进程状态:


进程被内核调度的过程中的状态可以分为:

     可中断:interruptable
     不可中断:uninterruptable,
         
通常是指被IO阻塞的过程,等待IO满足之前无法继续运行。


9.进程的分类:


10.进程与线程的关系:


参考资料:《Linux内核设计与实现》《深入理解Linux内核》




二、进程的系统管理工具


   CentOS 6:upstart

   CentOS 7:systemd
   
   /sbin/init

1.pstree命令

   pstree-display a tree of processes(显示查看进程树)

   以下为CentOS 6 和 CentOS 7 的区别

[root@CentOS6 ~]# pstree
init─┬─abrtd           #可以看到都是有init生成的
     ├─acpid
     ├─atd
     ├─auditd───{auditd}
     ├─automount───4*[{automount}]
     ├─bonobo-activati───{bonobo-activat}
     ├─clock-applet
     ├─console-kit-dae───63*[{console-kit-da}]
     ├─crond
     ├─cupsd
     ├─2*[dbus-daemon]
     ├─2*[dbus-launch]
     ├─devkit-power-da
     ├─dhclient
     ├─gconf-im-settin
     ├─gconfd-2
[root@centos7 ~]# pstree
systemd─┬─NetworkManager─┬─2*[dhclient]
        │                └─2*[{NetworkManager}]
        ├─2*[abrt-watch-log]
        ├─abrtd
        ├─alsactl
        ├─atd
        ├─auditd─┬─audispd─┬─sedispatch
        │        │         └─{audispd}
        │        └─{auditd}
        ├─chronyd
        ├─crond
        ├─cupsd
        ├─dbus-daemon
        ├─gssproxy───5*[{gssproxy}]
        ├─login───bash
        ├─lsmd
        ├─lvmetad
        ├─master─┬─pickup
        │        └─qmgr
        ├─polkitd───5*[{polkitd}]

2./proc:内核中的状态信息

   Linux系统各进程的相关信息均保存在/proc/PID目录下的各文件中   



技术分享


2.启动进程的方式:

   系统启动过程中自动启动:与终端无关的进程;

      用户通过终端启动:与终端相关的进程




三、显示系统当前进程的运行情况

ps: process state 

     ps-report a snapshot of the current processes  


1.使用ps来查看进程信息

      f 显示进程的父进程;
      o属性… 选项显示定制的信息


示例:如下

[root@CentOS6 1]# ps a
   PID TTY      STAT   TIME COMMAND
  2024 tty2     Ss+    0:00 /sbin/mingetty /dev/tty2
  2026 tty3     Ss+    0:00 /sbin/mingetty /dev/tty3
  2028 tty4     Ss+    0:00 /sbin/mingetty /dev/tty4
  2030 tty5     Ss+    0:00 /sbin/mingetty /dev/tty5
  2032 tty6     Ss+    0:00 /sbin/mingetty /dev/tty6
  2105 tty1     Ss+    0:00 -bash
  3922 pts/1    Ss+    0:00 /bin/bash
  4864 pts/2    Ss     0:00 -bash
  5142 pts/2    R+     0:00 ps a
[root@CentOS6 1]# ps
   PID TTY          TIME CMD
  4864 pts/2    00:00:00 bash
  5144 pts/2    00:00:00 ps
[root@CentOS6 1]# ps aux
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.0  0.1  19344  1632 ?        Ss   08:27   0:01 /sbin/init
root          2  0.0  0.0      0     0 ?        S    08:27   0:00 [kthreadd]
root          3  0.0  0.0      0     0 ?        S    08:27   0:00 [migration/0]
root          4  0.0  0.0      0     0 ?        S    08:27   0:00 [ksoftirqd/0]
root          5  0.0  0.0      0     0 ?        S    08:27   0:00 [stopper/0]
root          6  0.0  0.0      0     0 ?        S    08:27   0:00 [watchdog/0]
root          7  0.0  0.0      0     0 ?        S    08:27   0:18 [events/0]
root          8  0.0  0.0      0     0 ?        S    08:27   0:00 [events/0]
root          9  0.0  0.0      0     0 ?        S    08:27   0:00 [events_long/0]
root         10  0.0  0.0      0     0 ?        S    08:27   0:00 [events_power_ef]
root         11  0.0  0.0      0     0 ?        S    08:27   0:00 [cgroup]
root         12  0.0  0.0      0     0 ?        S    08:27   0:00 [khelper]

2.常用组合之一:aux

VSZ: Virtual memory SiZe,虚拟内存集,线性内存
RSS: ReSidentSize, 常驻内存集
STAT:进程状态
    R:running
    S: interruptable sleeping 可中断睡眠
    D: uninterruptable sleeping 不可中断睡眠
    T: stopped 停止状态
    Z: zombie  僵死态
    +: 前台进程
    l: 多线程进程
    N:低优先级进程
    <: 高优先级进程
    s: session leader,会话(子进程)发起者

3.常用组合之二:-ef

   -e: 显示所有进程

    -f: 显示完整格式程序信息

[root@CentOS6 1]# ps -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 08:27 ?        00:00:01 /sbin/init
root          2      0  0 08:27 ?        00:00:00 [kthreadd]
root          3      2  0 08:27 ?        00:00:00 [migration/0]
root          4      2  0 08:27 ?        00:00:00 [ksoftirqd/0]
root          5      2  0 08:27 ?        00:00:00 [stopper/0]
root          6      2  0 08:27 ?        00:00:00 [watchdog/0]
root          7      2  0 08:27 ?        00:00:20 [events/0]
root          8      2  0 08:27 ?        00:00:00 [events/0]
root          9      2  0 08:27 ?        00:00:00 [events_long/0]
root         10      2  0 08:27 ?        00:00:00 [events_power_ef]
root         11      2  0 08:27 ?        00:00:00 [cgroup]
root         12      2  0 08:27 ?        00:00:00 [khelper]
root         13      2  0 08:27 ?        00:00:00 [netns]
root         14      2  0 08:27 ?        00:00:00 [async/mgr]


4.常用组合之三:-eFH

    -F: 显示更完整格式的进程信息

            C:cpu utillization

            PSR:运行于哪颗cpu之上

    -H: 以进程层级格式显示进程相关信息

[root@CentOS6 1]# ps -eFH
UID         PID   PPID  C    SZ   RSS PSR STIME TTY          TIME CMD
root          2      0  0     0     0   0 08:27 ?        00:00:00 [kthreadd]
root          3      2  0     0     0   0 08:27 ?        00:00:00   [migration/0]
root          1      0  0  4836  1632   0 08:27 ?        00:00:01 /sbin/init
root        581      1  0  2709   952   0 08:27 ?        00:00:00   /sbin/udevd -d
root       2033    581  0  2708   940   0 08:27 ?        00:00:00     /sbin/udevd -d
root       2034    581  0  2708   920   0 08:27 ?        00:00:00     /sbin/udevd -d
root       1525      1  0  6899   868   0 08:27 ?        00:00:00   auditd
root       1559      1  0 62288  1696   0 08:27 ?        00:00:00   /sbin/rsyslogd -i /var/run/syslogd.pid -c 5
rpc        1610      1  0  4745   892   0 08:27 ?        00:00:00   rpcbind
dbus       1630      1  0  5559  1700   0 08:27 ?        00:00:00   dbus-daemon --system
rpcuser    1652      1  0  5838  1380   0 08:27 ?        00:00:00   rpc.statd
root       1687      1  0 47243  3360   0 08:27 ?        00:00:00   cupsd -C /etc/cups/cupsd.conf


常用组合之四 :-eo axo

 o field1,field2...:自定义要显示的字段,以逗号分隔;

 常用的字段:pid,ni,pri,pcpu,stat,comm,tty,ppid

      ni: nice值

      pri: priority,优先级

      psr: processor, CPU编号

      rtprio: 实时优先级

[root@CentOS6 1]# ps -eo pid,tty,ni,comm
   PID TT        NI COMMAND
     1 ?          0 init
     2 ?          0 kthreadd
     3 ?          - migration/0
     4 ?          0 ksoftirqd/0
     5 ?          - stopper/0
     6 ?          - watchdog/0
     7 ?          0 events/0
     8 ?          0 events/0
     9 ?          0 events_long/0
    10 ?          0 events_power_ef
    11 ?          0 cgroup



    







评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!