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

下载本文档

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

文档简介

1、精选优质文档-倾情为你奉上深 圳 大 学 实 验 报 告 课程名称: 操作系统 实验项目名称: 进程控制 学院: 软件学院 专业: 软件工程 指导教师: 梁正平 报告人: 文 成 学号: 班级: 2 实验时间: 2013. 03. 31 实验报告提交时间: 2013. 04. 24 教务处制一. 实验目的通过进程的创建、撤销和运行加深对进程概念和进程并发执行的理解,明确进程与程序之间的区别。二. 实验要求1 实现对进程生命周期全过程的管理,包括进程的创建、撤销、执行、阻塞、唤醒、挂起、激活等。2 实现对多个进程并发执行的管理。三. 方法、步骤1、 函数说明l fork(建立一个新的进程)定义函

2、数 pid_t fork(void);函数说明 fork()会产生一个新的子进程,其子进程会复制父进程的数据与堆栈空间,并继承父进程的用户代码,组代码,环境变量、已打开的文件代码、工作目录和资源限制等。 返回值 如果fork()成功则在父进程会返回新建立的子进程代码(PID),而在新建立的子进程中则返回0。如果fork 失败则直接返回-1,失败原因存于errno中。l waitpid(等待子进程中断或结束)定义函数 pid_t waitpid(pid_t pid,int * status,int options);函数调用 waitpid(pid, NULL, 0);函数说明 waitpid(

3、)会暂时停止目前进程的执行,直到有信号来到或子进程结束。如果在调用waitpid()时子进程已经结束,则wait()会立即返回子进程结束状态值。子进程的结束状态值会由参数status返回,而子进程的进程识别码也会一快返回。如果不在意结束状态值,则参数status可以设成NULL。参数pid为欲等待的子进程识别码,其他数值意义如下:pid<-1 等待进程组识别码为pid绝对值的任何子进程。pid=-1 等待任何子进程,相当于wait()。pid=0 等待进程组识别码与目前进程相同的任何子进程。pid>0 等待任何子进程识别码为pid的子进程。返回值 如果执行成功则返回子进程识别码(P

4、ID),如果有错误发生则返回-1。失败原因存于errnol getpid(取得进程识别码) 定义函数 pid_t getpid(void);函数说明 getpid()用来取得目前进程的进程识别码,许多程序利用取到的此值来建立临时文件,以避免临时文件相同带来的问题。返回值 目前进程的进程识别码l exit(正常结束进程) 定义函数 void exit(int status);函数说明 exit()用来正常终结目前进程的执行,并把参数status返回给父进程,而进程所有的缓冲区数据会自动写回并关闭未关闭的文件。l execl(执行文件)定义函数 int execl(const char * pat

5、h,const char * arg,.);函数说明 execl()用来执行参数path字符串所代表的文件路径,接下来的参数代表执行该文件时传递过去的argv(0)、argv1,最后一个参数必须用空指针(NULL)作结束。返回值 如果执行成功则函数不会返回,执行失败则直接返回-1,失败原因存于errno中。调用ls命令范例: execl("/bin/ls", "/bin/ls", "-l" , "/etc", NULL);启动VC+等开发平台,创建我们所需的程序文件并保存到CYGWIN的用户文件夹下四. 实验过程及

6、内容实验指导例程2:#include <unistd.h>#include <stdarg.h>#include <time.h>#include <sys/types.h>#include <sys/wait.h>#include <stdio.h>#include <stdlib.h>int tprintf (const char*fmt,.);int main(void)int i=0,j=0;pid_t pid;printf("Hello from Parent Process,PID is

7、%d.n",getpid();pid=fork();printf("process %d | My parent PID is %d.n",getpid(),getppid();sleep(1);if(pid=0)/子进程执行sleep(1);for(i=0;i<3;i+)printf("Hello from THE child process %d.%d timesn",getpid(),i+1);sleep(1);else if(pid!=-1)/父进程tprintf("Parent forked one child pro

8、cess-%d.n",pid); pid=fork(); printf("process %d | My parent PID is %d.n",getpid(),getppid(); sleep(1); if(pid=0)sleep(1);for(i=0;i<3;i+)printf("Hello from child process %d.%d timesn",getpid(),i+1);sleep(1);else if(pid!=-1)tprintf("Parent forked one child process-%d.n

9、",pid); tprintf("Parent is waiting for child to exit.n");waitpid(pid,NULL,0);tprintf("child process has exited.n");tprintf("parent had exited.n");else tprintf("everything was done whitout error.n");return 0;int tprintf(const char*fmt,.)va_list args;struct

10、 tm *tstruct;time_t tsec;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);return vprintf(fmt,args);模仿例程,编写一段程序(创建两个子进程):#include <unistd.h>#include <stdarg.h>#include

11、<time.h>#include <sys/types.h>#include <sys/wait.h>#include <stdio.h>#include <stdlib.h>int tprintf(const char *fmt,.);int main(void)pid_t pid1,pid2;pid1=fork();if(pid1 = 0) /子进程sleep(5);tprintf("Hello from Child Process1!n");tprintf("my parent is %d.n&qu

12、ot;,getppid();tprintf("I am calling exec.n");execl("/bin/ps","-a",NULL);/ execl("/bin/ls","-l","/etc",NULL);tprintf("You should never see this because the child is alredy gone.n");else if(pid1 != -1) /父进程 pid2=fork(); /用父进程创建第二个子进

13、程if(pid2 = 0)sleep(10); /时间设长于上面的睡眠时间,保证两个进程的有序输出tprintf("Hello from Child Process1!n");tprintf("my parent is %d.n",getppid();tprintf("I am calling exec.n");execl("/bin/ps","-a",NULL);/ execl("/bin/ls","-l","/etc",NULL);

14、tprintf("You should never see this because the child is alredy gone.n"); else if(pid2 != -1)tprintf("Hello from Parent,pid %d.n",getpid();sleep(1);tprintf("Parent forked process1 %d.n",pid1);sleep(1);tprintf("Parent forked process2 %d.n",pid2);sleep(1);tprintf

15、("Patent is waiting for child to exit.n");waitpid(pid1, NULL,0);waitpid(pid2, NULL,0);tprintf("Parent had exited.n"); elsetprintf("Everything was done without error.n");return 0;int tprintf(const char *fmt,.)va_list args;struct tm *tstruct;time_t tsec;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);return vprintf(fmt,args);运行结果:分析:运用例3的代码,加上一个子进程的嵌套来实现两个子进程的创建,

温馨提示

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

评论

0/150

提交评论