




已阅读5页,还剩6页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验报告实验名称:Linux 多进程程序设计一、 实验目的熟悉Linux下多进程编程的方法,掌握Linux下fork、exec、wait、waitpid等进程相关的系统调用使用方法。二、 实验内容在父进程中创建两个子进程,其中一个子进程运行“ls -l”指令,另一个子进程在暂停5s之后退出。父进程先用阻塞方式等待第一个子进程的结束,然后用非阻塞方式等待第二个子进程的退出,待收集到第二个子进程结束的信息,父进程就返回。三、 实验内容1、 程序说明、流程图等父进程首先创建一个子进程A,采用组赛模式等待进程A结束,然后创建进程B,采用非阻塞模式等待B 进程结束,然后父进程结束 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 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);sleep(5);printf(B process end!n);exit(0);else dopr=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 process is wating for children !wating .cess 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 02: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 ? 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:00 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 ? 00: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 upstart-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: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 upstart-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/getty -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: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 /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 /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/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 1122 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/pulseaudio/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 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 1171 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-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/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/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/indicatohao 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-noti
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 固原市中石油2025秋招面试半结构化模拟题及答案油田工程技术岗
- 广东地区中石油2025秋招笔试模拟题含答案电气仪控技术岗
- 洛阳市中储粮2025秋招面试专业追问题库综合管理岗
- 2025年鱼船船员考试题及答案
- 中国广电齐齐哈尔市2025秋招面试无领导高频议题20例
- 2025年医院招人考试试题及答案
- 大唐电力甘孜自治州2025秋招机械工程专业面试追问及参考回答
- 安顺市中石油2025秋招笔试模拟题含答案炼油工艺技术岗
- 六安市中石油2025秋招笔试模拟题含答案数智化与信息工程岗
- 中国广电白城市2025秋招网络优化与维护类专业追问清单及参考回答
- JT-T-1258-2019港口能源计量导则
- (必会)中级《审计理论与实务》近年考试真题题库(300题)
- 烘焙与甜点制作
- T-CRHA 028-2023 成人住院患者静脉血栓栓塞症风险评估技术
- 地基事故案例分析
- 国家开放大学《财政与金融(农)》形考任务1-4参考答案
- 英语考级-a级词汇完整版
- 隧道钻爆法掘进施工安全操作规程
- 计算机网络技术专业介绍解析
- 圆锥式破碎机说明书样本
- 九年级英语 第二单元 教案 ·(全)·
评论
0/150
提交评论