模拟进程创建、终止、阻塞、唤醒原语--附带注释

更新时间:2023-12-30 20:24:01 阅读量: 教育文库 文档下载

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

题目:计算机操作系统模拟 院系:信息学院

专业:计算机科学与技术 班级:2013级1班

辽宁大学

实验题目一:模拟进程创建、终止、阻塞、唤醒原语

一、题目类型:必做题目。

二、实验目的:通过设计并调试创建、终止、阻塞、唤醒原语功能,有助于对操作系统中进

程控制功能的理解,掌握操作系统模块的设计方法和工作原理。 三、实验环境:

1、硬件:pc机及其兼容机。

2、软件:Windows XP,Turbo C或C++、VC++等。 四、实验内容:

1、设计创建、终止、阻塞、唤醒原语功能函数。

2、设计主函数,采用菜单结构(参见后面给出的流程图)。

3、设计“显示队列”函数,目的能将就绪、阻塞队列中的进程信息显示在屏幕上,以供

随时查看各队列中进程的变化情况。 五、实验要求:

1、进程PCB中应包含以下内容:

进程名 优先级 运行时间 状态 指针 2、系统总体结构:

系统主菜单 1?创建 2?阻塞 3?唤醒 4?终止 5?显示 0?退出 请输入您需要的功能(0-5):

结束 退出 输入选择=? 0 1 创建 2 阻塞 3 唤醒 4 终止 显示 5 开始 进程名:用P1,P2标识。 优先级:为实验题目二做准备。 运行时间:为实验题目二做准备。

状态为:就绪、运行、阻塞,三种基本状态。 指针:指向下一个PCB。

另加实验二:模拟进程调度功能

/*PCB的组织方式:线性方式*/ #include \#include \#include \

typedef struct { char p_name[10];//进程名 char p_pro;//优先级 1-3个级别 1.低 2.中 3.高 char p_status;//运行状态 0.阻塞 1.运行 2.就绪 int p_runtime;//运行时间 int p_wait;//等待时间 struct PCB *next;//指针,指向下一个PCB }PCB;

void Run(PCB *head)//任何时候保证程序里面至少有一个进程在运行 { PCB *p=head->next;//直接将P指向第一个结点 while(p!=NULL)//遍历一遍链表,将所有就绪队列等待时间加1,防止前面结点因为唤醒又进入运行状态 { if(p->p_status=='2') { p->p_wait++;//将等待时间加1 } p=p->next; } p=head->next;//将P重置在第一个结点 while(p->p_status!='1' && p!=NULL) { if(p->p_status=='2')//防止线性链表前面的结点因为从阻塞唤醒后又进入运行状态 { p->p_status='1'; p->p_wait=2; } if(p->p_status=='1')//对上一个if进行处理 { return;

} p=p->next; } return; }

void Insert(PCB *head,PCB *temp)//插入链表函数 { PCB *p; p=head;//将头结点保存起来 while(p->next!=NULL) { p=p->next; } p->next=temp; temp->next=NULL; }

int Check(PCB *head,PCB *temp) { PCB *p=head; while(p->next) { p=p->next; if(strcmp(p->p_name,temp->p_name)==0) return 0; } return 1; }

void Create(PCB *head)//创建进程函数 { int chk=0; PCB *temp;//申请临时存储空间,方便接受数据 temp=(PCB *)malloc(sizeof(PCB)); system(\ printf(\进程创建-----------\\n\ printf(\请输入进程名:\ scanf(\ getchar(); /*检查进程名称,如果相同则返回主界面*/ chk=Check(head,temp); if(chk==0)

{ printf(\进程队列已有该名称进程,创建失败,即将返回主界面.\\n\ system(\ return; } printf(\请输入进程优先级(1.低 2.中 3.高):\ scanf(\ getchar(); printf(\请输入进程运行时间:\ scanf(\ getchar(); temp->p_status='2'; temp->p_wait=2; /* printf(\请输入该进程状态:\ scanf(\ getchar(); */ Insert(head,temp);//调用插入链表函数 system(\ Run(head); }

void Show(PCB *head)//显示队列进程函数 { int ready=1,block=1,run=1; PCB *p=head,*q; system(\ if(p->next==NULL) { printf(\目前系统中没有进程.请返回主界面创建进程!\\n\ system(\ return; } /*列出就绪队列列表*/ q=p->next;//指针指到第一个结点 printf(\就绪队列--\\n\ while(q) { if(q->p_status=='2') { printf(\进程名:%s\ printf(\ 进程优先级:%c\

printf(\ 进程运行时间:%d\ printf(\ 进程等待时间:%d\\n\ } q=q->next; } printf(\ /*列出运行队列列表*/ q=p->next;//将指针重置到第一个结点 printf(\运行队列--\\n\ while(q) { if(q->p_status=='1') { printf(\进程名:%s\ printf(\ 进程优先级:%c\ printf(\ 进程运行时间:%d\\n\ //printf(\ 进程已运行时间:\ } q=q->next; } printf(\ /*列出阻塞队列列表*/ q=p->next; printf(\阻塞队列--\\n\ while(q) { if(q->p_status=='0') { printf(\进程名:%s\ printf(\ 进程优先级:%c\ printf(\ 进程运行时间:%d\ printf(\ 进程等待时间:%d\\n\ } q=q->next; } printf(\ printf(\进程显示完毕.\ system(\}

void Block(PCB *head)//阻塞进程函数

{

char name[10];

PCB *p=head;//保护头结点 system(\

printf(\阻塞进程-----------\\n\

printf(\输入你要放入阻塞队列的进程名称:\scanf(\getchar(); p=p->next; while(p) { if(strcmp(p->p_name,name)==0) break; p=p->next; } if(!p) { printf(\队列中无该进程.\\n\ system(\}

if(p->p_status=='1') { printf(\该进程正在运行.\\n\ printf(\将该进程放入阻塞队列\\n\\n\ system(\ p->p_status='0'; printf(\该进程已经被放入阻塞队列\\n\ system(\} else { if(p->p_status=='0') { printf(\该进程已在阻塞队列中.\\n\ system(\ } if(p->p_status=='2') { printf(\该进程正在就绪队列中.不可放入阻塞队列\\n\ system(\ } }

Run(head);

}

void Delete(PCB *head,PCB *temp)/*head为链表头结点,temp为将要删除的结点*/ { PCB *p=head,*q=temp->next; while(p->next!=temp) { p=p->next; } p->next=q; free(temp); }

void Stop(PCB *head)//终止进程函数 { char name[10]; PCB *p=head; system(\ printf(\终止进程-----------\\n\ printf(\输入你要终止的进程名称:\ scanf(\ getchar(); p=p->next; while(p) { if(strcmp(p->p_name,name)==0) break; p=p->next; } if(!p) { printf(\进程队列中无该进程.\\n\ system(\ } Delete(head,p);//调用删除结点函数 printf(\进程终止成功\\n\ system(\ Run(head); }

void Wakeup(PCB *head)//唤醒进程函数 { char name[10]; PCB *p=head;//保护头结点

system(\ printf(\唤醒进程-----------\\n\ printf(\输入你要唤醒的进程名称:\ scanf(\ getchar(); p=p->next; while(p) { if(strcmp(p->p_name,name)==0) break; p=p->next; } if(!p) { printf(\阻塞队列中无该进程名称.\\n\ system(\ return; } if(p->p_status=='0') { printf(\该进程正在阻塞队列中.\\n\ printf(\将该进程放回就绪队列中\\n\ system(\ p->p_status='2'; p->p_wait=2; printf(\该进程已经被放入就绪队列中\\n\ system(\ } else { if(p->p_status=='1') { printf(\该进程正在运行.不可唤醒\\n\ system(\ } if(p->p_status=='2') { printf(\该进程正在就绪队列中.不可唤醒\\n\ system(\ } } }

void prior_Sche(PCB *head)

{ PCB *p=head->next,*temp=head->next;//保护头结点p,temp为将要删除的结点 system(\ if(p==NULL) { printf(\目前系统中没有进程.请返回主界面创建进程!\\n\ system(\ return; } while(p) { if(temp->p_pro < p->p_pro) { temp=p;//将此时优先级最大的结点地址给临时空间保存 } p=p->next; } printf(\ printf(\经过调度,此时程序中运行的进程是:\\n\ printf(\ 进程名:%s\ printf(\ 进程优先级:%c\ printf(\ 进程运行时间:%d\\n\ printf(\该进程PCB显示完毕!\\n\ system(\ Delete(head,temp); Run(head); }

void time_Sche(PCB *head) { int ready=1; PCB *p=head,*q,*temp=NULL;//保护头结点p,temp为时间片用完将要删除时,保护的临时结点 system(\ if(p->next==NULL) { printf(\目前系统中没有进程.请返回主界面创建进程!\\n\ system(\ return; } /*列出就绪队列列表*/ q=p->next;//指针指到第一个结点 printf(\就绪队列--\\n\

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

Top