Chinaunix首页 | 论坛 | 博客
  • 博客访问: 157647
  • 博文数量: 76
  • 博客积分: 1513
  • 博客等级: 上尉
  • 技术积分: 755
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-25 15:15
文章分类

全部博文(76)

文章存档

2012年(2)

2011年(74)

我的朋友

分类: C/C++

2011-11-25 20:56:57

  1. #include<iostream>

  2. #include<sys/wait.h>

  3. #include<sys/types.h>

  4. using namespace std;

  5.  

  6. int main()

  7. {

  8. pid_t pid;

  9. char* message;

  10. int n;

  11. pid=fork();

  12.  

  13. if(pid==-1)

  14. {

  15. cout<<"error"<<endl;

  16. exit(1);

  17. }

  18. else if(pid==0)

  19. {

  20. message="child process";

  21. n=5;

  22. }

  23. else

  24. {

  25. message="parent process";

  26. n=3;

  27. }

  28. for(;n>0;n--)

  29. cout<<message<<endl;

  30. int stat;

  31. pid_t child_pid=wait(&stat);

  32. cout<<child_pid<<endl;

  33. if(pid!=0)

  34. {

  35. cout<<WIFEXITED(stat)<<endl;

  36. cout<<WEXITSTATUS(stat)<<endl;

  37. cout<<WIFSIGNALED(stat)<<endl;

  38. cout<<WTERMSIG(stat)<<endl;

  39. system("ps ax");

  40. cout<<WIFSTOPPED(stat)<<endl;

  41. cout<<WSTOPSIG(stat)<<endl;

  42. }

  43. return 0;

  44. }

output:

child process

child process

child process

child process

child process

-1

parent process

parent process

parent process

20060

1

0

0

