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

下载本文档

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

文档简介

1、实验题目进程的创建小组合作否姓名班级学号一、实验目的1、了解进程的创建。2、了解进程间的调用以及实现。3、分析进程竞争资源的现象,学习解决互斥的方法。4'加深对进程概念的理解,认识并发执行的本质。二.实验环境Windows系统的计算机一台5安装了 Linux虚拟机三、实验内容与步骤1、fork ()系统调用的使用例子程序代码:#in clude<stdio.h>#in clude<sys/types.h>#in clude< uni std.h>int glob=3;int main( void)pid_t pid;i nt loc=3;prin t

2、f("before fork();glod=%d,loc=%d.n",glob,loc);if(pid=fork()<0)prin tf(nfork() error. nH);exit(O);else if(pid=O)glob+;loc-;prin tf(Hchild process cha nges glob and loc: n ”); elsewait(O);prin tf(Hpare nt process does n't cha nge the glob and loc:rf); prin tf("glob=%d,loc=%dnH,glo

3、b,loc);exit(O);运行结果:制助阻I tn®编辑终端 转到峻 rooiAloca Ihos i ml did # gee f i lei .c -o f i lei ,exe叵rooiSloca I host mldnj# Vf i lei ,exebe fore fork( )lod=3,loc=S.ch i Id process cti3ngeg lob And loc:pa reii t prove ss Juesn I change t he g lob a加loc ; g lob=4 f loc=2卩 a rent proce ss doc *t change

4、the glob and loc : g lob=3Joc=3toot: Oloca Ihos I mldrril # |2、理解vofork ()调用:程序代码:#in clude<stdio.h>#in clude<sys/types.h>#in clude< uni std.h>int glob=3;int main( void)pid_t pid;int loc=3;if(pid=vfork()<0)prin tf("vfork() error nH);exit(O);else if(pid=O)glob+;loc-;prin tf(

5、Mchild process cha nges the glob and loc rT);exit(O);elseprintf ("pare nt process does n't cha nge the glob and locn n);prin tf("glob=%d,val=%dn",glob,loc);运行结果: rooIhcs t mJdnrJ# Vf i Ie2 + exech i Id process chdii|*e s ih 亡 g lob and Joe parent proces$ doesn 11 change the glob a

6、nd loc g lob =4 ts=2 roo I ?'loca Ihos t nil dir#3、给进程指定一个新的运行程序的函数0.程序代码:prin tel .c 代码:#in clude<stdio.h>int main (i nt argc5char * argv)int n;char * * ptr;exter n char * * environ;for(n=0;nv argc ;n+)prin tf(,'argv%d:%sn',J n,argv nJ);for(ptr=e nvir on; * ptr!=O;ptr+)prin tf(,%s

7、n,J* ptr);exit(O);file4.c代码如下:#in clude<stdio.h>#in clude<sys/types.h>#in clude< uni std.h>#in clude<sys/wait.h>char * en vJist=,USER=rootn,PATH=/root/,',NULL;int mai n()pid t pid;if(pid=fork()<0)prin tf("fork error!n"); exit(O);else if(pid=O)if(execle(,7root

8、/pri nt1 M;fpri nt1l,;,arg1f,;larg2,(char *)0,e nv_list)<0) prin tf(Hexecle error!nM);exit(O); if(waitpid(pid,NULL,O)<O) prin tf("WAIT ERROR!nH);exit(O);if(pid=fork()<0)prin tf(*'fork error!* rT); exit(O);else if(pid=O)if(execlp(Mpri ntT,”pri ntl'V'argVchar *)0)<0) prin

9、tf(nexecle error!nn); exit(O);exiO);运行结果:rooiAlocalbost ml did#./ f i Ie4ai g0:p 17 intiargi ; argliirg2Arg2IEER= roo I PAThk/roo i丫|£roai®IQa I ho si mldidfi丄4、进程终止函数exit() °程序代码:#in clude<stdio.h>main ()prin tf("this is a exit system call! n ”); exit(O);prin tf("this

