1、Linux多进程程序设计实验报告实验名称:Linux 多进程程序设计一、 实验目的熟悉Linux下多进程编程的方法,掌握Linux下fork、exec、wait、waitpid等进程相关的系统调用使用方法。二、 实验内容在父进程中创建两个子进程,其中一个子进程运行“ls -l”指令,另一个子进程在暂停5s之后退出。父进程先用阻塞方式等待第一个子进程的结束,然后用非阻塞方式等待第二个子进程的退出,待收集到第二个子进程结束的信息,父进程就返回。三、 实验内容1、 程序说明、流程图等父进程首先创建一个子进程A,采用组赛模式等待进程A结束,然后创建进程B,采用非阻塞模式等待B 进程结束,然后父进程结束
2、 A进程运行 ps -ef B进程等待5S 自动结束 父进程会铺货进程A,B的结束,然后给予相应的提示。2、 程序代码#include #include #include #include #include int main(void ) pid_t pa,pb,pr; pa=fork(); if(pa0) perror(fork a error!); else if(pa=0) sleep(1); printf(process A is running.n); if(execlp(ps,ps,-ef,NULL)0) perror(execlp a error!); exit(0); else
3、 printf(this is parent process!n); printf(parent process is wating for children !n); printf(wating .wating.n); pr=wait(NULL); if(pr=pa) printf(parent catched the child A!n); printf(creat process B!n); pb=fork(); if(pb0) printf(error fork Bn); else if(pb=0) printf(B process is running( 5 S )!n); slee
4、p(5); printf(B process end!n); exit(0); else do pr=waitpid(pb,NULL,WNOHANG); if(pr=0)/*B not end*/ printf(father is wating for child B!n); sleep(1); while(pr=0);/*wait for process B*/ if(pr=pb) printf(child B very well!n); else printf(my child B lost!); 3、 运行测试输出的结果this is parent process!parent proc
5、ess is wating for children !wating .wating.process A is running.UID PID PPID C STIME TTY TIME CMDroot 1 0 0 02:57 ? 00:00:01 /sbin/initroot 2 0 0 02:57 ? 00:00:00 kthreaddroot 3 2 0 02:57 ? 00:00:00 ksoftirqd/0root 5 2 0 02:57 ? 00:00:00 kworker/u:0root 6 2 0 02:57 ? 00:00:00 migration/0root 7 2 0 0
6、2:57 ? 00:00:00 cpusetroot 8 2 0 02:57 ? 00:00:00 khelperroot 9 2 0 02:57 ? 00:00:00 netnsroot 10 2 0 02:57 ? 00:00:00 sync_supersroot 11 2 0 02:57 ? 00:00:00 bdi-defaultroot 12 2 0 02:57 ? 00:00:00 kintegritydroot 13 2 0 02:57 ? 00:00:00 kblockdroot 14 2 0 02:57 ? 00:00:00 kacpidroot 15 2 0 02:57 ?
7、 00:00:00 kacpi_notifyroot 16 2 0 02:57 ? 00:00:00 kacpi_hotplugroot 17 2 0 02:57 ? 00:00:00 ata_sffroot 18 2 0 02:57 ? 00:00:00 khubdroot 19 2 0 02:57 ? 00:00:00 mdroot 22 2 0 02:57 ? 00:00:00 khungtaskdroot 23 2 0 02:57 ? 00:00:00 kswapd0root 24 2 0 02:57 ? 00:00:00 ksmdroot 25 2 0 02:57 ? 00:00:0
8、0 fsnotify_markroot 26 2 0 02:57 ? 00:00:00 aioroot 27 2 0 02:57 ? 00:00:00 ecryptfs-kthrearoot 28 2 0 02:57 ? 00:00:00 cryptoroot 32 2 0 02:57 ? 00:00:00 kthrotldroot 34 2 0 02:57 ? 00:00:00 scsi_eh_0root 35 2 0 02:57 ? 00:00:01 scsi_eh_1root 36 2 0 02:57 ? 00:00:00 kworker/u:3root 39 2 0 02:57 ? 0
9、0:00:00 kmpathdroot 40 2 0 02:57 ? 00:00:00 kmpath_handlerdroot 41 2 0 02:57 ? 00:00:00 kondemandroot 42 2 0 02:57 ? 00:00:00 kconservativeroot 160 2 0 02:57 ? 00:00:00 scsi_eh_2root 176 2 0 02:57 ? 00:00:00 jbd2/sda1-8root 177 2 0 02:57 ? 00:00:00 ext4-dio-unwritroot 224 1 0 02:57 ? 00:00:00 upstar
10、t-udev-bridge -daemonroot 235 1 0 02:57 ? 00:00:00 udevd -daemonsyslog 329 1 0 02:57 ? 00:00:00 rsyslogd -c4102 334 1 0 02:57 ? 00:00:00 dbus-daemon -system -fork -avahi 361 1 0 02:57 ? 00:00:00 avahi-daemon: running hao-Viravahi 362 361 0 02:57 ? 00:00:00 avahi-daemon: chroot helperroot 375 1 0 02:
11、57 ? 00:00:00 NetworkManagerroot 383 1 0 02:57 ? 00:00:00 /usr/sbin/modem-managerroot 392 1 0 02:57 ? 00:00:00 /usr/lib/policykit-1/polkitdroot 437 1 0 02:57 ? 00:00:00 /sbin/wpa_supplicant -u -sroot 445 2 0 02:57 ? 00:00:00 kpsmousedroot 506 2 0 02:57 ? 00:00:00 iprtroot 526 1 0 02:57 ? 00:00:00 up
12、start-socket-bridge -daemonroot 606 1 0 02:57 tty4 00:00:00 /sbin/getty -8 38400 tty4root 614 1 0 02:57 tty5 00:00:00 /sbin/getty -8 38400 tty5root 626 1 0 02:57 tty2 00:00:00 /sbin/getty -8 38400 tty2root 633 1 0 02:57 tty3 00:00:00 /sbin/getty -8 38400 tty3root 636 1 0 02:57 tty6 00:00:00 /sbin/ge
13、tty -8 38400 tty6daemon 643 1 0 02:57 ? 00:00:00 atdroot 644 1 0 02:57 ? 00:00:00 acpid -c /etc/acpi/events -s /root 645 1 0 02:57 ? 00:00:00 cronroot 664 2 0 02:57 ? 00:00:00 flush-8:0root 746 1 0 02:57 ? 00:00:02 /usr/sbin/VBoxServiceroot 905 1 0 02:57 ? 00:00:00 gdm-binaryroot 908 1 0 02:57 ? 00:
14、00:00 /usr/sbin/cupsd -Froot 910 1 0 02:57 tty1 00:00:00 /sbin/getty -8 38400 tty1root 914 1 0 02:57 ? 00:00:00 /usr/sbin/console-kit-daemon -root 980 905 0 02:57 ? 00:00:00 /usr/lib/gdm/gdm-simple-slaveroot 985 980 2 02:57 tty7 00:01:28 /usr/bin/X :0 -nr -verbose -auroot 1000 980 0 02:57 ? 00:00:00
15、 /usr/lib/gdm/gdm-session-workehao 1009 1000 0 02:57 ? 00:00:00 gnome-session -session=ubuntuhao 1052 1 0 02:57 ? 00:00:00 /usr/bin/VBoxClient -clipboarhao 1064 1 0 02:57 ? 00:00:00 /usr/bin/VBoxClient -displayhao 1070 1 0 02:57 ? 00:00:00 /usr/bin/VBoxClient -seamlesshao 1076 1 0 02:57 ? 00:00:30 /
16、usr/bin/VBoxClient -draganddhao 1079 1009 0 02:57 ? 00:00:00 /usr/bin/ssh-agent /usr/bin/dbhao 1082 1 0 02:57 ? 00:00:00 /usr/bin/dbus-launch -exit-wihao 1083 1 0 02:57 ? 00:00:01 /bin/dbus-daemon -fork -prihao 1088 1 0 02:57 ? 00:00:00 /usr/lib/libgconf2-4/gconfd-2hao 1101 1 0 02:57 ? 00:00:00 /usr
17、/bin/gnome-keyring-daemonhao 1106 1 0 02:57 ? 00:00:04 /usr/lib/gnome-settings-daemonhao 1109 1 0 02:57 ? 00:00:00 /usr/lib/gvfs/gvfsdhao 1114 1009 0 02:57 ? 00:00:05 metacityhao 1116 1 0 02:57 ? 00:00:00 /usr/lib/gvfs/gvfs-fuse-daemohao 1120 1 0 02:57 ? 00:00:04 /usr/bin/pulseaudio -start -rtkit 11
18、22 1 0 02:57 ? 00:00:00 /usr/lib/rtkit/rtkit-daemonhao 1127 1009 0 02:57 ? 00:00:10 nautilushao 1128 1009 0 02:57 ? 00:00:00 /usr/lib/evolution/2.32/evoluthao 1131 1009 0 02:57 ? 00:00:04 gnome-panelhao 1135 1009 0 02:57 ? 00:00:00 zeitgeist-datahubhao 1137 1120 0 02:57 ? 00:00:00 /usr/lib/pulseaudi
19、o/pulse/gconhao 1138 1009 0 02:57 ? 00:00:00 nm-applet -sm-disablehao 1140 1009 0 02:57 ? 00:00:00 gnome-power-managerhao 1142 1009 0 02:57 ? 00:00:00 /usr/lib/policykit-1-gnome/polhao 1143 1009 0 02:57 ? 00:00:00 bluetooth-applethao 1152 1 0 02:57 ? 00:00:00 /usr/lib/gvfs/gvfs-gdu-volume-root 1156
20、1 0 02:57 ? 00:00:00 /usr/lib/udisks/udisks-daemonroot 1158 1156 0 02:57 ? 00:00:01 udisks-daemon: polling /dev/srhao 1160 1 0 02:57 ? 00:00:01 /usr/bin/python /usr/bin/zeitghao 1163 1 0 02:57 ? 00:00:00 /usr/lib/gvfs/gvfs-afc-volume-hao 1167 1 0 02:57 ? 00:00:00 /usr/lib/gvfs/gvfs-gphoto2-volhao 11
21、71 1160 0 02:57 ? 00:00:00 /bin/cathao 1176 1160 0 02:57 ? 00:00:00 zeitgeist-datah root 1195 1 0 02:57 ? 00:00:00 /usr/lib/upower/upowerdhao 1263 1 0 02:57 ? 00:00:00 /usr/lib/gvfs/gvfsd-trash -sphao 1265 1 0 02:57 ? 00:00:00 gnome-screensaverhao 1272 1 0 02:57 ? 00:00:02 /usr/lib/notify-osd/notify
22、-osdhao 1290 1 0 02:57 ? 00:00:00 /usr/lib/bonobo-activation/bonhao 1303 1 0 02:57 ? 00:00:03 /usr/lib/gnome-panel/wnck-applhao 1305 1 0 02:57 ? 00:00:00 /usr/lib/gnome-applets/trashaphao 1311 1 0 02:57 ? 00:00:00 /usr/lib/gnome-panel/notificathao 1312 1 0 02:57 ? 00:00:01 /usr/lib/indicator-applet/
23、indihao 1327 1009 0 02:57 ? 00:00:00 /usr/lib/gnome-disk-utility/gdhao 1330 1 0 02:57 ? 00:00:00 /usr/lib/gvfs/gvfsd-burn -spahao 1339 1 0 02:57 ? 00:00:00 /usr/lib/gvfs/gvfsd-metadatahao 1343 1 0 02:57 ? 00:00:00 /usr/lib/d-conf/dconf-servicehao 1347 1 0 02:57 ? 00:00:00 /usr/lib/indicator-messages
24、/inhao 1349 1 0 02:57 ? 00:00:00 /usr/lib/indicator-sound/indichao 1351 1 0 02:57 ? 00:00:00 /usr/lib/indicator-applicationhao 1353 1 0 02:57 ? 00:00:00 /usr/lib/indicator-session/indhao 1356 1 0 02:57 ? 00:00:00 /usr/lib/indicator-datetime/inhao 1357 1 0 02:57 ? 00:00:00 /usr/lib/indicator-me/indic
25、atohao 1397 1 0 02:57 ? 00:00:00 /usr/lib/geoclue/geoclue-mastehao 1399 1009 0 02:57 ? 00:00:00 /usr/bin/python /usr/share/syshao 1404 1 0 02:58 ? 00:00:02 /usr/lib/bamf/bamfdaemonhao 1409 1009 0 02:58 ? 00:00:00 update-notifierroot 1864 2 0 03:32 ? 00:00:00 kworker/0:0root 1871 2 0 03:37 ? 00:00:00
26、 kworker/0:1root 1918 2 0 03:42 ? 00:00:00 kworker/0:2root 1954 235 0 03:45 ? 00:00:00 udevd -daemonroot 1955 235 0 03:45 ? 00:00:00 udevd -daemonroot 1983 2 0 03:45 ? 00:00:00 kworker/0:3hao 2010 1 1 03:46 ? 00:00:03 gedit /home/hao/Desktop/test.chao 2015 1 1 03:47 ? 00:00:01 gnome-terminalhao 2019
27、 2015 0 03:47 ? 00:00:00 gnome-pty-helperhao 2020 2015 0 03:47 pts/0 00:00:00 bashhao 2085 2020 0 03:49 pts/0 00:00:00 ./testhao 2086 2085 3 03:49 pts/0 00:00:00 ps -efparent catched the child A!creat process B!father is wating for child B!B process is running( 5 S )!father is wating for child B!father is wating for child B!father is wating for child B!father is wating for child B!B process end!child B very well!haohao-VirtualBox:/Desktop$
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1