0

  PID TTY      STAT   TIME COMMAND

    1 ?        Ss     0:09 init [5]                                            

    2 ?        S<     0:00 [migration/0]

    3 ?        SN     0:00 [ksoftirqd/0]

    4 ?        S<     0:00 [watchdog/0]

    5 ?        S<     0:00 [migration/1]

    6 ?        SN     0:00 [ksoftirqd/1]

    7 ?        S<     0:00 [watchdog/1]

    8 ?        S<     0:00 [events/0]

    9 ?        S<     0:00 [events/1]

   10 ?        S<     0:00 [khelper]

   11 ?        S<     0:00 [kthread]

   15 ?        S<     0:00 [kblockd/0]

   16 ?        S<     0:00 [kblockd/1]

   17 ?        S<     0:00 [kacpid]

   63 ?        S<     0:00 [cqueue/0]

   64 ?        S<     0:00 [cqueue/1]

   67 ?        S<     0:00 [khubd]

   69 ?        S<     0:00 [kseriod]

  137 ?        S      0:00 [khungtaskd]

  138 ?        S      0:00 [pdflush]

  139 ?        S      0:00 [pdflush]

  140 ?        S<     0:00 [kswapd0]

  141 ?        S<     0:00 [aio/0]

  142 ?        S<     0:00 [aio/1]

  297 ?        S<     0:00 [kpsmoused]

  331 ?        S<     0:00 [ata/0]

  332 ?        S<     0:00 [ata/1]

  333 ?        S<     0:00 [ata_aux]

  340 ?        S<     0:00 [kstriped]

  353 ?        S<     0:00 [ksnapd]

  368 ?        S<     0:08 [kjournald]

  394 ?        S<     0:00 [kauditd]

  427 ?        S

  822 ?        S<     0:00 [xenwatch]

  825 ?        S<     0:00 [xenbus]

 1219 ?        S<     0:00 [kmpathd/0]

 1220 ?        S<     0:00 [kmpathd/1]

 1222 ?        S<     0:00 [kmpath_handlerd]

 1282 ?        S<     0:00 [kjournald]

 1446 ?        S<     0:00 [iscsi_eh]

 1469 ?        S<     0:00 [iw_cxgb3]

 1509 ?        S<     0:00 [ib_addr]

 1517 ?        S<     0:00 [ib_mcast]

 1518 ?        S<     0:00 [ib_inform]

 1519 ?        S<     0:00 [local_sa]

 1523 ?        S<     0:00 [iw_cm_wq]

 1526 ?        S<     0:00 [ib_cm/0]

 1527 ?        S<     0:00 [ib_cm/1]

 1532 ?        S<     0:00 [rdma_cm]

 1549 ?        Ssl    0:00 brcm_iscsiuio

 1554 ?        Ss     0:00 iscsid

 1555 ?        S

 1738 ?        S

 1740 ?        S

 1762 ?        Ss     0:00 syslogd -m 0

 1765 ?        Ss     0:00 klogd -x

 1819 ?        Ss     0:02 irqbalance

 1837 ?        Ss     0:00 portmap

 1864 ?        S<     0:00 [rpciod/0]

 1865 ?        S<     0:00 [rpciod/1]

 1872 ?        Ss     0:00 rpc.statd

 1896 ?        Ss     0:00 rpc.idmapd

 1911 ?        Ss     0:02 dbus-daemon --system

 1926 ?        Ss     0:00 /usr/sbin/hcid

 1930 ?        Ss     0:00 /usr/sbin/sdpd

 1956 ?        S<     0:00 [krfcommd]

 1993 ?        Ssl    0:00 pcscd

 2003 ?        Ss     0:00 /usr/sbin/acpid

 2016 ?        Ss     0:00 hald

 2017 ?        S      0:00 hald-runner

 2026 ?        S      0:00 hald-addon-acpi: listening on acpid socket /var/run/a

 2031 ?        S      0:00 hald-addon-keyboard: listening on /dev/input/event0

 2040 ?        S     17:12 hald-addon-storage: polling /dev/hdc

 2057 ?        Ss     0:00 /usr/bin/hidd --server

 2091 ?        Ssl    0:00 automount

 2108 ?        Ss     0:00 ./hpiod

 2113 ?        S      0:00 /usr/bin/python ./hpssd.py

 2128 ?        Ss     0:00 /usr/sbin/sshd

 2139 ?        Ss     0:00 cupsd

 2153 ?        Ss     0:00 xinetd -stayalive -pidfile /var/run/xinetd.pid

 2165 ?        Ss     0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf

 2187 ?        Ss     0:00 sendmail: accepting connections

 2197 ?        Ss     0:00 sendmail: Queue runner@01:00:00 for /var/spool/client

 2207 ?        Ss     0:00 gpm -m /dev/input/mice -t exps2

 2216 ?        Ss     0:00 crond

 2249 ?        Ss     0:00 xfs -droppriv -daemon

 2266 ?        Ss     0:00 /usr/sbin/atd

 2308 ?        Ss     0:00 avahi-daemon: running [m1.local]

 2309 ?        Ss     0:00 avahi-daemon: chroot helper

 2374 ?        S      0:00 /usr/sbin/smartd -q never

 2379 tty1     Ss+    0:00 /sbin/mingetty tty1

 2383 tty2     Ss+    0:00 /sbin/mingetty tty2

 2384 tty3     Ss+    0:00 /sbin/mingetty tty3

 2385 tty4     Ss+    0:00 /sbin/mingetty tty4

 2386 tty5     Ss+    0:00 /sbin/mingetty tty5

 2387 tty6     Ss+    0:00 /sbin/mingetty tty6

 2388 ?        Ss     0:00 /usr/sbin/gdm-binary -nodaemon

 2448 ?        SN     0:00 /usr/bin/python -tt /usr/sbin/yum-updatesd

 2450 ?        SN     0:00 /usr/libexec/gam_server

 2490 ?        S      0:00 /usr/sbin/gdm-binary -nodaemon

 2492 ?        Sl     0:00 /usr/libexec/gdm-rh-security-token-helper

 2493 tty7     Ss+    0:02 /usr/bin/Xorg :0 -br -audit 0 -auth /var/gdm/:0.Xauth

 2636 ?        Ss     0:00 /usr/libexec/gdmgreeter

 3075 ?        S      0:00 /usr/sbin/nm-system-settings --config /etc/NetworkMan

 3412 ?        Ssl    0:00 /usr/libexec/bonobo-activation-server --ac-activate -

17254 ?        S      0:00 /usr/sbin/gdm-binary -nodaemon

17275 ?        Ss     0:00 /usr/bin/gnome-session

17315 ?        Z      0:00 [Xsession]

17318 ?        Ss     0:00 /usr/bin/ssh-agent /bin/sh -c exec -l /bin/bash -c "/

17352 ?        S      0:00 /usr/bin/dbus-launch --exit-with-session /etc/X11/xin

17353 ?        Ss     0:00 /bin/dbus-daemon --fork --print-pid 4 --print-address

17364 ?        S      0:00 /usr/libexec/gconfd-2 11

17365 ?        Ss     0:00 /usr/lib/scim-1.0/scim-launcher -d -c simple -e all -

17368 ?        S      0:00 /usr/bin/gnome-keyring-daemon

17370 ?        Sl     0:00 /usr/libexec/gnome-settings-daemon

17396 ?        Ss     0:00 metacity --sm-client-id=default1

17404 ?        Ss     0:00 /usr/lib/scim-1.0/scim-helper-manager

17405 ?        Ssl    0:00 /usr/lib/scim-1.0/scim-panel-gtk --display 10.104.1.2

17407 ?        Ss     0:00 /usr/lib/scim-1.0/scim-launcher -d -c socket -e socke

17413 ?        Ss     0:00 gnome-panel --sm-client-id default2

17415 ?        Ss     0:00 nautilus --no-default-window --sm-client-id default3

17425 ?        Ssl    0:00 /usr/libexec/bonobo-activation-server --ac-activate -

17429 ?        Ss     0:00 eggcups --sm-client-id default4

17439 ?        S      0:00 /usr/libexec/gnome-vfs-daemon

17441 ?        Ss     0:00 bt-applet --sm-disable

17468 ?        Ss     0:00 pam-panel-icon --sm-client-id default0

17473 ?        Sl     0:00 ./escd --key_Inserted="/usr/bin/esc" --on_Signal="/us

17481 ?        Ss     0:00 gnome-power-manager

17492 ?        S      0:00 /sbin/pam_timestamp_check -d root

17494 ?        S      0:00 /usr/libexec/wnck-applet --oaf-activate-iid=OAFIID:GN

17503 ?        S      0:00 /usr/libexec/mapping-daemon

17507 ?        S      0:00 /usr/libexec/gam_server

17509 ?        S      0:00 /usr/libexec/trashapplet --oaf-activate-iid=OAFIID:GN

17517 ?        S      0:00 /usr/libexec/notification-area-applet --oaf-activate-

17520 ?        S      0:00 /usr/libexec/clock-applet --oaf-activate-iid=OAFIID:G

17522 ?        S      0:00 /usr/libexec/mixer_applet2 --oaf-activate-iid=OAFIID:

17540 ?        S      0:00 /home/vcuser/Desktop/link to STS

17541 ?        Sl     0:54 /usr/java/jdk1.6.0_23/bin/java -Dosgi.requiredJavaVer

17567 ?        S      0:03 scim-bridge

17610 ?        Ss     0:00 gnome-screensaver

18518 ?        S      0:00 /usr/sbin/gdm-binary -nodaemon

18539 ?        Ss     0:00 /usr/bin/gnome-session

18579 ?        Z      0:00 [Xsession]

18583 ?        Ss     0:00 /usr/bin/ssh-agent /bin/sh -c exec -l /bin/bash -c "/

18616 ?        Ss     0:00 /bin/dbus-daemon --fork --print-pid 4 --print-address

18617 ?        S      0:00 /usr/bin/dbus-launch --exit-with-session /etc/X11/xin

18628 ?        S      0:00 /usr/libexec/gconfd-2 5

18630 ?        S      0:00 /usr/bin/gnome-keyring-daemon

18632 ?        Sl     0:00 /usr/libexec/gnome-settings-daemon

18643 ?        Ss     0:00 /usr/lib/scim-1.0/scim-launcher -d -c simple -e all -

18657 ?        Ss     0:02 metacity --sm-client-id=default1

18669 ?        Ss     0:00 gnome-panel --sm-client-id default2

18671 ?        Ssl    0:03 nautilus --no-default-window --sm-client-id default3

18684 ?        S      0:00 /usr/libexec/gnome-vfs-daemon

18686 ?        Ssl    0:00 /usr/libexec/bonobo-activation-server --ac-activate -

18691 ?        Ss     0:00 eggcups --sm-client-id default4

18701 ?        Ss     0:00 bt-applet --sm-disable

18733 ?        Ss     0:00 pam-panel-icon --sm-client-id default0

18739 ?        S      0:00 /usr/libexec/gam_server

18741 ?        S      0:01 /usr/libexec/wnck-applet --oaf-activate-iid=OAFIID:GN

18745 ?        S      0:00 /usr/libexec/trashapplet --oaf-activate-iid=OAFIID:GN

18752 ?        Ss     0:00 gnome-power-manager

