操作系统实验指导书3

更新时间:2024-03-14 02:42:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

《计算机操作系统》

实 验 指 导 书

实验报告内容规范

实验报告内容要求如下:

课程名称、实验日期、实验名称、实验类型、班级、姓名(学号)、指导教师 一、实验目的及要求

本次实验所涉及并要求掌握的知识点。 二、实验环境

本次实验所使用的设备和软件。 三、实验内容

本次实验的具体内容。 四、实验步骤

按照实验过程列出实验步骤(涉及编程的,须首先进行算法设计,画出流程图)。

五、调试过程

详细记录实验过程中出现的问题及解决方法。 六、实验结果及分析 记录实验结果并进行分析。 七、总结

心得体会及改进意见。 八、附录(图)

目 录

实验一 进程调度设计 ............................................... 1 实验二 页面调度算法 ............................................... 4 实验三 文件管理 .................................................. 10 实验四

命令解释程序 .............................................. 13

实验一 进程调度设计

1. 目的和要求

进程调度是处理机管理的核心内容。本实验要求用C语言编写和调试一个简单的进程调度程序。通过本实验可以加深理解有关进程控制块、进程队列的概念,并体会和了解FIFO调度算法的具体实现方法。

2. 实验内容

①设计进程控制块PCB表结构。 ②编制FIFO进程调度算法.

3. 实验环境

PC兼容机/Windows、DOS系统/Turbo C 2.0

4. 参考程序

#include \#include \#define max 100

#define pfree 0 /*process end*/

#define running 1 /*process running status*/ #define aready 2 /*process aready status */

#define blocking 3 /*process aready blocking status*/ typedef struct node

{

char name; int status;

int precendence; int ax,bx,cx,dx; int pc; int psw;

struct node *next; /*pcb define*/ }pcb;

pcb *createprocess(pcb *head)

