计算机操作系统实验.docx
《计算机操作系统实验.docx》由会员分享,可在线阅读,更多相关《计算机操作系统实验.docx(24页珍藏版)》请在冰豆网上搜索。
计算机操作系统实验
实验一:
了解操作系统
一、实验目的:
通过在虚拟机中装载操作系统、并对硬盘进行分区的操作对操作系统有进一步和深入的了解,学会使用VMwareWorkstation,掌握操作系统的安装和对磁盘进行分区等各种操作
二、实验原理:
在VMwareWorkstation中装载操作系统、对硬盘进行分区
三、实验器材:
硬件:
计算机软件:
VMwareWorkstation、windows系统镜像
四、实验步骤:
1.打开计算机并运行VMwareWorkstation,新建虚拟机文件
2.在VMwareWorkstation中找到保存在本地的系统镜像文件并选择
3.启动虚拟机
4.在弹出对话框中选择PQ系统分区,对硬盘进行分区
5.重新启动虚拟机,选择系统安装位置
6.在装好的操作系统中进行各项操作,检查系统是否存在错误
7.实验完成,关闭VMwareWorkstation,关闭计算机
五、实验心得:
通过本次实验中的各项操作,我学会了安装操作系统并对磁盘进行分区,了解了操作系统,掌握了安装操作系统以及对硬盘进行分区等的各项操作。
实验二:
进程同步和互斥
一、实验目的:
1、掌握临界资源,临界正概念及并发进程互斥,同步访问原理
2、学会使用高级语言进行多线程编程方法
3、掌握利用VC++语言线程库实现进程的互斥,条件竞争并编码实现。
P.O操作,利用P.O操作实现两个并发线程,对有界临界区的同步访问。
二、实验要求:
1、知识基础:
学生应在完成进程和线程及调度章节学习后进行
2、开发环境工具:
硬件平台----网络实验室
软件平台-----windows操作系统VC++语言开发环境
三、实验内容:
1、实现临界资源临界区进程或线程的定义与创建
2、合用两个并发运行的进程实现互斥算法和有界缓冲区的同步算法
实验步骤:
kanra//Process.cpp:
定义控制台应用程序的入口点。
//
#include"stdafx.h"
#include
#include
usingnamespacestd;
staticintMutex=1;
staticintS=5;
intMBlock=0;
intSBlock=0;
classUiProcess
{
public:
intSwait(int&s)
{
s=s-1;
if(s<=0)
{
returnBlockProcess();
}
else
{
return0;
}
}
voidSsignal(int&s)
{
s=s+1;
}
intBlockProcess()
{
if(Mutex<=0)
{
MBlock=abs(Mutex);
returnMBlock;
}
if(S<=0)
{
SBlock=abs(S);
returnSBlock;
}
}
voidPrint(strings)
{
cout<
}
voidPrint(strings,inti)
{
cout<
}
voidProcess()
{
inti=this->Swait(S);
intj=this->Swait(Mutex);
if(i==0&&j==0)
{
Print("进程可以运行");
}
elseif(i>0)
{
Print("当前因资源阻塞的进程数目为:
",i);
}
elseif(j>0)
{
Print("当前因互斥阻塞的进程数目为:
",j);
}
this->Ssignal(Mutex);
//this->Ssignal(S);
}
};
int_tmain(intargc,_TCHAR*argv[])
{
for(inti=0;i<10;i++)
{
UiProcessui;
ui.Process();
}
system("pause");
return0;
}
、实验心得:
通过这次试验,加深了对进程同步的理解。
实验三:
矩阵乘法
一、实验目的:
熟悉矩阵乘法规则以及在计算中实现
二、实验环器材:
WindowsXP,vc++6.0
三、实验内容:
将程序输入计算机,调试运行程序。
程序如下:
#includevoidmain()
{
inti,j,k;
inta[5][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6},{5,6,7}};//相乘的一个矩阵
intb[3][4]={{1,2,3,4},{1,2,3,4},{1,2,3,4}};
intc[5][4]={0};
for(i=0;i<5;i++)for(j=0;j<4;j++)for(k=0;k<3;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
/*以上是主要的乘法算法,三个for循环*/
/*以下是打印得到的矩阵*/
for(i=0;i<5;i++)
{
printf("\n");for(j=0;j<4;j++)
{
printf("%3.2d",c[i][j]);
}
}
getch();/*输入任意字符结束程序,使程序停下来以便观察结果*/
}
四、实验心得:
通过本次实验对三个for循环相乘时的过程分析,熟悉了矩阵相乘的算法以及二维数组在内存中的存储形式
实验四:
银行家算法
一、实验目的:
1,理解死锁概念,银行家算法及安全性检测算法
2,掌握利用C语言设计实现银行家算数的基本进程
3,验证银行家算法对于避免死锁的作用。
二、实验要求:
1,学生应完成如下章节的学习:
进程和线程的调度,死锁
2,安装上述操作系统,使用C程序编程完成设计实现
三、实验步骤:
1,定义并初始化进程及其资源结构
2,提供一个用户界面,用户利用它可动态输入进程和资源种类等相关参数
3,设计实现安全状态,检测和银行家死锁避免功能函数
四、实验步骤:
1,开发一个变量程序
首先从文件中读入系统描述信息,包括进程的数目,资源的种类和数量,每个进程最大资源请求。
程序自动根据文件内容创建一个当前系统描述
如:
每类资源的数目用...准数组R[M]描述,m为资源的种类,每个[J]记录资源的RJ的数量。
进程的最大需求矩阵用P[n][m]表示,P[3][J]记录进程Pi对资源RJ的最大需求。
分配矩阵和请求矩阵,可使用二维数组表示。
2,用户输入一个请求,
格式类似:
request(i,j,k)求release(i,j,k),在这里,i表示进程Pi,J表示进程PJ,K为申请释放的数量。
对于每一个请求,系统回应是满足要求,还是拒绝分配。
3,设定一个申请和释放序列,无任何检测和避免死锁的算法,分配会导致死锁
4,设定一个申请和释放序列,按照安全性算法进行设计,回应系统是否安全。
然后实现银行家算法,确保没有死锁的分配
五、算法如下:
#include
#include
structtype
{
inta;
intb;
intc;
};
structallocation
{
structtypevalue;
structallocation*next;
};
structmax
{
structtypevalue;
structmax*next;
};
structavailable
{
structtypevalue;
structavailable*next;
};
structneed
{
structtypevalue;
structneed*next;
};
structpath
{
intvalue;
structpath*next;
};
structfinish
{
intvalue;
structfinish*next;
};
voidmain()
{
intp,status=0,i,j,temp,flag=0;
structallocation*allochead,*alloc1,*alloc2,*alloctemp;
structmax*maxhead,*max1,*max2,*maxtemp;
structavailable*availablehead,*workhead,*worktemp;
structneed*needhead,*need1,*need2,*needtemp;
structpath*pathhead,*path1,*path2,*pathtemp;
structfinish*finishhead,*finish1,*finish2,*finishtemp;
printf("请输入进程的数目\n");
scanf("%d",&p);
for(i=0;i
{
printf("\n输入进程p%d已经分配的资源\n",i+1);
if(flag==0)
{
allochead=alloc1=alloc2=(structallocation*)malloc(sizeof(structallocation));
printf("\t当前资源类型是%c\t",'a');
scanf("%d",&alloc1->value.a);
printf("\t当前资源类型是%c\t",'b');
scanf("%d",&alloc1->value.b);
printf("\t当前资源类型是%c\t",'c');
scanf("%d",&alloc1->value.c);
flag++;
allochead=alloc1;
}
else
{
alloc2=(structallocation*)malloc(sizeof(structallocation));
printf("\t当前资源类型是%c\t",'a');
scanf("%d",&alloc2->value.a);
printf("\t当前资源类型是%c\t",'b');
scanf("%d",&alloc2->value.b);
printf("\t当前资源类型是%c\t",'c');
scanf("%d",&alloc2->value.c);
alloc1->next=alloc2;
alloc1=alloc2;
flag++;
}
}
alloc2->next=NULL;
flag=0;
for(i=0;i
{
printf("\n输入进程p%d要求的最大数目\n",i+1);
if(flag==0)
{
maxhead=max1=max2=(structmax*)malloc(sizeof(structmax));
printf("\t当前资源类型是%c\t",'a');
scanf("%d",&max1->value.a);
printf("\t当前资源类型是%c\t",'b');
scanf("%d",&max1->value.b);
printf("\t当前资源类型是%c\t",'c');
scanf("%d",&max1->value.c);
maxhead=max1;
flag++;
}
else
{
max2=(structmax*)malloc(sizeof(structmax));
printf("\t当前资源类型是%c\t",'a');
scanf("%d",&max2->value.a);
printf("\t当前资源类型是%c\t",'b');
scanf("%d",&max2->value.b);
printf("\t当前资源类型是s%c\t",'c');
scanf("%d",&max2->value.c);
max1->next=max2;
max1=max2;
flag++;
}
}
max2->next=NULL;
flag=0;
printf("\n请输入可以利用是资源数目\n");
availablehead=workhead=(structavailable*)malloc(sizeof(structavailable));
printf("\n");
printf("\t当前资源类型是%c\t",'a');
scanf("%d",&availablehead->value.a);
printf("\t当前资源类型是%c\t",'b');
scanf("%d",&availablehead->value.b);
printf("\t当前资源类型是%c\t",'c');
scanf("%d",&availablehead->value.c);
workhead=availablehead;
workhead->value=availablehead->value;
flag=0;
alloctemp=allochead;
maxtemp=maxhead;
for(i=0;i
{
if(flag==0)
{
needhead=need1=need2=(structneed*)malloc(sizeof(structneed));
need1->next=need2->next=NULL;
need1->value.a=(maxtemp->value.a)-(alloctemp->value.a);
need1->value.b=(maxtemp->value.b)-(alloctemp->value.b);
need1->value.c=(maxtemp->value.c)-(alloctemp->value.c);
needhead=need1;
flag++;
}
else
{
need2=(structneed*)malloc(sizeof(structneed));
need2->value.a=(maxtemp->value.a)-(alloctemp->value.a);
need2->value.b=(maxtemp->value.b)-(alloctemp->value.b);
need2->value.c=(maxtemp->value.c)-(alloctemp->value.c);
need1->next=need2;
need1=need2;
flag++;
}
maxtemp=maxtemp->next;
alloctemp=alloctemp->next;
}
need2->next=NULL;
flag=0;
for(i=0;i
{
if(flag==0)
{
finishhead=finish1=finish2=(structfinish*)malloc(sizeof(structfinish));
finish1->next=finish2->next=NULL;
finish1->value=0;
finishhead=finish1;
flag++;
}
else
{
finish2=(structfinish*)malloc(sizeof(structfinish));
finish2->value=0;
finish1->next=finish2;
finish1=finish2;
flag++;
}
}
finish2->next=NULL;
flag=0;
for(temp=0;temp
{
alloctemp=allochead;
needtemp=needhead;
finishtemp=finishhead;
worktemp=workhead;
for(j=0;j
{
if(finishtemp->value==0)
{
if((needtemp->value.a<=worktemp->value.a)&&(needtemp->value.b<=worktemp->value.b)&&(needtemp->value.c<=worktemp->value.c))
{
worktemp->value.a+=alloctemp->value.a;
worktemp->value.b+=alloctemp->value.b;
worktemp->value.c+=alloctemp->value.c;
finishtemp->value=1;
if(flag==0)
{
pathhead=path1=path2=(structpath*)malloc(sizeof(structpath));
path1->next=path2->next=NULL;
path1->value=j+1;
pathhead=path1;
flag++;
}
else
{
path2=(structpath*)malloc(sizeof(structpath));
path2->value=j+1;
path1->next=path2;
path1=path2;
flag++;
}
finishtemp=finishtemp->next;
alloctemp=alloctemp->next;
needtemp=needtemp->next;
}
else
{
finishtemp=finishtemp->next;
alloctemp=alloctemp->next;
needtemp=needtemp->next;
}
}
else
{
finishtemp=finishtemp->next;
alloctemp=alloctemp->next;
needtemp=needtemp->next;
}
}
}
path2->next=NULL;
finishtemp=finishhead;
pathtemp=pathhead;
for(temp=0;temp
{
if(finishtemp->value==0)
{
printf("\n警告!
当前系统是不安全的\n");
exit(0);
}
finishtemp=finishtemp->next;
}
printf("\n当前系统是安全的!
\n");
printf("\n安全序列为:
\n");
for(i=0;i
{
printf("p%d\t",pathhead->value);
pathhead=pathhead->next;
}
}
五、实验心得:
程序基本实现了银行家算法的功能,并在其基础上考虑了输出显示的格式的美观性、输入错误要求重新输入等几个方面,尽可能的使程序完整一些。
这段程序算是三个试验中最困难最复杂的一个,由于能力有限,所以在紧张的的编写和调试过程中遇到了许多的问题,通过查询资料、翻阅课本、向同学请教、多次调试等方法逐渐解决了大部分问题,编写的过程中也有比较一些算法的优劣性,找出比较好的算法,收获非常大。
通过本次实验,加深了我对银行家算法的理解。
实验中遇到了许多问题,通过网络这个大平台问题最终也得到了解决。
通过这次的编程,使得自己的动手能力又增强了许多,同时也加深了对理论知识的理解。
实验五:
文件操作
一、实验目的:
1.掌握文件系统的工作原理。
2.理解文件系统的主要数据结构。
3.编程实现文件操作。
二、实验内容:
1.设计并实现一个一级(单用户)文件系统程序
提供以下操作:
文件创建/删除接口命令create/delete;
目录创建/删除接口命令midair/rmdir;
显示目录内容命令ls。
创建的文件不要求格式和内容
2.在以上基础上世纪并实现一个二级文件系统
具备提供用户登录
文件、目录要有权限
三、程序源代码:
//FileControl.cpp:
主项目文件。
/*
程序功能:
对文件进行操作:
判断文件是否存在、创建新文件、读文件、写文件、创建目录、删除目录、删除文件、将文件创建到指定目录中。
作者:
日期:
2011-12-4
*/
#include"stdafx.h"
usingnamespaceSystem;
usingnamespaceSystem:
:
IO;
intmain(array:
String^>^args)
{
/*
File类可用于创建文件、查找文件、设置文件等
Directory类可用于创建目录、查找目录、设置目录等
*/
String^dir="a";//设置目录名,注意路径分量间用两个\\隔开。
String^filename="b";//设置文件明
String^path=dir+filename;
TextReadertReader(path);//读文件类
TextWritertWriter(path);//写文件类
if(File:
:
Exists(path))//判断文件是否存在,若存在则可直接读写,否则创建文件
{
Console:
:
WriteLine(L"文件存在!
");
//直接对文件进行读操作,可以修改数据
//写文件操作。
}
else
{
File:
:
CreateText(path);//创建文件
//新建文件应先写入数据,然后在读取操作
//此处调用tWriter对象写入数据,数据可为任意内容。
}
Console:
:
ReadKey();
return0;
}
四、参考程序:
1.#include
#include
voidmain()
{
FILE*p1,*p2,*p3;
charch;
if((p1=fopen("test.txt","w"))==NULL)
{
printf("cannotopenthefile:
");
exit
(1);
}
printf("请输入字符保存到test中:
");
while((ch=getchar())!
='\n')
fputc(ch,p1);
if((p2=fopen("test1.txt","w+"))==NULL)
{
printf("cannotopenthefile:
");
exit
(1);
}
printf("请输入字符保存到test1中:
");
while((ch=