东北大学操作系统实验 绝对自创 实验报告 含可执行源代码 linux环境可运行 有截图.docx
《东北大学操作系统实验 绝对自创 实验报告 含可执行源代码 linux环境可运行 有截图.docx》由会员分享,可在线阅读,更多相关《东北大学操作系统实验 绝对自创 实验报告 含可执行源代码 linux环境可运行 有截图.docx(21页珍藏版)》请在冰豆网上搜索。
东北大学操作系统实验绝对自创实验报告含可执行源代码linux环境可运行有截图
计算机科学与工程学院实验报告
实验课程名称
操作系统实验
实验成绩
专业
班级
指导教师签字
学号
姓名
实验报告批改时间
实验项目目录
1.实验一1-7页
2.实验二8-11页
3.实验三12-15页
4.实验四16-23页
实验报告正文
实验一进程的状态转换及PCB的变化
一、实验目的
这是一个设计型实验。
要求自行设计、编制模拟程序,通过形象化的状态显示,加深理解进程的概念、进程之间的状态转换及其所带来的PCB组织的变化,理解进程与其PCB间的一一对应关系。
二、实验内容
#include
#include
#include
usingnamespacestd;
queueready;
queuerunning;
queueblock;
voidprint();
voidf1();
voidf2();
voidf3();
voidf4();
intmain()
{
ready.push('A');
ready.push('B');
ready.push('C');
running.push('D');
block.push('E');
block.push('F');
print();
intp;
while(scanf("%d",&p)!
=EOF)
{
switch(p)
{
case2:
f2();break;
case3:
f3();break;
case4:
f4();break;
default:
break;
}
}
return0;
}
voidf1()
{
charc=ready.front();
ready.pop();
running.push(c);
print();
}
voidf2()
{
charc=running.front();
running.pop();
ready.push(c);
print();
f1();
}
voidf3()
{
charc=running.front();
running.pop();
block.push(c);
print();
if(!
ready.empty())
{
f1();
}
}
voidf4()
{
charc=block.front();
block.pop();
ready.push(c);
print();
if(running.empty())
{
f1();
}
}
voidprint()
{
intn1,n2,n3;
charch;
n1=ready.size();
n2=running.size();
n3=block.size();
cout<<"___________________"<cout<<"ready:
";
for(inti=0;i{
ch=ready.front();
cout<ready.push(ch);
ready.pop();
}
cout<cout<<"running:
";
for(inti=0;i{
ch=running.front();
cout<running.push(ch);
running.pop();
}
cout<cout<<"block:
";
for(inti=0;i{
ch=block.front();
cout<block.push(ch);
block.pop();
}
cout<}
三、实验结果
实验二进程同步和通信棗生产者和消费者问题模拟
一、实验目的
这是一个验证型实验。
通过对给出的程序进行验证、修改,进一步加深理解进程的概念,了解同步和通信的过程,掌握进程通信和同步的机制,特别是利用缓冲区进行同步和通信的过程。
通过补充新功能,加强对知识的灵活运用,培养创新能力。
二、实验内容
#include
#include
#include
#include
usingnamespacestd;
voidproducer();//生产者
voidprint();//显示缓冲区
voidconsumer();//消费者
queuebuffer;//缓冲区
queueblockp;//等待进入缓冲区的数据
intdata=0;//生产者生产的数据
intnum=0;//缓冲区元素个数
intnp=0;//正在等待的生产者
intnc=0;//正在等待的消费者
intmain()
{
print();
charc;
while(scanf("%c",&c)!
='e')
{
switch(c)
{
case'p':
producer();print();break;
case'c':
consumer();print();break;
case'e':
exit(0);
}
}
}
voidproducer()//生产者
{
if(num<8)
{
if(nc>0)
{
data++;
printf("\t\t\t\t\t正在等待的消费者数量为:
%d\n",--nc);
}
else
{
buffer.push(data++);
num++;
}
}
else
{
blockp.push(data++);
printf("\t\t\t\t\t正在等待的生产者数量为:
%d\n",++np);
}
}
voidconsumer()//消费者
{
if(num>0)
{
buffer.pop();
num--;
if(np>0)
{
buffer.push(blockp.front());
num++;
blockp.pop();
printf("\t\t\t\t\t正在等待的生产者数量为:
%d\n",--np);
}
}
else
{
printf("\t\t\t\t\t正在等待的消费者数量为:
%d\n",++nc);
}
}
voidprint()//显示缓冲区
{
intch;
intn=buffer.size();
printf("\t\t\t\t\t缓冲区状态%d:
\t\t\t\t\t",num);
for(inti=0;i{
ch=buffer.front();
printf("%d\t",ch);
buffer.push(ch);
buffer.pop();
}
printf("\n");
}
三、实验结果
实验三进程的管道通信
一、实验目的
加深对进程概念的理解,明确进程和程序的区别。
学习进程创建的过程,进一步认识进程并发执行的实质。
分析进程争用资源的现象,学习解决进程互斥的方法。
学习解决进程同步的方法。
掌握Linux系统中进程间通过管道通信的具体实现。
二、实验内容
#include
#include
#include
#include
#include
#include
#include
#include
#include
usingnamespacestd;
charchild_1[20]="Thisisprocess1";
charchild_2[20]="Thisisprocess2";
charchild_3[20]="Thisisprocess3";
charfather[20];
intmain()
{
intfd[2];
intre=pipe(fd);
pid_tp1,p2,p3;
int*status=NULL;
p1=fork();
if(p1==0)
{
lockf(fd[1],1,0);
printf("子进程一正在写入,prd=:
%d\n",getpid());
write(fd[1],child_1,17);
lockf(fd[1],0,0);
exit(0);
}
elseif(p1>0)
{
p2=fork();
if(p2==0)
{
lockf(fd[1],1,0);
printf("子进程二正在写入,pid=:
%d\n",getpid());
write(fd[1],child_2,17);
lockf(fd[1],0,0);
exit(0);
}
elseif(p2>0)
{
p3=fork();
if(p3==0)
{
lockf(fd[1],1,0);
printf("子进程三正在写入,prd=:
%d\n",getpid());
write(fd[1],child_3,27);
lockf(fd[1],0,0);
exit(0);
}
elseif(p3>0)
{
wait(status);
read(fd[0],father,17);
cout<wait(status);
read(fd[0],father,17);
cout<wait(status);
read(fd[0],father,17);
cout<}
}
}
return0;
}
三、实验结果
实验四页面置换算法
一、实验目的
进一步加深理解父子进程之间的关系及其并发执行。
理解内存页面调度的机理。
掌握页面置换算法及其实现方法。
培养综合运用所学知识的能力。
二、实验内容
#include
#include
#include
#include
usingnamespacestd;
intmain()
{
intflag_f=0;
intflag_l=0;
floatfifo_ok=0;//fifo命中数
floatfifo_miss=0;//fifo缺页数
floatlru_ok=0;//lru命中数
floatlru_miss=0;//lru缺页数
intframe_num;//内存中分配页面个数
cout<<"请输入内存中分配页面个数:
";
cin>>frame_num;
intM_Frame[frame_num]={0};
inttotal_instruction;//随机数组长度
cout<<"请输入随机数数组长度:
";
cin>>total_instruction;
intAcess_Series[total_instruction];
intAcess_Num;//页程序个数
cout<<"请输入页程序个数:
";
cin>>Acess_Num;
srand(time(NULL));//初始化随机数组
cout<<"访问页的顺序为:
";
for(inti=0;i{
Acess_Series[i]=rand()%Acess_Num+1;
cout<}
cout<intp1=fork();//创建子进程一
if(p1>0)
{
intp2=fork();//创建子进程二
if(p2==0)//子进程二LRU
{
cout<<"这里是子进程二:
LRU算法"<for(inti=0;i{
for(intj=0;j{
if(Acess_Series[i]==M_Frame[j])//命中
{
cout<<"子程序2命中:
";
lru_ok++;
intt;
t=M_Frame[frame_num-1];
M_Frame[frame_num-1]=M_Frame[j];
M_Frame[j]=t;
for(intn=0;n{
cout<}
cout<flag_l=1;//表示命中
break;
}
elseflag_l=0;
}
if(flag_l==0)
{
cout<<"子程序2缺页:
";
lru_miss++;//缺页
for(intk=0;k{
M_Frame[k]=M_Frame[k+1];
}
M_Frame[frame_num-1]=Acess_Series[i];
for(intn=0;n{
cout<}
cout<}
}
cout<<"LRU:
"<<"命中数:
"<"<"<}
}
elseif(p1==0)//子进程一FIFO
{
cout<<"这里是子进程一:
FIFO算法"<for(inti=0;i{
for(intj=0;j{
if(Acess_Series[i]==M_Frame[j])//命中
{
cout<<"子程序1命中:
";
fifo_ok++;
for(intn=0;n{
cout<}
cout<flag_f=1;//表示命中
break;
}
elseflag_f=0;
}
if(flag_f==0)
{
cout<<"子程序1缺页:
";
fifo_miss++;//缺页
for(intk=0;k{
M_Frame[k]=M_Frame[k+1];
}
M_Frame[frame_num-1]=Acess_Series[i];
for(intn=0;n{
cout<}
cout<}
}
cout<<"FIFO"<<"命中数:
"<"<"<}
return0;
}
三、实验结果