欢迎来到天天文库
浏览记录
ID:48298465
大小:140.00 KB
页数:9页
时间:2020-01-16
《操作系统实验报告(进程的创建)》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、实验题目进程的创建小组合作否姓名班级学号一、实验目的1、了解进程的创建。2、了解进程间的调用以及实现。3、分析进程竞争资源的现象,学习解决互斥的方法。4、加深对进程概念的理解,认识并发执行的本质。二.实验环境Windows系统的计算机一台,安装了Linux虚拟机三、实验内容与步骤1、fork()系统调用的使用例子程序代码:#include#include#includeintglob=3;intmain(void){pid_tpid;intloc=3;printf("beforefork();glod=%
2、d,loc=%d.",glob,loc);if((pid=fork())<0){printf("fork()error.");exit(0);}elseif(pid==0){glob++;loc--;printf("childprocesschangesglobandloc:");}elsewait(0);printf("parentprocessdoesn'tchangetheglobandloc:");printf("glob=%d,loc=%d",glob,loc);exit(0);}运行结果:2、理解vofork()调用:程序代码:#inc
3、lude#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");exit(0);}elseprintf("parentprocessdoesn'tchangetheglobandloc");p
4、rintf("glob=%d,val=%d",glob,loc);}运行结果:3、给进程指定一个新的运行程序的函数exec().程序代码:printe1.c代码:#includeintmain(intargc,char*argv[]){intn;char**ptr;externchar**environ;for(n=0;n5、:#include#include#include#includechar*env_list[]={"USER=root","PATH=/root/",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(6、"execleerror!");exit(0);}if((waitpid(pid,NULL,0))<0)printf("WAITERROR!");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()。程序代码:#includemain7、(){printf("thisisaexitsystemcall!!");exit(0);printf("thissentenceneverbedisplayen:");}#includemain(){printf("thisisa_exit_testsystemcall!");printf("contentinbuffer");exit(0);}运行结果:5、wait()函数和sleep()函数。程序代码:#includemain(){intpid1;if(pid1=fork()){if(fork()
5、:#include#include#include#includechar*env_list[]={"USER=root","PATH=/root/",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(
6、"execleerror!");exit(0);}if((waitpid(pid,NULL,0))<0)printf("WAITERROR!");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
7、(){printf("thisisaexitsystemcall!!");exit(0);printf("thissentenceneverbedisplayen:");}#includemain(){printf("thisisa_exit_testsystemcall!");printf("contentinbuffer");exit(0);}运行结果:5、wait()函数和sleep()函数。程序代码:#includemain(){intpid1;if(pid1=fork()){if(fork()
此文档下载收益归作者所有