欢迎来到天天文库
浏览记录
ID:26804460
大小:130.50 KB
页数:8页
时间:2018-11-29
《操作系统实验报告[进程的创建]》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、WORD格式整理实验题目进程的创建小组合作否姓名班级学号一、实验目的1、了解进程的创建。2、了解进程间的调用以及实现。3、分析进程竞争资源的现象,学习解决互斥的方法。4、加深对进程概念的理解,认识并发执行的本质。二.实验环境Windows系统的计算机一台,安装了Linux虚拟机三、实验内容与步骤1、fork()系统调用的使用例子程序代码:#include#include#includeintglob=3;intmain(void){pid_tpid;intloc=3;pri
2、ntf("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:");printf("glob=%d,loc=
3、%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("childprocesschangestheglobandloc
4、n");exit(0);}elseprintf("parentprocessdoesn'tchangetheglobandloc");printf("glob=%d,val=%d",glob,loc);专业知识分享WORD格式整理}运行结果:3、给进程指定一个新的运行程序的函数exec().程序代码:printe1.c代码:#includeintmain(intargc,char*argv[]){intn;char**ptr;externchar**environ;for(n=0;n5、tf("argv[%d]:%s",n,argv[n]);for(ptr=environ;*ptr!=0;ptr++)printf("%s",*ptr);exit(0);}file4.c代码如下:#include#include#include#includechar*env_list[]={"USER=root","PATH=/root/",NULL};intmain(){pid_tpid;if((pid=fork())<0){printf(6、"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!");exit(0);if((pid=fork())<0){printf("forkerror!");exit(0);}elsei7、f(pid==0){if(execlp("print1","print1","arg1",(char*)0)<0)printf("execleerror!");exit(0);}exit(0);}运行结果:4、进程终止函数exit()。程序代码:#includemain(){printf("thisisaexitsystemcall!!");exit(0);printf("thissentenceneverbedisplayen:");}#includemain(){printf("thi8、sisa_exit_testsystemcall!");printf("contentinbuffer");exit(0);}专业知识分享WORD格式整理运行结果:5、wait()函数和sleep()函
5、tf("argv[%d]:%s",n,argv[n]);for(ptr=environ;*ptr!=0;ptr++)printf("%s",*ptr);exit(0);}file4.c代码如下:#include#include#include#includechar*env_list[]={"USER=root","PATH=/root/",NULL};intmain(){pid_tpid;if((pid=fork())<0){printf(
6、"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!");exit(0);if((pid=fork())<0){printf("forkerror!");exit(0);}elsei
7、f(pid==0){if(execlp("print1","print1","arg1",(char*)0)<0)printf("execleerror!");exit(0);}exit(0);}运行结果:4、进程终止函数exit()。程序代码:#includemain(){printf("thisisaexitsystemcall!!");exit(0);printf("thissentenceneverbedisplayen:");}#includemain(){printf("thi
8、sisa_exit_testsystemcall!");printf("contentinbuffer");exit(0);}专业知识分享WORD格式整理运行结果:5、wait()函数和sleep()函
此文档下载收益归作者所有