{

pcb *p,*q;

int a,b,c,d,m,n; char ID; int s; q=NULL;

printf(\scanf(\

scanf(\while(ID!='*') {

p=(pcb*)malloc(sizeof(pcb)); p->name=ID;

1

p->ax=a; p->bx=b; p->cx=c; p->dx=d; p->pc=m; p->psw=n;

p->precendence=pfree; p->status=aready; if(head==NULL) head=p; else

q->next=p; q=p;

printf(\scanf(\

scanf(\}

if(q!=NULL) q->next=NULL; q=head; while(q) {

printf(\

printf(\>bx,q->cx,q->dx,q->pc,q->psw); q=q->next; }

return head;/*createprocess end*/ }

void processfifo(pcb *head) /*use fifo */

{

pcb *p; p=head;

printf(\printf(\while(p!=NULL) {

p->status=running;

printf(\

printf(\p->pc,p->psw); /*check process running status */ p->status=0; p=p->next; }

printf(\p=head; while(p) {

printf(\p=p->next; }

printf(\

2

} main()

{

pcb *head; head=NULL;

head=createprocess(head); processfifo(head); }

3

实验二 页面调度算法

1. 目的和要求

通过本实验可以加深理解有关虚拟存储器的工作原理,进一步体会和了解页面替换算法的具体实现方法。

2. 实验内容

① 实现三种算法:先进先出;OPT;LRU

② 页面序列从指定的文本文件(TXT文件)中取出

③ 输出:第一行:每次淘汰的页面号,第二行:显示缺页的总次数

3. 实验环境

PC兼容机/Windows、DOS系统/Turbo C 2.0

4. 参考程序

#include #include #include #define null 0

#define len sizeof(struct page)

struct page

{

int num; int tag;

struct page *next; };

struct page *create(int n) /*建立分配的内存空间,并初始化,返回头结点*/

{

int count=1;

struct page *p1,*p2,*head;

head=p2=p1=(struct page *)malloc(len); p1->tag=-1;p1->num=-1; while(count

count++;

p1=(struct page *)malloc(len); p1->tag=-1;p1->num=-1; p2->next=p1; p2=p1; }

p2->next=null; return(head); }

void FIFO(array,n)

4

int array[],n; {

int *p;

struct page *cp,*dp,*head,*new; int count=0; head=create(n); p=array;

while(*p!=-1) {

cp=dp=head;

for(;cp->num!=*p&&cp->next!=null;) cp=cp->next; if (cp->num==*p) printf(\else {

count++; cp=head;

for(;cp->tag!=-1&&cp->next!=null;) cp=cp->next; if(cp->tag==-1) {

cp->num=*p; cp->tag=0; printf(\} else {

new=(struct page*)malloc(len); new->num=*p; new->tag=0; new->next=null; cp->next=new; head=head->next;

printf(\free(dp); } } p++; }

printf(\}

void LRU(array,n)

int array[],n; {

int count=0,*p=array;

struct page *head,*cp,*dp,*rp,*new,*endp; head=create(n); while(*p!=-1) {

cp=dp=rp=endp=head;

for(;endp->next!=null;) endp=endp->next; for(;cp->num!=*p&&cp->next!=null;) {

5

rp=cp;cp=cp->next;} if(cp->num==*p)

{

printf(\if(cp->next!=null) {

if(cp!=head)

rp->next=cp->next; else head=head->next; }

endp->next=cp; cp->next=null; } else {

count++; cp=rp=head;

for(;cp->tag!=-1&&cp->next!=null;) cp=cp->next; if(cp->tag==-1) {

printf(\cp->num=*p; cp->tag=0; } else {

new=(struct page *)malloc(len); new->num=*p; new->tag=0; new->next=null; cp->next=new; dp=head;

head=head->next;

printf(\free(dp); } } p++;

}

printf(\}

OPT(array,n)

int array[],n; {

int *p,*q,count=0,i;

struct page *head,*cp,*dp,*new; p=array;

head=create(n); while(*p!=-1) {

cp=head;

6

for(;cp->num!=*p&&cp->next!=null;) cp=cp->next; if(cp->num!=*p) {

count++; cp=head;

for(;cp->tag!=-1&&cp->next!=null;) cp=cp->next; if(cp->tag==-1) {

printf(\cp->num=*p; cp->tag=0; } else {

i=1;q=p;q++;cp=head; while(*q!=-1&&i

for(;*q!=cp->num&&cp->next!=null;) cp=cp->next; if(*q==cp->num) {

cp->tag=1; i++; }

q++;cp=head; }

if(i==n) {

for(;cp->tag!=0;) cp=cp->next; printf(\cp->num=*p; } else {

cp=head;

for(;cp->tag!=0;) cp=cp->next; if(cp==head) {

for(;cp->next!=null;) cp=cp->next; new=(struct page *)malloc(len); new->num=*p; new->tag=0; new->next=null; cp->next=new; dp=head;

head=head->next;

printf(\free(dp); } else {

printf(\cp->num=*p;

7

}

}

cp=head;

for(;cp->next!=null;) {cp->tag=0;cp=cp->next;} cp->tag=0; }

}

else printf(\p++;

}

printf(\}

main()

{

FILE *fp; char pt;

char str[10]; int i,j=0;

int page[50],space=0; for(i=0;i<50;i++)

page[i]=-1;

fp=fopen(\if(fp==NULL) {

printf(\exit(0); } i=0;

while((pt=fgetc(fp))!=EOF)/*将数字字符串转化成整型-开始*/ {

if(pt>='0'&&pt<='9') {

str[i]=pt;i++; space=0; } else {

if(pt==' '||pt=='\\n') {

if(space==1) break; else {

str[i]='\\0';

page[j]=atoi(str); if(pt=='\\n') break; else {

space=1; j++; i=0; }

8

} }

} }/*结束*/

if(pt==EOF) {str[i]='\\0';page[j]=atoi(str);} i=0;

while(page[i]!=-1) {printf(\fclose(fp); printf(\

printf(\printf(\FIFO(page,3); printf(\LRU(page,3);

printf(\OPT(page,3); }

9

实验三 文件管理

1. 目的和要求

通过独立使用高级语言编写和调试一个简单的文件系统,达到模拟文件管理工作的目的,并进一步使学生对各种文件操作命令的实质内容和执行过程有比较深入的了解。

2. 实验内容

设计一个简单的文件系统,对文件的操作设计如下命令(使用菜单选择): creat 建立文件 delete 删除文件 list 文件列表 bye 退出

编写程序并调试通过,运行出结果,画出流程图

3. 实验环境

PC兼容机/Windows、DOS系统/Turbo C 2.0

4. 参考程序

#include

struct filenode {

char *filename; int lenth;

struct filenode *next; } *filehead=NULL;

list(struct filenode *fhead) {

struct filenode *p; if(!fhead)

{printf(\p=fhead;

printf(\while(p) {

printf(\ p=p->next; } }

creat(char *fname) {

int len;

struct filenode *p,*q,*p1; p=p1=filehead; while(p)

{

if(!strcmp(fname,p->filename)) {printf(\ return; } p1=p;

p=p->next; }

q=malloc(sizeof(struct filenode)); printf(\scanf(\

strcpy(q->filename,fname); q->lenth=len; q->next=NULL; p1->next=q;

if(!filehead) filehead=q; }

delete(char *fname) {

struct filenode *p,*q; p=q=filehead; while(p) {

if(!strcmp(fname,p->filename)) {

q->next=p->next; free(p);

printf(\ return; }

p=p->next; }

printf(\}

quit() {

struct filenode *p,*q; p=filehead; while(p) { q=p;

p=p->next; free(q); } }

void main() {

int choice;

11

char *newname=\/*struct filenode *filehead;*/

/*filehead=NULL; malloc(sizeof(struct filenode));*/

while(1) {

printf(\

printf(\ printf(\ printf(\ printf(\ printf(\

printf(\ scanf(\ switch(choice) {

case 1:printf(\ scanf(\ creat(newname); break;

case 2:printf(\ scanf(\ delete(delname); break;

case 3:list(filehead);break; case 4:quit();exit(0); } } }

12

实验四 命令解释程序

1. 目的和要求

理解命令解释程序工作原理。

2. 实验内容

利用高级语言编写一个微型命令解释程序,接收并解释执行以下命令: dir 列出当前目录 cop 文件1 文件2 拷贝文件 era 文件名 删除文件 dat 显示日期 tim 显示时间 end 结束,退出

画出程序流程图,编写程序,实现上述功能。

3. 实验环境

PC兼容机/Windows、DOS系统/Turbo C 2.0

4. 参考程序

#include void main() {

int i,num;

char *comm=\char gjz[6][4]={\

while(1) {

printf(\printf(\printf(\printf(\printf(\printf(\printf(\printf(\printf(\scanf(\for(i=0;i<6;i++) {

if(!strcmp(comm,gjz[i])) {num=i; break;} num=9; }

command=\src=\des=\

delf=\switch(num) {

case 0:command=\

case 1:printf(\ scanf(\

printf(\ scanf(\

command=strcat(command,\ command=strcat(command,src); command=strcat(command,\ command=strcat(command,des); break;

case 2:printf(\ scanf(\

command=strcat(command,\ command=strcat(command,delf); break;

case 3:command=\ case 4:command=\

case 5:printf(\

default:printf(\ }

if(num>=0&&num<=5) system(command); } }

14

本文来源:https://www.bwwdw.com/article/siq8.html

Top