18754 ?        Sl     0:00 ./escd --key_Inserted="/usr/bin/esc" --on_Signal="/us

18755 ?        Ss     0:00 /usr/lib/scim-1.0/scim-helper-manager

18766 ?        Ssl    0:00 /usr/lib/scim-1.0/scim-panel-gtk --display 10.104.1.2

18768 ?        Ss     0:00 /usr/lib/scim-1.0/scim-launcher -d -c socket -e socke

18773 ?        S      0:00 /sbin/pam_timestamp_check -d root

18778 ?        S      0:00 /usr/libexec/mapping-daemon

18783 ?        S      0:00 /usr/libexec/notification-area-applet --oaf-activate-

18785 ?        S      0:00 /usr/libexec/clock-applet --oaf-activate-iid=OAFIID:G

18787 ?        S      0:00 /usr/libexec/mixer_applet2 --oaf-activate-iid=OAFIID:

18808 ?        Rs     0:01 gnome-screensaver

18846 ?        S      0:01 scim-bridge

19536 ?        S      0:00 /usr/sbin/gdm-binary -nodaemon

19558 ?        Ss     0:00 /usr/bin/gnome-session

19598 ?        Z      0:00 [Xsession] //这个是僵尸进程:即结束后但是仍然在进程表中的子进程

19601 ?        Ss     0:00 /usr/bin/ssh-agent /bin/sh -c exec -l /bin/bash -c "/

19635 ?        S      0:00 /usr/bin/dbus-launch --exit-with-session /etc/X11/xin

19636 ?        Ss     0:00 /bin/dbus-daemon --fork --print-pid 4 --print-address

19647 ?        S      0:00 /usr/libexec/gconfd-2 5

19649 ?        S      0:00 /usr/bin/gnome-keyring-daemon

19651 ?        Sl     0:00 /usr/libexec/gnome-settings-daemon

19659 ?        Ss     0:00 /usr/lib/scim-1.0/scim-launcher -d -c simple -e all -

19673 ?        Ss     0:00 metacity --sm-client-id=default1

19686 ?        Ss     0:00 gnome-panel --sm-client-id default2

19688 ?        Ss     0:00 nautilus --no-default-window --sm-client-id default3

19701 ?        S      0:00 /usr/libexec/gnome-vfs-daemon

19703 ?        Ss     0:00 eggcups --sm-client-id default4

19711 ?        Ss     0:00 bt-applet --sm-disable

19738 ?        Sl     0:00 ./escd --key_Inserted="/usr/bin/esc" --on_Signal="/us

19740 ?        Ssl    0:00 /usr/libexec/bonobo-activation-server --ac-activate -

19756 ?        Ss     0:00 /usr/lib/scim-1.0/scim-helper-manager

19757 ?        Ssl    0:00 /usr/lib/scim-1.0/scim-panel-gtk --display 10.104.1.2

19759 ?        Ss     0:00 /usr/lib/scim-1.0/scim-launcher -d -c socket -e socke

19770 ?        Ss     0:00 pam-panel-icon --sm-client-id default0

19773 ?        Ss     0:00 gnome-power-manager

19775 ?        S      0:00 /usr/libexec/gam_server

19778 ?        S      0:00 /usr/libexec/trashapplet --oaf-activate-iid=OAFIID:GN

19788 ?        S      0:00 /usr/libexec/wnck-applet --oaf-activate-iid=OAFIID:GN

19791 ?        S      0:00 /sbin/pam_timestamp_check -d root

19796 ?        S      0:00 /usr/libexec/mapping-daemon

19801 ?        S      0:00 /usr/libexec/notification-area-applet --oaf-activate-

19803 ?        S      0:00 /usr/libexec/clock-applet --oaf-activate-iid=OAFIID:G

19805 ?        S      0:00 /usr/libexec/mixer_applet2 --oaf-activate-iid=OAFIID:

19828 ?        Ss     0:00 gnome-screensaver

19856 ?        Rl     0:13 /home/zxq/Desktop/eclipse

19982 ?        S      0:02 gedit file:///home/zxq/signal_test.cpp

20059 ?        S      0:00 /home/zxq/workspace/process_test/Debug/process_test

20061 ?        R      0:00 ps ax

0

0


阅读(1885) | 评论(0) | 转发(0) |
0

上一篇:Linux多线程之信号2

下一篇:kill()程序示例

给主人留下些什么吧!~~