实验一进程创建模拟

上传人:仙*** 文档编号:37571133 上传时间:2021-11-03 格式:DOC 页数:8 大小:100KB
收藏 版权申诉 举报 下载
实验一进程创建模拟_第1页
第1页 / 共8页
实验一进程创建模拟_第2页
第2页 / 共8页
实验一进程创建模拟_第3页
第3页 / 共8页
资源描述:

《实验一进程创建模拟》由会员分享,可在线阅读,更多相关《实验一进程创建模拟(8页珍藏版)》请在装配图网上搜索。

1、实验一 进程创建模拟实验学时:2实验类型:验证实验要求:必修一、实验目的1) 理解进程创建相关理论;2) 掌握进程创建方法;3) 掌握进程相关数据结构。二、实验内容本实验针对操作系统中进程创建相关理论进行实验。要求实验者输入实验指导书提供的代码并进行测试。代码简化了进程创建的多个步骤和内容。进程的树形结构采用广义二叉树的方式进行存储。三、实验原理1)进程控制块为了描述和控制进程的运行,系统为每个进程定义了一个进程控制块(PCB),它是进程实体的一部分,是操作系统管理进程最重要的数据结构。其主要包含四类信息:(1) 进程标识符它唯一地标识一个进程。通常包括进程号 pid,父进程号 ppid 和用

2、户号 uid。(2) 处理机状态处理器的状态通常由处理机的各种寄存器中的内容组成。PCB 存放中断(阻塞,挂起)时的各寄存器值,当该进程重新执行时,可以从断点处恢复。主要包括: a) 通用寄存器; b) 指令计数器; c) 程序状态字 PSW; d) 用户栈指针。(3) 进程调度信息 a) 进程状态; b) 进程优先级(用于描述优先使用 cpu 级别的一个整数,高优先级的进程先得到cpu,通常情况下,优先值越小优先级越高); c) 其它信息(等待时间、总执行时间等); d) 事件(等待原因)。(4) 进程控制信息 a) 程序和数据的地址(程序在内存和外存中的首址); b) 进程同步和通信机制;

3、 c) 资源列表(进程除 CPU 以外的所有资源); d) 链接指针(进程队列中指向下一个进程的 PCB 首址)。2) 进程创建流程 (1) 申请空白 PCB 为新进程申请获得唯一的数字标识符,并从 PCB 集合中索取一个空白 PCB。如果无空白PCB,可以创建一个新的 PCB。在本实验中,每次动态创建 PCB。 (2) 为新进程分配资源 为新进程分配内存空间和栈空间。 (3) 初始化进程控制块 a) 初始化标识信息; b) 初始化处理机状态信息; c) 初始化处理机控制信息。 (4) 将新进程插入就绪队列P1P2P3P4P5P6P7P8P9P10P11P12 3) 进程树 图 1-1 进程树

4、 进程树用于描述进程家族关系,如图 1-1 中可以看出,进程 P1 创建了进程 P2、P3、P4、P5,而 P2 又创建了 P6、P7、P8 。在进程创建过程中,需要对每一个新增加的进程加入到进程树中,有了清晰的父子关系,可以使资源继承或进程删除等操作变得很方便。4) 进程总链它是一个 PCB 链表,每一个新创建的进程必须把其 PCB 放入总链中,该总链可以对破坏的进程树进行修复,也方便 PCB 查找。四、程序清单1.basic.h 文件#ifndef basic_h#include#include#include#define basic_hchar *errormsg256;/proces

5、s control blockstruct pcb int pid;/process id int ppid;/parent process id int prio;/priority int state;/state int lasttime;/last execute time int tottime; /totle execute time;/process nodestruct pnode pcb *node; pnode *sub; pnode *brother; pnode *next;/信号量struct semphore char name5; /名称 int count;/计

6、数值 int curpid;/当前进程 id pnode *wlist; /等待链表;#define geterror(eno) printf(%sn,errormsgeno) void initerror() errormsg0 = (char *)malloc(20); errormsg0=error command!; errormsg1 = (char *)malloc(20); errormsg1=error parameter!;/get a substring in string schar * substr(char *s,int start,int end) char *s1

