操作系统pcb的组织与维护实验.docx
《操作系统pcb的组织与维护实验.docx》由会员分享,可在线阅读,更多相关《操作系统pcb的组织与维护实验.docx(14页珍藏版)》请在冰豆网上搜索。
操作系统pcb的组织与维护实验
****大学计算机科学系
实验报告书
实验题目:
pcb的组织与维护
课程名称:
操作系统
主讲教师:
辅导教师:
课程编号:
班级:
实验时间:
一、实验目的:
1、通过编写和调试pcb组织与维护的模拟程序以加深对pcb组织与维护方案的理解。
2、通过编写和调试pcb组织与维护的模拟程序以加强对进程的基本情况以及进程的运行变化过程的进一步理解。
二、实验环境:
VC6.0++
三、实验内容
1、通过线性方式对以pcb为唯一标志的进程进行创建、查询、添加、删除等操作。
2、通过索引方式对以pcb为唯一标志的进程进行创建、查询、添加、删除等操作。
3、通过链接方式对以pcb为唯一标志的进程进行创建、查询、添加、删除等操作。
四、实验设计原理
1、系统利用PCB来描述进程的基本情况以及进程的运行变化过程。
PCB是进程存在的唯一标志,当系统创建一个进程时,为进程设置一个PCB,再利用PCB对进程进行管理和控制。
撤销进程时,系统收回它的PCB,进程也随之消亡。
进程是由程序、数据和进程控制块PCB三部分组成.PCB是进程的“灵魂”,由于进程控制块中保存了进程的地址信息,通过PCB可以得到进程程序的存储位置,也可以找到整个进程。
2、为了便于管理,系统把所有的PCB用适当方式组织起来。
一般来说,大致有以下三种组织方式:
1、线性方式;2、索引方式;3、连接方式。
通过这三种方式的组织进而对进程进行高效的管理。
五、算法设计与流程
程序设计流程图如下:
1、线性方式流程图:
线性方式代码:
#include
#include
usingnamespacestd;
#defineFALSE0
#defineTRUE1
#defineTASK_READY3
#defineTASK_WAITE4
#defineTASK_RUNNING5
structtask_struct
{
stringpname;//进程名
intpid;//进程号
intstate;//进程当前状态/*3就绪状态4等待状态5运行状态*/
task_struct*next;//指向下一个的
};
/////////////////////////////////////
voidshowInput();
voidmenu(task_struct*pcb);
////////////////////////////////
intinputNum()//提示输入进程数
{
intn;
cout<<"请输入进程数:
";
cin>>n;
cout<returnn;
}
voidCreateList(task_struct*pcb,intn)
{
pcb->next=NULL;
task_struct*p=newtask_struct;
p=pcb;
for(inti=0;i{
task_struct*q=newtask_struct;
inttmp;
q->pid=i+1;
cout<<"请输入第"<
";
cin>>q->pname;
loop:
cout<<"请输入进程状态(3为就绪状态,4为等待,5为运行):
";
cin>>tmp;
if(tmp!
=3&&tmp!
=4&&tmp!
=5)
{
cout<<"输入错误!
"<gotoloop;
}
else
q->state=tmp;
if(p->next==NULL)
{
q->next=NULL;
p->next=q;
//p=q;
cout<<"Addanode"<}
else
{
q->next=NULL;
p=p->next;
p->next=q;
//p=q;
cout<<"forAdd"<}
cout<}
}
voiddisplay(task_struct*pcb)
{
task_struct*p;
cout<<"****************************************"<cout<<"进程号\t进程名\t当前状态"<for(p=pcb->next;p!
=NULL;p=p->next)
{
cout<pid<<"\t"<pname<<"\t";
switch(p->state)
{
case3:
cout<<"TASK_READY"<case4:
cout<<"TASK_WAITE"<case5:
cout<<"TASK_RUNNING"<}
}
cout<<"****************************************"<menu(pcb);
}
///////////////////////////////////////////////
voidoutput(task_struct*pcb,task_struct*p)//输入单个进程记录信息
{
cout<<"****************************************"<cout<<"进程号\t进程名\t当前状态"<cout<pid<<"\t"<pname<<"\t";
switch(p->state)
{
case3:
cout<<"TASK_READY"<case4:
cout<<"TASK_WAITE"<case5:
cout<<"TASK_RUNNING"<}
cout<menu(pcb);
}
voidQuery(task_struct*pcb)//查询
{
intpid;
cout<<"请输入要查询的进程号:
";
cin>>pid;
task_struct*p;
for(p=pcb->next;p!
=NULL;p=p->next)
{
if(p->pid==pid)
{
output(pcb,p);
return;
}
}
cout<<"无该进程!
"<menu(pcb);
}
voidAdd(task_struct*pcb)//添加
{
inttmp;
task_struct*p=newtask_struct;
task_struct*q;
for(q=pcb;q->next!
=NULL;q=q->next);
//if(q->next==NULL)
if(pcb->next==NULL)
p->pid=1;
else
p->pid=q->pid+1;
cout<<"请输入要添加的进程名:
";
cin>>p->pname;
loop1:
cout<<"请输入进程状态(3为就绪状态,4为等待,5为运行):
";
cin>>tmp;
if(tmp!
=3&&tmp!
=4&&tmp!
=5)
{
cout<<"输入错误!
"<gotoloop1;
}
else
p->state=tmp;
p->next=NULL;
//if(q->next==NULL)
if(pcb->next==NULL)
pcb->next=p;
else
q->next=p;
cout<display(pcb);
}
voidDelete(task_struct*pcb)//删除
{
task_struct*p,*q;
cout<<"请输入要删除的进程号:
";
inttmp;
cin>>tmp;
for(p=pcb;p!
=NULL;p=p->next)
{
q=p->next;
if(q->pid==tmp)
{
p->next=q->next;
deleteq;
display(pcb);
}
}
cout<<"无该进程!
"<menu(pcb);
}
////////////////////////////////////////////////
voidDestroy(task_struct*pcb)//销毁链表
{
task_struct*p,*q;
p=pcb;
while(p->next!
=NULL)
{
q=p->next;
p->next=q->next;
deleteq;
}
deletepcb;
}
voidmenu(task_struct*pcb)//提示选择相应的操作
{
intm;
cout<<"1.查询"<cout<<"2.添加"<cout<<"3.删除"<cout<<"4.返回首菜单"<cout<<"选择:
";
cin>>m;
cout<switch(m)
{
case1:
Query(pcb);break;
case2:
Add(pcb);break;
case3:
Delete(pcb);break;
case4:
Destroy(pcb);showInput();break;
default:
cout<<"输入有误!
重新输入"<menu(pcb);
}
}
voidList()//线性方式
{
intn=inputNum();
task_struct*pcb=newtask_struct;
CreateList(pcb,n);
cout<display(pcb);
}
voidHash()//索引方式
{
//intn=inputNum();
}
voidshowInput()//提示选择输入
{
inttmp;
cout<<"1.线性方式"<//cout<<"2.索引方式"<cout<<"请输入你的选择:
";
cin>>tmp;
cout<switch(tmp)
{
case1:
List();break;
//case2:
Hash();break;
default:
cout<<"输入有误!
请重新输入。
"<showInput();
}
}
voidmain()
{
showInput();
}
六、实验调试与结果分析(问题的发现、分析、解决方案与创新)
实验结果展示:
****************************************
进程号进程名当前状态
1aTASK_WAITE
2sTASK_READY
3dTASK_RUNNING
****************************************
1.查询
2.添加
3.删除
4.返回首菜单
选择:
请输入要查询的进程号:
2
****************************************
进程号进程名当前状态
2sTASK_READY
1.查询
2.添加
3.删除
4.返回首菜单
选择:
选择:
2
请输入要添加的进程名:
f
请输入进程状态(3为就绪状态,4为等待,5为运行):
5
****************************************
进程号进程名当前状态
1aTASK_WAITE
2sTASK_READY
3dTASK_RUNNING
4fTASK_RUNNING
****************************************
1.查询
2.添加
3.删除
4.返回首菜单
选择:
选择:
3
请输入要删除的进程号:
2
****************************************
进程号进程名当前状态
1aTASK_WAITE
3dTASK_RUNNING
4fTASK_RUNNING
****************************************
1.查询
2.添加
3.删除
4.返回首菜单
选择:
选择:
4
1.线性方式
2.索引方式
请输入你的选择: