




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、操作系统进程管理实验实验题目:(1) 进程的创建编写一段程序,使用系统调用fork( )创建两个子进程。当此程序运行时,在系统中有一个父进程和两个子进程活动。让每一个进程在屏幕上显示一个字符:父进程显示字符“a”;子进程分别显示字符“b”和字符“c”。试观察记录屏幕上的显示结果,并分析原因。(2) 进程的控制修改已编写的程序,将每个进程输出一个字符改为每个进程输出一句话,在观察程序执行时屏幕上出现的现象,并分析原因。(3) 编制一段程序,使其实现进程的软中断通信。要求:使用系统调用fork( )创建两个子进程,再用系统调用signal( )让父进程捕捉键盘上来的中断信号(即按Del键);当捕捉
2、到中断信号后,父进程调用系统调用kill( )向两个子进程发出信号,子进程捕捉到信号后分别输出下列信息后终止: Child process 1 is killed by parent! Child process 2 is killed by parent! 父进程等待两个子进程终止后,输出如下的信息后终止: Parent process is killed! 在上面的程序中增加语句signal(SIGINT, SIG_IGN)和 signal(SIGQUIT, SIG_IGN),观察执行结果,并分析原因。(4) 进程的管道通信编制一段程序,实现进程的管道通信。使用系统调用pipe( )建立一
3、条管道线;两个进程P1和P2分别向管道各写一句话: Child 1 is sending a message! Child 2 is sending a message! 而父进程则从管道中读出来自于两个子进程的信息,显示在屏幕上。要求父进程先接收子进程P1发来的消息,然后再接收子进程P2发来的消息。实验源程序及报告:(1)、进程的创建 #include int main(int argc, char *argv) int pid1,pid2; /*fork first child process*/ if ( ( pid1=fork() ) 0 ) printf( ProcessCreate
4、 Failed!); exit(-1); if ( ( pid1=fork() ) = 0 ) printf( bn ); /*fork second child process*/ if ( ( pid2=fork() ) 0 ) printf( ProcessCreate Failed!); exit(-1); if ( ( pid2=fork() ) = 0 ) printf( cn ); /*parent process*/ else wait(NULL); printf( an ); exit(0); return 0; (2)、进程的控制#include int main(int
5、argc, char *argv) int pid1,pid2; /*fork first child process*/ if ( ( pid1=fork() ) 0 ) printf( ProcessCreate Failed!); exit(-1); if ( ( pid1=fork() ) = 0 ) printf( This is my Unix OS program!n ); /*fork second child process*/ if ( ( pid2=fork() ) 0 ) printf( ProcessCreate Failed!); exit(-1); if ( (
6、pid2=fork() ) = 0 ) printf( This is the second Child process!n ); /*parent process*/ else wait(NULL); printf( This is the Parent processn ); exit(0); return 0; (3) 编制一段程序,使其实现进程的软中断通信。#include #include #include #include int wait_flag; void stop( ); main( ) int pid1, pid2; signal(3, stop); while (pid
7、1 = fork( ) = -1); if ( (pid1 = fork() ) 0) while (pid2 = fork( ) = -1); if ( pid2 = fork() 0 ) wait_flag = 1; signal(SIGINT, stop); sleep(5); kill(pid1, 16); kill(pid2,17); wait(0); wait(0); printf(n Parent process is killed.n); exit(0); else wait_flag = 1; signal(17, stop); printf(n Child process
8、2 is killed by parent.n); exit(0); else wait_flag = 1; signal(16, stop); printf(n Child process 1 is killed by parent.n); exit(0); void stop( ) wait_flag = 0; (4) 进程的管道通信#include #include #include int pid1,pid2;int main() int fd2; char OutPipe100, InPipe100; pipe(fd); while(pid1 = fork() = -1); if(
9、pid1 = 0) lockf( fd1, 1, 0 ); sprintf(OutPipe, n Child process 1 is sending message!n); write( fd1, OutPipe, 50); sleep(5); lockf(fd1, 0 , 0); exit(0); else while(pid2 = fork() = -1); if( pid2 = 0) lockf( fd1, 1, 0 ); sprintf(OutPipe, n Child process 2 is sending message!n); write( fd1, OutPipe, 50)
10、; sleep(3); lockf(fd1, 0 , 0); exit(0); else wait(0); read(fd0, InPipe, 50); printf(%sn, InPipe); wait(0); read(fd0, InPipe, 50); printf(%sn, InPipe); exit(0); return 0; 三、程序注释:(1)进程的创建 源代码: #include int main(int argc, char *argv) int pid1,pid2; /*调用fork函数创建进程1*/ if ( ( pid1=fork() ) 0 ) /*fork()返回值
11、为负数,则创建进程失败*/ printf( ProcessCreate Failed!); exit(-1);/*/*fork()返回值为0,则创建子进程成功且当前进程为子进程*/ if ( ( pid1=fork() ) = 0 ) printf( bn ); /子进程1输出b; /*/*创建子进程2 */ if ( ( pid2=fork() ) 0 ) /*fork()返回值为负数,则创建进程失败*/ printf( ProcessCreate Failed!); exit(-1); /*/*fork()返回值为0,则创建子进程成功且当前进程为子进程*/ if ( ( pid2=fork
12、() ) = 0 ) printf( cn ); /子进程2输出c; /*fork()返回值大于0,则当前进程为父进程*/ else wait(NULL); printf( an ); /父进程输出a; exit(0); return 0; (2)进程的控制源代码: #include int main(int argc, char *argv) int pid1,pid2; /*调用fork函数创建进程1*/ if ( ( pid1=fork() ) 0 ) /*fork()返回值为负数,则创建进程失败*/ printf( ProcessCreate Failed!); exit(-1);/*
13、/*fork()返回值为0,则创建子进程成功且当前进程为子进程*/ if ( ( pid1=fork() ) = 0 ) printf( This is my Unix OS program!n ); /子进程1输出句子; /*/*创建子进程2 */ if ( ( pid2=fork() ) 0 ) /*fork()返回值为负数,则创建进程失败*/ printf( ProcessCreate Failed!); exit(-1); /*/*fork()返回值为0,则创建子进程成功且当前进程为子进程*/ if ( ( pid2=fork() ) = 0 ) printf( This is the
14、 second Child process!n ); /子进程2输出句子; /*fork()返回值大于0,则当前进程为父进程*/ else wait(NULL); printf( This is the Parent processn ); /父进程输出句子; exit(0); return 0; (3)编制一段程序,使其实现进程的软中断通信。源代码:(软中断用Ctrl+ | 实现)#include #include #include #include int wait_flag; void stop( ); /stop()是自己定义的Signal()触发的自定义函数main( ) int p
15、id1, pid2; /定义两个进程号参数 signal(3, stop); /Signal()触发软中断 while (pid1 = fork( ) = -1); /程序等待成功创建子进程事件的发生 if ( (pid1 = fork() ) 0) while (pid2 = fork( ) = -1); if ( pid2 = fork() 0 ) /*当前进程为父进程,父进程发出两个软中断信号Kill子进程*/ wait_flag = 1; signal(SIGINT, stop); sleep(5); kill(pid1, 16); kill(pid2,17); wait(0); /等
16、待子进程死信号 wait(0); printf(n Parent process is killed.n); /接收到子进程死信号后,杀死父进程 exit(0); else /* /*当前进程为子进程,则发送子进程Kill信号,杀死该子进程2 */ wait_flag = 1; signal(17, stop); printf(n Child process 2 is killed by parent.n); exit(0); else /* /*当前进程为子进程,则发送子进程Kill信号,杀死该子进程1 */ wait_flag = 1; signal(16, stop); printf(n
17、 Child process 1 is killed by parent.n); exit(0); void stop( ) /* /*自定义函数,供signal()调用 */ wait_flag = 0; (4)进程的管道通信 源代码:#include #include #include int pid1,pid2; /定义两个进程号参数 int main() int fd2; /*定义一个数组作为sys_pipe()的参数*/ char OutPipe100, InPipe100; pipe(fd); /*调用sys_pipe()创建管道线*/ while(pid1 = fork() =
18、-1); if( pid1 = 0) /* /*根据sys_pipe()函数的定义fd1用于向管道写数据 */ lockf( fd1, 1, 0 ); /*锁定管道写入端fd1*/ /*进程1向管道写入要输出的句子*/ sprintf(OutPipe, n Child process 1 is sending message!n); write( fd1, OutPipe, 50); sleep(5); lockf(fd1, 0 , 0); exit(0); else while(pid2 = fork() = -1); if( pid2 = 0) lockf( fd1, 1, 0 ); /*锁定管道写入端fd1*/ /*进程2向管道写入要输出的句子*/ sprintf(OutPipe, n Child process 2
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 金融科技产品创新与推广服务协议
- 酒店与健身房合作健身服务协议
- 网络系统安全维护合同
- 电商平台跨境进口业务合同
- 自考行政管理本科自我学习计划试题及答案探讨
- 领导者的决策能力与团队绩效关系试题及答案
- 行政管理心理学解决方案试题及答案
- 2025年自考行政管理案例研究与试题答案
- 行政管理的多层次治理研究试题及答案
- 2025机械设备采购合同模板示例
- JGJ114-2014 钢筋焊接网混凝土结构技术规程
- 毕业设计220kv变电站电气一次部份设计
- 安心护行 从个案分析看创伤骨科患者VTE管理低分子肝素合理应用版本
- JGT501-2016 建筑构件连接处防水密封膏
- 实验 验证牛顿第二定律
- 钻孔水文地质工程地质综合编录一览表模板
- 备用柴油发电机定期启动试验记录表
- 国企食堂运作方案
- 二年级上册心理健康教育说课稿-面对批评 全国通用
- 工程管理检讨书
- 劳务派遣合同示范文本(4篇)
评论
0/150
提交评论