10、 sente nee n ever be displaye n:n ”);#in clude<stdio.h>main ()prin tf("this is a _exit_test system call! n ”);prin tf(”c ontent in buffer*1); exit(O);运行结果:r qo t o ca I ho K t mldnil Sger ex i t_te s I, c -o (?xit_te£i+exc rootAloca Ihos I mldrril# ./ex i i_tes L .exe1 h i i s ex i 1

11、 sy s Lem ca 1111roo ttii'loca Iho s i mldnO |rootAlocaLhos t J oomldnij#gcc_exit_testc-o Jtruldni)#L/_eKit. tes L eci Moca I hoa. ttest.extth,s is a -cxHJestliyitemol11|AAoj/AloAc3 Ihos tJJ mldniTfl5、wait()函数和sleep(函数0程序代码:#in clude<stdio.h> main ()int pidl;if(pid1=fork()|if(fork()prin tf

12、(”pare nt's con text,n“);prin tf("pare nt's wait ing the childl term in ate,nH); wait(O);prin tf("pare nt's wait ing the child2 term in ate,n"); wait(O); prin tf("pare nt term in ates,n”); exit(O);厂else而Ttf(”child2's con text,n“);sleep(5);prin tf("child2 term

13、i nates,n'*); exit(O);elseif(pid1=O)prin tf("child1's con text,n");sleep(10);prin tf("child1 term in ates,n“);exit(O);运行结果:roo t Aloca Ihos L nnJdnil# . /f i left + eiech i Id 1 s context,ch i Id2fs context.parenfs conj 亡忙 t,parent s wi 11 i ng the child 】t© r Tuna le fch

14、 i Id? termina tcs tparents ua i i ing 1 he ch i Id2 Is r iniiu le tcti i Id 1 termina les *parent termina les »rootlocalhust nidnr6、编写一段程序,父进程使用fork()创建两个子进程,利用输出函数put char父进程显示字符”a”,两个子进程分别显示“ b”和“ c”。程序代码:#in clude<stdio.h>#in clude<sys/types.h>#in clude< uni std.h>int mai

15、n()int pid;if(pid=fork()if(fork()prin tf("pare nt process is n ”);putchar(TV);prin tf(”n”);elseprin tf("child2 process is n ”);putchar(C);prin tf(nnn);elseif(pid=O)prin tf("child1 process is n ”);putcharfB1); prin tf(nnH);运行结果:roajAfocalbpiit : /m 111/ldm骗描终端转到ffiUljCHiroctAlocA (host

16、 mldinl # v im kaosh i *roo LAloca Iho5 t ml did # v 初 kaosh i1 oo LiiAloca I host mldiTi # l /kaosh i xCiechildl process isBchild2 proceiscrfnl prace ss i sArootlocalhost mldni#,/kao*hi.exe四、实验过程与分析1在1例子中,调用正确完成时,给父进程返回的是被创建子逬程标识,给子进程 自己返回的是0;创建失败时,返回给父进程的是。2、在2例子中,vfork ()调用后需要注意两点:(1) 子进程先运行,父进程挂起。子进程调用exec()或exi t ()之后 父进程的执行顺序不再有限制。(2) 子进程在调用exec()或exit ()之前。父进程被激活,就会造成死锁。3、在6例子中,上述程序是父进程先创建一个子进程,若成功,再创建另一个子 进程,之后三个进程并发执行。究竟谁先执行,是随机的。所以执行结果有多重 种。五、实验总结1、一个进程调用exec ()函数来运行一个新程序。之后该进程的代码段、数据段和堆栈段就被新程序的所代替。新程序从自己的main()函数开始 执行。exec ()函数有6种不同的形式,任何一个都可以完成exec ()的功能,只 是调用参数不同。2

温馨提示

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

评论

0/150

提交评论