操作系统实验报告_第1页
操作系统实验报告_第2页
操作系统实验报告_第3页
操作系统实验报告_第4页
操作系统实验报告_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、武汉工程大学实验报告专业班级:网工 01 |姓名: 何彬|学号:1305120411课程名称:操作系统实验成绩:指导教师:蔡敦波 实验名称:进程控制与进程间通信一、实验目的:1、掌握进程的概念,明确进程和程序的区别。2、认识和了解并发执行的实质。3、了解什么是信号。4、熟悉LINUX系统中进程之间软中断通信的基本原理。二、实验内容:1、进程的创建(必做题)编写一段程序,使用系统调用fork()创建两个子进程,在系统中有一个父进程和两个子进程活动。让每个进程在屏幕上显示一个字符;父进程显示字符“a”,子进程分别显示字符 “b”和“c”。试观察记录屏幕上的显示结果,并分析原因。参考程序roat0h

2、ebin -# cat fork, c#include<stdio.h>pinclude<stdlib.h>int main() Int pl, p2;while(pl=fork() = -1);if(pl = 0)£putcharf * b *);p rintf("n") ; else while(p2=fork() = -1); if(p2 = 0)( putch«r( 'c 1 );printf (*'n"); else putchar( a ); printf (;root®he-bin

3、 -# vim fork. croothebin j# gcc fork. c -o forkroottghebln -# ./forkbc运行的结果是bca.首先创建进程p1,向子进程返回0,输出b.又创 建进程p2,向子进程返回0,输出c,同时向父进程返回子进程的 pid , 输出a2、修改已编写的程序,将每个进程的输出由单个字符改为一句话,再观察程序执行时屏幕上 出现的现象,并分析其原因。(必做题)参考程序# include<stdio.h> int main() int p1, p2, i;while(p1=fork()= = -1);if(p1= =0)for(i=0;i

4、<500;i+)printf( "child%dn” ,i);else while(p2=fork()= =-1);If(p2= =0)for(i=0;i<500;i+)printf( "son%d n” ,i);elsefor(i=0;i<500;i+)printf( "daughter%d n” ,i);9roothetin -# cat fork.c# include<stdio.h>#include<stdlib.h>Int maln() int pl, p2, 1;while(pl-fork(J) “ -1);i

5、f(pl = 6)for(i=Q;i<5;i+)printf(*,child good adn",i|; else while!(p2=Tork() = -1);!T(p2 = 0)for(i-0;i<5;i+)printf1"son is a good boydXn",i); elsefor(i=0;i<5;i+4-printf("daughter is a good girl%dn",i); rootghebin一vim fork.croatghebinT# gccfork.c -o forkroothebinHfcf#

6、./forkchild good0child good1child good2child good3child goodason is a goadboyBson is a goodboylson is a goodboy2son is a goodboy3son is a goadbay4daughter isagoodgi rl0daughter isagoodgirl1daughter isagoodgi12daughter isagoodgi13daughter isagoodgir14运行的结果是如上图所示.首先创建进程p1,向子进程返回0,并 for语句循环输出child +i字符

7、串.又创建进程p2,向子进程返回0,输 出字符串son+i,同时向父进程返回子进程的 pid ,输出字符串duaghter +i ,各打印5次。3、编写程序创建进程树如图 (选做题)1和图2所示,在每个进程中显示当前进程识别码和父进程识别码。实验一代码及结果raothebin cat tree.c#include<stdlo.h>ki nclu(1o<5trll i b. h>int p h.pZ c,p3 d;pl b = fork(;lf-(pl b > Dprirtf("process father a:pid = *dnr,qptpid,);el

8、se ifpl b = G) (printf t*child b:pid - father is dXn*y getpid (), getppid J j;<p2 c = 0jprintf("child c:pld =.father Is %dnw tgetpid(k getppidO;pl d = to rk();if(p3 jd = 0)printf tchlLd d; pid = %d - father is dn111getpldf) - ijetppidO !;process father a:pld 4571图一进程树实验结果及相关代码eg勘Mincat trq”,

9、工tinclu desftdlib T hoLnl udlnf) int uL U,cl :,。上也|>2_。; pl b = fork(): i.f ipl b k g(priMtcchild 馆:pir) - Mi t&tner is Myn-1 tpid(,',getrplclt);plc w tork(if(p) c = 0)print T('child c邛id - *d,Tatlwr is <tdn",getpid( .getppidC |);alM if (pl b>9) (printr( -rather 1, Xin"

10、; .getpLdi);p2_d = forkO;If (p2 dl « 91 (prlntfi -child dipid - *f*tXr is MMT76p1口。,qqtgWUh p3 e = fork!J;IfTpa e = Gprintf (child enpid = M. father is MJ'Me呼tpidf I 追讨口口idi H ;rootghebin ./tree2:hild b :pid 5152. father is 5151 rhild crpid = 5153Pfather 15 5152 rather is 5151= S1&4,fat

11、her is 51,S1 zhlld e:pld - 5155,father is 5154图二进程树实验结果及相关代码4、编写一段程序,使用系统调用fork()创建两个子进程,再用系统调用signal()让父进 程捕捉键盘上来的中断信号(即按ctrl+c键),当捕捉到中断信号后,父进程用系统调用 kill() 向两个子进程发出信号,子进程捕捉到信号后,分别输出下列信息后终止:Child process 1 is killed by parent!Child process 2 is killed by parent!父进程等待两个子进程终止后,输出以下信息后终止:Parent process

12、 is killed!参考程序#include<stdio.h>#include<signal.h>#include<unistd.h>#include<sys/types : .h>#include<sys/wait.h>int wait_mark;void waiting(),stop();void main()int p1, p2;signal(SIGINT,stop);while(p1=fork()=-1);if(p1>0)/*在父进程中*/while(p2=fork()=-1);If(p2>0)/*在父进程中*/

13、wait_mark=1;waiting(0);kill(p1,10);kill(p2,12);wait( );wait( );printf("parent process is killed!n");exit(0);else/* 在子进程2 中 */wait_mark=1;signal(12,stop);waiting();lockf(1,1,0);printf("child process 2 is killed by parent!n");lockf(1,0,0);exit(0);else/* 在子进程1 中 */wait_mark=1;signal

14、(10,stop);waiting();lockf(1,1,0);printf("child process 1 is killed by parent!n");lockf(1,0,0);exit(0);void waiting()while(wait_mark!=0);void stop()wait_mark=0;实验要求:、运行程序并分析结果。roothebin 桌 面# ./progressACchild process 1 is killed by parent I child process 2 Is killed by parent! parent proces

15、s is killed!rootghebin 桌面#中断后,父进程收到子进程退出状态,依次杀死子进程2子进程1,等待子进程完全退出后,杀死父进程、如果把signal(SIGINT,stop)放在号和号位置,结果会怎样并分析原因。roothebin 桌面# ./progress"child p rocess 2 is killed by parent! parent process is killed!roothebin 桌面#改变signal(SIGINT,stop) 的位置后发现,只是杀死了子进程 2和 父进程,而子进程1没有被杀死。是因为子进程1没有设置对信号的处 理操作,则键盘

16、中断时,仍然无法将其杀死。、该程序段前面部分用了两个wait(0),为什么?wait(0)是为了让前面的子进程完全执行完,才可以进行下面的操作。、该程序段中每个进程退出时都用了语句exit(0),为什么?首先是为了让进程退出,其次是向父进程返回退出时的状态;5、修改上面的程序,增加语句 signal(SIGINT,SIG_IGN) 和语句 signal(SIGQUIT,SIG_IGN),再 观察程序执行时屏幕上出现的现象,并分析其原因。参考程序# include<stdio.h># include<signal.h># include<unistd.h>ma

17、in() int pid1, pid2;int EndFlag=0;Pf1=0;Pf2=0;void IntDelete()kill(pid1,10);kill(pid2,12);EndFlag=1;void Int1()printf( “ child process 1 is killed by parent !n” );exit(0);void Int2()printf( “ child process 2 is killed by parent !n” );exit(0);main()int exitcode;signal(SIGINT,SIG_IGN);signal(SIGQUIT,SIG_IGN);while(pid1=fork()=-1);if(pid=0)signal(SIGUSER1,Int1);signal(SIGQUIT,SIG_IGN);pause();exit(0);elsewhile(pid2=fork()= =-1);if(pid2=0)signal(SIGUSER1,Int1);signal(SIGQUIT,SIG_IGN);pause();exit(0);elsesignal(SIGINT,IntDelete);waitpid(-1,&exitcode,0);prin

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论