资源描述:
《操作系统实验报告(进程的创建)》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、WORD资料下载可编辑实验题目进程的创建小组合作否姓名班级学号一、实验目的1、了解进程的创建。2、了解进程间的调用以及实现。3、分析进程竞争资源的现象,学习解决互斥的方法。4、加深对进程概念的理解,认识并发执行的本质。二.实验环境Windows系统的计算机一台,安装了Linux虚拟机三、实验内容与步骤1、fork()系统调用的使用例子程序代码:#include#include#includeintglob=3;intmain(void){pid_tpid;i
2、ntloc=3;printf("beforefork();glod=%d,loc=%d.",glob,loc);if((pid=fork())<0){printf("fork()error.");exit(0);}elseif(pid==0){glob++;loc--;printf("childprocesschangesglobandloc:");}else技术资料专业分享WORD资料下载可编辑wait(0);printf("parentprocessdoesn'tchangetheglobandloc:
3、n");printf("glob=%d,loc=%d",glob,loc);exit(0);}运行结果:2、理解vofork()调用:程序代码:#include#include#includeintglob=3;intmain(void){pid_tpid;intloc=3;if((pid=vfork())<0){printf("vfork()error");exit(0);}elseif(pid==0){glob++;loc--;printf("c
4、hildprocesschangestheglobandloc");exit(0);}elseprintf("parentprocessdoesn'tchangetheglobandloc");printf("glob=%d,val=%d",glob,loc);技术资料专业分享WORD资料下载可编辑}运行结果:3、给进程指定一个新的运行程序的函数exec().程序代码:printe1.c代码:#includeintmain(intargc,char*argv[]){intn;char**pt
5、r;externchar**environ;for(n=0;n#include#include#includechar*env_list[]={"USER=root","PATH=/root/
6、",NULL};intmain(){pid_tpid;if((pid=fork())<0){printf("forkerror!");exit(0);}elseif(pid==0){if(execle("/root/print1","print1","arg1","arg2",(char*)0,env_list)<0)printf("execleerror!");技术资料专业分享WORD资料下载可编辑exit(0);}if((waitpid(pid,NULL,0))<0)printf("WAITERROR!"
7、);exit(0);if((pid=fork())<0){printf("forkerror!");exit(0);}elseif(pid==0){if(execlp("print1","print1","arg1",(char*)0)<0)printf("execleerror!");exit(0);}exit(0);}运行结果:4、进程终止函数exit()。程序代码:#includemain(){printf("thisisaexitsystemcall!!");exit(0);prin
8、tf("thissentenceneverbedisplayen:");}#includemain(){printf("thisisa_exit_testsystemcall!");printf("contentinbuffer");exit(0);}技术资料专业分享WORD资料下载可编辑运