操作系统实验报告
班级:
学号:
姓名:
2017.5
实验 1:熟悉 Linux 系统
一、 实验目的:
熟悉和掌握 Linux 系统基本命令,熟悉 Linux 编程环境,为以后的实验打
下基础。
1、启动、退出、ls(显示目录内容)、cp(文件或目录的复制)、mv(文
件、目录更名或移动)、rm(删除文件或目录)、mkdir(创建目录)、rmdir
(删除空目录)、cd(改变工作目录)…
2、C 语言编辑、编译
二、实验内容及要求:
1、熟练掌握 Linux 基本文件命令 ;
2、掌握 Linux 编辑程序、对源代码进行编译、连接、运行及调试的过程 ;
3、认真做好预习,书写预习报告 ;
4、实验完成后要认真总结、完成实验报告。
实验二 进程状态转换及其 PCB 的变化
一、实验目的:
这是一个设计型实验。要求自行设计、编制模拟程序,通过形象化的状
态显示,加深理解进程的概念、进程之间的状态转换及其所带来的 PCB 组
织的变化,理解进程与其 PCB 间的一一对应关系。
二、实验内容及要求:
(1)、 设计并实现一个模拟进程状态转换及其相应 PCB 内容、组织结构变化的
程序。
(2)、独立编写、调试程序。进程的数目、进程的状态模型(三状态、五状态、
七状态或其它)以及 PCB 的组织形式可自行选择。
(3)、 程序界面应能反映出在模拟条件下,进程之间状态转换及其对应 PCB
组织的变化。
(4)、 代码书写要规范,要适当地加入注释。
(5)、 鼓励在实验中加入新的观点或想法,并加以实现。
(6)、 认真进行预习,完成预习报告。
(7)、 实验完成后,要认真总结,完成实验报告。
三进程状态:
三、流程图:
开始
初始化变量
N
i<=4?
Y
依次输入进程信息
输入选项进行选择操作
Y
Y
Y
Y
运行进程
时间片到
使进程等待
进程处于就绪态
R?
T?
N
N
W?
N
N
C?
Y
Y
退出进程
Y
输出结果
结束
E?
N
P?
N
O?
N
输入一个选项
四、使用的数据结构及其说明:
struct PCB
{
//进程控制块 PCB
//名字标识
char name;
string state; //状态
int time;
//执行时间
};
struct QNode
{
ElemType data;
struct QNode *next;
//链式队列结点
};
五、源代码及注释:
#include
#include
using namespace std;
struct PCB
{
//进程控制块 PCB
char name;
//名字标识
string state; //三状态
int time;
//执行时间
};
typedef struct PCB Operation;
struct QNode
{
Operation data;
struct QNode *next;
//链式队列结点
};
typedef struct QNode QNode;
typedef struct QNode *PNode;
typedef struct
{
PNode fron;
PNode rear;
} Queue; //链式队列
//结点
void Insert_Queue(Queue &Q,Operation e) //插入
{
PNode os = new QNode();
//PNode os=(PNode)malloc(sizeof(QNode));
if(!os)
{
cout<<"(Insert_Queue)动态分配结点失败!\n";
exit(1);
}
os->data=e;
os->next=NULL;
Q.rear->next=os;
Q.rear=os;
}
int Init_Queue(Queue &Q) //初始化
{
Q.fron=Q.rear=(PNode)malloc(sizeof(QNode));
if(!Q.fron&&!Q.rear)
{
cout<<"(Insert_Queue)动态分配结点失败!\n";
exit(1);
}
Q.fron->next=NULL;
return 0;
}
int Delete_Queue(Queue &Q,Operation &e) //删除
{
PNode node;
if(Q.fron==Q.rear)
//空队列
return 1;
//删除第一个元素
node=Q.fron->next;
Q.fron->next=node->next;
e=node->data;
if(Q.rear==node)
Q.rear=Q.fron;
free(node);
return 0;
}
int Empty_Queue(Queue Q)
{
//判断是否为空队列,是 1,否 0
if(Q.fron==Q.rear)
//空队列
return 1;
else
return 0;
//
return (Q.fron==Q.rear?1:0);
}
void Print_Queue(Queue &Q) //打印队列元素
{
PNode ptr;
if(Q.fron==Q.rear)
{
//队列为空时,返回提示信息
cout<<"\t\tempty.\n";
}
else
{
ptr=Q.fron->next;
while(ptr!=NULL)
{
cout<<"\t\tProcess's name : "<
data.name<next;
}
}
}
void Print_State(Queue &Q_Ready,Queue &Q_Running,Queue &Q_Blocked)
{
cout<<"\t-----------------------------\n";
cout<<"\n\tStatus_Ready:\n";
Print_Queue(Q_Ready);
cout<<"\n\tStatus_Running: \n";
Print_Queue(Q_Running);
cout<<"\n\tStatus_Blocked: \n";
Print_Queue(Q_Blocked);
cout<<"\n\t-----------------------------\n";
}
void Transision_Two(Queue &Q1,Queue &Q2)
{
Operation e;
if(Empty_Queue(Q1))
{
cout<<"\nERROR! 前一个队列为空!.\n";
return;
}
Delete_Queue(Q1,e);
Insert_Queue(Q2,e);
}
int main()
{
char ch;
Operation e;
Queue Q_Ready,Q_Running,Q_Wait;
Init_Queue(Q_Ready);
Init_Queue(Q_Wait);
Init_Queue(Q_Running);
for(int i=1; i<=4; i++)
{
cout<<"\n 请输入第"<>e.name;
e.state="ready";
cout<<"请输入进程时间: ";
cin>>e.time;
Insert_Queue(Q_Ready,e);
}
cout<<"\n 初始化进程均处于就绪态";
cout<<"\nR=Running,T=Timeout,W=Wait,C=Ready,E=Exit,P=Print,O=Out"<>ch;
while(ch!='O')
{
switch(ch)
{
case 'R'://使进程处于运行态
Transision_Two(Q_Ready,Q_Running);
Print_State(Q_Ready,Q_Running,Q_Wait);
break;
case 'T'://一个进程执行完毕
Transision_Two(Q_Running,Q_Ready);
Print_State(Q_Ready,Q_Running,Q_Wait);
break;
case 'W'://使进程处于等待态
Transision_Two(Q_Running,Q_Wait);
Print_State(Q_Ready,Q_Running,Q_Wait);
break;
case 'C': //使进程处于就绪态
Transision_Two(Q_Wait,Q_Ready);
Print_State(Q_Ready,Q_Running,Q_Wait);
break;
case 'E': //退出进程
if(Empty_Queue(Q_Running))
{
cout<<"\n\tRelease Error! 没有正在执行的进程.\n";
Print_State(Q_Ready,Q_Running,Q_Wait);
break;
}
Delete_Queue(Q_Running,e);
Print_State(Q_Ready,Q_Running,Q_Wait);
break;
case 'P'://输出各进程
Print_State(Q_Ready,Q_Running,Q_Wait);
break;
case 'O'://退出程序
exit(1);
default:
cout<<"输入有误,请重新选择"<>ch;
}
return 0;
}
六、运行结果: