操作系统实验报告1.doc
- 文档编号:2730125
- 上传时间:2023-05-04
- 格式:DOC
- 页数:8
- 大小:152.50KB
操作系统实验报告1.doc
《操作系统实验报告1.doc》由会员分享,可在线阅读,更多相关《操作系统实验报告1.doc(8页珍藏版)》请在冰点文库上搜索。
深圳大学实验报告
课程名称:
操作系统
实验项目名称:
进程控制
学院:
软件学院
专业:
软件工程
指导教师:
梁正平
报告人:
文成学号:
2011150259 班级:
2
实验时间:
2013.03.31
实验报告提交时间:
2013.04.24
教务处制
一.实验目的
通过进程的创建、撤销和运行加深对进程概念和进程并发执行的理解,明确进程与程序之间的区别。
二.实验要求
1.实现对进程生命周期全过程的管理,包括进程的创建、撤销、执行、阻塞、唤醒、挂起、激活等。
2.实现对多个进程并发执行的管理。
三.方法、步骤
1、函数说明
lfork(建立一个新的进程)
定义函数pid_tfork(void);
函数说明fork()会产生一个新的子进程,其子进程会复制父进程的数据与堆栈空间,并继承父进程的用户代码,组代码,环境变量、已打开的文件代码、工作目录和资源限制等。
返回值如果fork()成功则在父进程会返回新建立的子进程代码(PID),而在新建立的子进程中则返回0。
如果fork失败则直接返回-1,失败原因存于errno中。
lwaitpid(等待子进程中断或结束)
定义函数pid_twaitpid(pid_tpid,int*status,intoptions);
函数调用waitpid(pid,NULL,0);
函数说明waitpid()会暂时停止目前进程的执行,直到有信号来到或子进程结束。
如果在调用waitpid()时子进程已经结束,则wait()会立即返回子进程结束状态值。
子进程的结束状态值会由参数status返回,而子进程的进程识别码也会一快返回。
如果不在意结束状态值,则参数status可以设成NULL。
参数pid为欲等待的子进程识别码,其他数值意义如下:
pid<-1等待进程组识别码为pid绝对值的任何子进程。
pid=-1等待任何子进程,相当于wait()。
pid=0等待进程组识别码与目前进程相同的任何子进程。
pid>0等待任何子进程识别码为pid的子进程。
返回值如果执行成功则返回子进程识别码(PID),如果有错误发生则返回-1。
失败原因存于errno
lgetpid(取得进程识别码)
定义函数pid_tgetpid(void);
函数说明getpid()用来取得目前进程的进程识别码,许多程序利用取到的此值来建立临时文件,以避免临时文件相同带来的问题。
返回值目前进程的进程识别码
lexit(正常结束进程)
定义函数voidexit(intstatus);
函数说明exit()用来正常终结目前进程的执行,并把参数status返回给父进程,而进程所有的缓冲区数据会自动写回并关闭未关闭的文件。
lexecl(执行文件)
定义函数intexecl(constchar*path,constchar*arg,....);
函数说明execl()用来执行参数path字符串所代表的文件路径,接下来的参数代表执行该文件时传递过去的argv(0)、argv[1]……,最后一个参数必须用空指针(NULL)作结束。
返回值如果执行成功则函数不会返回,执行失败则直接返回-1,失败原因存于errno中。
调用ls命令范例:
execl("/bin/ls","/bin/ls","-l","/etc",NULL);
启动VC++等开发平台,创建我们所需的程序文件并保存到CYGWIN的用户文件夹下
四.实验过程及内容
实验指导例程2:
#include
#include
#include
#include
#include
#include
#include
inttprintf(constchar*fmt,...);
intmain(void)
{
inti=0,j=0;
pid_tpid;
printf("HellofromParentProcess,PIDis%d.\n",getpid());
pid=fork();
printf("process%d|MyparentPIDis%d.\n",getpid(),getppid());
sleep
(1);
if(pid==0)//子进程执行
{
sleep
(1);
for(i=0;i<3;i++)
{
printf("HellofromTHEchildprocess%d.%dtimes\n",getpid(),i+1);
sleep
(1);
}
}
elseif(pid!
=-1)//父进程
{
tprintf("Parentforkedonechildprocess--%d.\n",pid);
pid=fork();
printf("process%d|MyparentPIDis%d.\n",getpid(),getppid());
sleep
(1);
if(pid==0)
{
sleep
(1);
for(i=0;i<3;i++)
{
printf("Hellofromchildprocess%d.%dtimes\n",getpid(),i+1);
sleep
(1);
}
}
elseif(pid!
=-1)
{
tprintf("Parentforkedonechildprocess--%d.\n",pid);
tprintf("Parentiswaitingforchildtoexit.\n");
waitpid(pid,NULL,0);
tprintf("childprocesshasexited.\n");
tprintf("parenthadexited.\n");
}
}
elsetprintf("everythingwasdonewhitouterror.\n");
return0;
}
inttprintf(constchar*fmt,...)
{
va_listargs;
structtm*tstruct;
time_ttsec;
tsec=time(NULL);
tstruct=localtime(&tsec);
printf("%02d:
%02d:
%02d:
%5d|",tstruct->tm_hour,tstruct->tm_min,tstruct->tm_sec,getpid());
va_start(args,fmt);
returnvprintf(fmt,args);
}
模仿例程,编写一段程序(创建两个子进程):
#include
#include
#include
#include
#include
#include
#include
inttprintf(constchar*fmt,...);
intmain(void)
{
pid_tpid1,pid2;
pid1=fork();
if(pid1==0)//子进程
{
sleep(5);
tprintf("HellofromChildProcess1!
\n");
tprintf("myparentis%d.\n",getppid());
tprintf("Iamcallingexec.\n");
execl("/bin/ps","-a",NULL);
//execl("/bin/ls","-l","/etc",NULL);
tprintf("Youshouldneverseethisbecausethechildisalredygone.\n");
}
elseif(pid1!
=-1)//父进程
{
pid2=fork();//用父进程创建第二个子进程
if(pid2==0)
{
sleep(10);//时间设长于上面的睡眠时间,保证两个进程的有序输出
tprintf("HellofromChildProcess1!
\n");
tprintf("myparentis%d.\n",getppid());
tprintf("Iamcallingexec.\n");
execl("/bin/ps","-a",NULL);
//execl("/bin/ls","-l","/etc",NULL);
tprintf("Youshouldneverseethisbecausethechildisalredygone.\n");
}
elseif(pid2!
=-1)
{
tprintf("HellofromParent,pid%d.\n",getpid());
sleep
(1);
tprintf("Parentforkedprocess1%d.\n",pid1);
sleep
(1);
tprintf("Parentforkedprocess2%d.\n",pid2);
sleep
(1);
tprintf("Patentiswaitingforchildtoexit.\n");
waitpid(pid1,NULL,0);
waitpid(pid2,NULL,0);
tprintf("Parenthadexited.\n");
}
}
else
tprintf("Everythingwasdonewithouterror.\n");
return0;
}
inttprintf(constchar*fmt,...)
{
va_listargs;
structtm*tstruct;
time_ttsec;
tsec=time(NULL);
tstruct=localtime(&tsec);
printf("%02d:
%02d:
%02d:
%5d|",tstruct->tm_hour,tstruct->tm_min,tstruct->tm_sec,getpid());
va_start(args,fmt);
returnvprintf(fmt,args);
}
运行结果:
分析:
运用例3的代码,加上一个子进程的嵌套来实现两个子进程的创建,但是代码重用率较高,并且不适于创建多个进程。
五.实验结论
多个进程可以并发运行,父进程可以创建子进程。
这一次的实验是通过参考实验书的指导做出来的,有一些语句还是不是很清楚,一些语句的实际意义也不是很明白,实验的一些更高要求没做出来,通过以后的实验我相信可以更加清楚地了解这些创建进程的函数。
指导教师批阅意见:
成绩评定:
指导教师签字:
年月日
备注:
注:
1、报告内的项目或内容设置,可根据实际情况加以调整和补充。
2、教师批改学生实验报告时间应在学生提交实验报告时间后10日内。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 操作系统 实验 报告