Linux程序设计笔记(第11章 进程和信号)

来源:互联网 发布:c语言双引号作用 编辑:程序博客网 时间:2024/05/18 03:45

*  进程相关命令

ps:process status,报告程序状况

参数:

   ......

    a  显示现行终端机下的所有程序,包括其他用户的程序。 

   -e   显示所有程序。 

   -f   显示UID,PPIP,C与STIME栏位。

   -l或l 采用详细的格式来显示程序状况。

   -H   显示树状结构,表示程序间的相互关系。

    x  显示所有程序,不以终端机来区分。

   ......

lincoln@ubuntu:~$ ps -eflHF S UID        PID  PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD1 S root         2     0  0  80   0 -     0 kthrea 09:33 ?        00:00:00 [kthreadd]1 S root         3     2  0 -40   - -     0 migrat 09:33 ?        00:00:00   [migration/0]......4 S root         1     0  0  80   0 -   699 poll_s 09:33 ?        00:00:02 /sbin/init1 S root       324     1  0  80   0 -   611 poll_s 09:33 ?        00:00:00   upstart-udev-bridge5 S root       327     1  0  76  -4 -   630 poll_s 09:33 ?        00:00:00   udevd --daemon5 S root       421   327  0  78  -2 -   629 poll_s 09:33 ?        00:00:00     udevd --daemon5 S root       454   327  0  78  -2 -   629 poll_s 09:33 ?        00:00:00     udevd --daemon4 S root       665     1  0  80   0 -  1387 poll_s 09:33 ?        00:00:00   /usr/sbin/sshd -D4 S root      1623   665  0  80   0 -  2214 unix_s 13:18 ?        00:00:00     sshd: lincoln [pri5 S lincoln   1685  1623  0  80   0 -  2214 ?      13:18 ?        00:00:00       sshd: lincoln@pt0 S lincoln   1686  1685  0  80   0 -  1436 n_tty_ 13:18 pts/0    00:00:00         -bash0 S lincoln   2509  1685  0  80   0 -  1436 wait   13:43 pts/1    00:00:00         -bash0 R lincoln   4220  2509  0  80   0 -   714 -      14:51 pts/1    00:00:00           ps -eflH0 S lincoln   3917  1685  0  80   0 -  1436 n_tty_ 14:40 pts/2    00:00:00         -bash5 S syslog     666     1  0  80   0 -  9057 poll_s 09:33 ?        00:00:02   rsyslogd -c45 S 102        690     1  0  80   0 -   810 poll_s 09:33 ?        00:00:01   dbus-daemon --system......
lincoln@ubuntu:~$ ps ax  PID TTY      STAT   TIME COMMAND    1 ?        Ss     0:02 /sbin/init    2 ?        S      0:00 [kthreadd]    3 ?        S      0:00 [migration/0]  489 ?        S      0:00 [kpsmoused]  593 ?        S      0:00 [kgameportd]  665 ?        Ss     0:00 /usr/sbin/sshd -D  666 ?        Sl     0:03 rsyslogd -c4  690 ?        Ss     0:01 dbus-daemon --system --fork  790 ?        Ss     0:00 acpid -c /etc/acpi/events -s /var/run/acpid.socket  ...... 2509 pts/1    Ss     0:00 -bash 3917 pts/2    Ss+    0:00 -bash 5977 pts/1    R+     0:00 ps ax

pstree:process status tree,以树状图显示程序

参数:

-a  显示每个程序的完整指令,包含路径,参数或是常驻服务的标示。

-h  列出树状图时,特别标明现在执行的程序。

-l  采用长列格式显示树状图。

-u  显示用户名称。

lincoln@ubuntu:~$ pstree -ahluinit  |-NetworkManager  |   |-dhclient -d -sf /usr/lib/NetworkManager/nm-dhcp-client.action -pf /var/run/dhclient-eth0.pid -lf /var/lib/dhcp3/dhclient-2bc4cdc5-038a-4c25-9180-c03d12819a23-eth0.lease -cf /var/run/nm-dhclient-eth0.conf eth0  |   `-{NetworkManager}  |-gdm-binary  |   |-gdm-simple-slav --display-id /org/gnome/DisplayManager/Display1  |   |   |-Xorg :0 -nr -verbose -auth /var/run/gdm/auth-for-gdm-t1dOuG/database -nolisten tcp vt7  |   |   |-gdm-session-wor  |   |   |   |-gnome-session,andrew  |   |   |   |   |-bluetooth-apple  |   |   |   |   |-gdu-notificatio  |   |   |   |   |-gnome-panel  |   |   |   |   |-gnome-power-man  |   |   |   |   |-metacity --replace  |   |   |   |   |-nautilus  |   |   |   |   |-nm-applet --sm-disable  |   |   |   |   |-polkit-gnome-au  |   |   |   |   |-python /usr/share/system-config-printer/applet.py  |   |   |   |   |-ssh-agent /usr/bin/dbus-launch --exit-with-session gnome-session  |   |   |   |   |-update-notifier  |   |   |   |   `-{gnome-session}  |   |   |   `-{gdm-session-wo}  |   |   `-{gdm-simple-sla}  |   `-{gdm-binary}  |-gvfsd,andrew  |-hald,haldaemon  |   |-hald-runner,root  |   |   |-hald-addon-acpi,haldaemon  |   |   |-hald-addon-inpu  |   |   |-hald-addon-stor  |   |   `-hald-addon-stor  |   `-{hald}  |-irqbalance

top:显示,管理执行中的程序

语法:top [bciqsS][d <间隔秒数>][n <执行次数>]

参数:

  c  列出程序时,显示每个程序的完整指令,包括指令名称,路径和参数等相关信息。 
 d<间隔秒数>  设置top监控程序执行状况的间隔时间,单位以秒计算。 
 i  执行top指令时,忽略闲置或是已成为Zombie的程序。 
 n<执行次数>  设置监控信息的更新次数。 
 q  持续监控程序执行的状况。 

lincoln@ubuntu:~$ top top - 15:52:15 up  6:18,  4 users,  load average: 0.01, 0.15, 0.15Tasks: 129 total,   1 running, 128 sleeping,   0 stopped,   0 zombieCpu(s):  0.2%us,  0.2%sy,  0.0%ni, 99.2%id,  0.2%wa,  0.0%hi,  0.2%si,  0.0%stMem:   2049508k total,   451464k used,  1598044k free,    82000k buffersSwap:  1340408k total,        0k used,  1340408k free,   263008k cached  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                              5689 lincoln   20   0  2544 1204  924 R    0  0.1   0:00.16 top                                     1 root      20   0  2796 1656 1216 S    0  0.1   0:02.08 init                                    2 root      20   0     0    0    0 S    0  0.0   0:00.01 kthreadd                                3 root      RT   0     0    0    0 S    0  0.0   0:00.71 migration/0                             4 root      20   0     0    0    0 S    0  0.0   0:00.13 ksoftirqd/0                             5 root      RT   0     0    0    0 S    0  0.0   0:00.00 watchdog/0                              6 root      RT   0     0    0    0 S    0  0.0   0:00.50 migration/1                             7 root      20   0     0    0    0 S    0  0.0   0:00.02 ksoftirqd/1                             8 root      RT   0     0    0    0 S    0  0.0   0:00.00 watchdog/1                              9 root      20   0     0    0    0 S    0  0.0   0:00.35 events/0                               10 root      20   0     0    0    0 S    0  0.0   0:00.45 events/1                               11 root      20   0     0    0    0 S    0  0.0   0:00.00 cpuset                                 12 root      20   0     0    0    0 S    0  0.0   0:00.00 khelper                                13 root      20   0     0    0    0 S    0  0.0   0:00.00 netns                                  14 root      20   0     0    0    0 S    0  0.0   0:00.00 async/mgr                              15 root      20   0     0    0    0 S    0  0.0   0:00.00 pm                                     17 root      20   0     0    0    0 S    0  0.0   0:00.04 sync_supers                         

kill:删除执行中的程序或工作。kill可将指定的信息送至程序。

参数:

 -l <信息编号>      若不加<信息编号>选项,则-l参数会列出全部的信息名称。 
 -s <信息名称或编号>  指定要送出的信息。 
 [程序]            [程序]可以是程序的PID或是PGID,也可以是工作编号。

lincoln@ubuntu:~$ kill -l 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL       5) SIGTRAP 6) SIGABRT      7) SIGBUS       8) SIGFPE       9) SIGKILL     10) SIGUSR111) SIGSEGV     12) SIGUSR2     13) SIGPIPE     14) SIGALRM     15) SIGTERM16) SIGSTKFLT   17) SIGCHLD     18) SIGCONT     19) SIGSTOP     20) SIGTSTP21) SIGTTIN     22) SIGTTOU     23) SIGURG      24) SIGXCPU     25) SIGXFSZ26) SIGVTALRM   27) SIGPROF     28) SIGWINCH    29) SIGIO       30) SIGPWR31) SIGSYS      34) SIGRTMIN    35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+338) SIGRTMIN+4  39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+843) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+1348) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-1253) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-758) SIGRTMAX-6  59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-263) SIGRTMAX-1  64) SIGRTMAX
<a target=_blank href="mailto:lincoln@ubuntu:~$">lincoln@ubuntu:~$</a> ps -efl0 S lincoln   2509  1685  0  80   0 -  1436 wait   13:43 pts/1    00:00:00 -bashlincoln@ubuntu:~$ kill 2509

*  进程

1>创建一个新进程

#include <sys/types.h>#include <unistd.h>pid_t fork(void);


2>等待进程

#include <sys/types.h>#include <sys/wait.h>pid_t wait(int *stat_loc);

*  信号

   信号是UNIX和linux系统响应某些条件而产生的一个事件。接收到该信号的进程会相应地采取一些行动。

信号名称

说明

SIGABORT

进程异常终止

SIGALRM

超时警告

SIGFPE

浮点运算异常

SIGHUP

连接挂断

SIGILL

非法指令

SIGINT

终端中断

SIGKILL

终止进程(此信号不能被捕获或忽略)

SIGPIPE

向无读进程的管道写数据

SIGQUIT

终端退出

SIGSEGV

无效内存段访问

SIGTERM

终止

SIGUSR1

用户定义信号1

SIGUSR2

用户定义信号2

SIGCHLD

子进程已经停止或退出

SIGCONT

继续执行暂停进程

SIGSTOP

停止执行

SIGTSTP

终端挂起

SIGTTIN

后台进程尝试读操作

SIGTTOUT

后台进程尝试写操作


#include <signal.h>void (*signal(int sig, void (*func)(int)))(int);功能:设置一个信号的对应动作
#include <sys/types.h>#include <signal.h>int kill(pid_t pid, int sig);功能:把sig给定的信号发送给pid指定进程。

#include <unistd.h>unsigned int alarm(unsigned int seconds);功能:经过参数seconds指定的秒数后发送一个SIGALRM信号。

#include <signal.h>int sigaction(int sig, const struct sgaction *act, struct sigaction *oact);更健壮的信号编程接口

信号集
#include <signal.h>int sigaddset(sigset_t *set, int signo);int sigemptyset(sigset_t *set);int sigfillset(sigset_t *set);int sigdelset(sigset_t *set, int signo);

#include <signal.h>int sigismember(sigset_t *set, int signo);int sigprocmask(int how, const sigset *set, sigset_t *oset);int sigpending(sigset_t *set);int sigsuspend(const sigset_t *sigmask);


0 0