7、; int len = strlen(s); if(start=len | startend) return NULL; s1=(char *)malloc(end-start+2); for(int i=0;i=end-start;i+) s1i = si+start; s1i=0; return s1;/find the location of c in string sint instr(char *s,char c) unsigned i; for(i=0;istrlen(s);i+) if(si=c) return i; return -1;/change the string to

8、 array dataint * strtoarray(char *s) int *a,count,x1; unsigned int i; char c, *s1,*s2; if(!s) printf(string cant be null!n); return NULL; count=0; s1=s; for(i=0;istrlen(s1);i+) if(s1i=,) count+; count+; a = (int *)malloc(count); c=,; for(i=0;i=0) s2=substr(s1,0,x1-1);else s2=s1;ai=atoi(s2);if(c=,) s

9、1=substr(s1,x1+1,strlen(s1)-1); return a;#endif2、源程序#include basic.hpnode *proot;/system process tree rootpnode *plink;/system process link head/create processint createpc(int *para) /add your code here pnode *p,*p1,*pp; int pflag; pflag=0; for(p=plink;p;p=p-next) if(p-node-pid = para0) /check if th

10、is pid is already exist printf(pid %d is already exist!n,para0); return -1; if(p-node-pid = para1) /find parent pcb pflag=1; pp = p; if(!pflag) printf(parent id %d is not exist!n,para1); return -2; /init new pcbp1 = new pnode;p1-node=new pcb;p1-node-pid = para0;p1-node-ppid = para1;p1-node-prio = pa

11、ra2;p1-sub=NULL;p1-next=NULL;p1-brother=NULL;/add to process treeif(!pp-sub) pp-sub=p1;else for(p=pp-sub;p-brother;p=p-brother); p-brother=p1;/ add to process linkfor(p=plink;p-next;p=p-next);p-next=p1;return 0;/show process detailvoid showdetail() /add your code here pnode *p,*p1; p=plink; for(;p;)

12、 /print all pcb info printf(%d(prio %d):,p-node-pid,p-node-prio); p1 = p-sub; for(;p1;) /print sub pcb printf(%d(prio %d),p1-node-pid,p1-node-prio); p1 = p1-brother; printf(n); p = p-next; printf(n);/dont changevoid main() initerror(); short cflag,pflag; char cmdstr32; proot = new pnode; proot-node=

13、new pcb; proot-node-pid=0; proot-node-ppid=-1; proot-node-prio=0; proot-next=NULL; proot-sub=NULL; proot-brother=NULL; plink=proot; for(;) cflag=0; pflag=0; printf(cmd:); scanf(%s,cmdstr); if(!strcmp(cmdstr,exit) /exit the program break; if(!strcmp(cmdstr,showdetail) cflag = 1;pflag = 1;showdetail()

14、; else int *para;char *s,*s1;s = strstr(cmdstr,createpc); /create process if(s) cflag=1; para = (int *)malloc(3); /getparameter s1 = substr(s,instr(s,()+1,strlen(s)-2); /get param string para=strtoarray(s1);/get parameter createpc(para); /create process pflag=1; if(!cflag) geterror(0); else if(!pfla

15、g) geterror(1); 五、实验步骤输入实验提供的代码后,可以输入 createpc 命令创建进程,输入 showdetail 显示每个进程及其子进程的信息,测试命令解释如下:1) createpc 创建进程命令。 参数: 1、 pid(进程 id) 2 、ppid(父进程 id)3、prio(优先级)。 示例:createpc(1,0,1) 。创建一个进程,其进程号为 1,父进程号为 0,优先级为 1.createpc(2,1,2) 。创建一个进程,其进程号为 2,父进程号为 1,优先级为 2。2) showdetail 显示进程信息命令。3) exit退出命令行。六、思考题 1) 进程创建的核心内容是什么?2) 该设计和实际的操作系统进程创建相比,缺少了哪些步骤?

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

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!