1. 头文件
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
#include <io.h>
#include <string.h>
#define MAX_PROCESS 10
int process_number=0; //下一个可用的进程编号
typedef struct pcb{
struct pcb *next; //下一个进程控制块指针
char process_name[20]; //进程名
int process_number; //进程编号
int process_start_moment; //进程启动时刻
int process_need_time; //要求运行时间
int process_time_slice; //时间片
int process_priority; //优先数
}PCB; //自定义数据类型:进程控制块
PCB pcb_table[MAX_PROCESS]; //进程控制块表
PCB *pcb_run=NULL; //进程运行队列头指针
PCB *pcb_free=NULL; //进程空闲队列头指针
PCB *pcb_ready=NULL; //进程就绪队列头指针
PCB *pcb_ready_rear=NULL; //进程就绪队列尾指针
PCB *pcb_blocked=NULL; //阻塞队列头指针
PCB *pcb_blocked_rear=NULL; //阻塞队列尾指针
void init_pcb_table( ); //初始化进程控制块表
void print_space(int num); //显示若干个空格
void display_process_queue(PCB *queue); //显示进程队列
PCB *create_process( ); //创建进程函数,成功时返回新创建进程的PCB,失败时返回NULL。
void block_process_by_name( ); //阻塞指定名称的进程。
void wakeup_process( ); //唤醒进程
void FCFS( ); //先来先服务进程调度算法
void RR( ); //时间片轮转进程调度算法
void HPF( ); //优先数进程调度算法
void MFBQ( ); //多级反馈队列进程调度算法
2.main函数
#include "process_schedule.h" //包含头文件
int main(int argc,char *argv[ ]){
char select; //存放用户选择的菜单项
init_pcb_table( ); //初始化进程控制块表
while(1){
printf("|----------MAIN MENU-------------|\n"); //显示菜单项
printf("| 1:first come first served |\n");
printf("| 2:round robin |\n");
printf("| 3:highest priority first |\n");
printf("| 4:multi_level feedback queue |\n");
printf("| 5:display ready process queue |\n");
printf("| 6:display blocked process queue |\n");
printf("| 7:display running queue |\n");
printf("| a:create a process |\n");
printf("| b:delete a process |\n");
printf("| c:block a process |\n");
printf("| d:wakeup a process |\n");
printf("| 8:exit |\n");
printf("|-----------------------------------|\n");
printf("select a function(1~8,a~d):"); //输出提示信息
do{
select=(char)getch( ); //接收用户的选项
}while(!((49<=select&&select<=56)||(97<=select&&select<=100)));
system("cls"); //清屏
switch(select){ //由选项控制程序功能
case '1':
FCFS( );
break;
case '2':
RR( );
break;
case '3':
HPF( );
break;
case '4':
MFBQ( );
break;
case '5':
printf(" ready queue\n");
display_process_queue(pcb_ready);
break;
case '6':
printf(" blocked queue\n");
display_process_queue(pcb_blocked);
break;
case '7':
printf(" running queue\n");
display_process_queue(pcb_run);
break;
case 'a':
create_process( );
break;
case 'b':
break;
case 'c':
block_process_by_name( );
break;
case 'd':
wakeup_process( );
break;
case '8':
return 0;
}
printf("\nPress any key to return to main menu.");
getch( );
system("cls");
}
return 0;
}
3.创建进程函数create_process( )
PCB *create_process()
{
PCB *p=pcb_free;
if(p==NULL)
return NULL;
else
{
pcb_free=pcb_free->next;
clear();
refresh();
printw("please enter the following fields:\n");
printw("| name | start_moment | need_time | time_slice | priority |\n");
scanw("%s%d%d%d%d",
p->process_name,
&(p->process_start_moment),
&(p->process_need_time),
&(p->process_time_slice),
&(p->process_priority));
p->process_number=(process_number+1)%100;
process_number++;
p->next=NULL;
if(pcb_ready==NULL)
pcb_ready=pcb_ready_rear=p;
else
{
pcb_ready_rear->next=p;
pcb_ready_rear=p;
}
return p;
}
}
4.阻塞进程函数block_process_by_name( )
void block_process_by_name()
{
char process_name[20];
PCB *p=pcb_ready;
PCB *previous_p=pcb_ready;
if(p==NULL)
{
printw("ready queue is empty,no process can be blocked!\n");
return;
}
display_process_queue(pcb_ready);
printw("enter the process name you want to block:\n");
scanw("%s",process_name);
while(p!=NULL){
if(!strcmp(p->process_name,process_name))
break;
previous_p=p;
p=p->next;
}
if(p==NULL)
{
printw("no such a process in ready queue:%s\nyou typed the wrong name\n",
process_name);
return;
}
else
{
if(p==pcb_ready_rear)
{
pcb_ready_rear=previous_p;
}
previous_p->next=p->next;
if(pcb_blocked==NULL)
{
pcb_blocked=pcb_blocked_rear=p;
p->next=NULL;
}
else
{
pcb_blocked_rear->next=p;
pcb_blocked_rear=pcb_blocked_rear->next;
p->next=NULL;
}
}
}
5.唤醒进程函数wakeup_process( )
void wakeup_process()
{
PCB *p=pcb_blocked;
if(pcb_blocked==NULL)
{
printw("blocked queue is empty,no process needs to be wakeuped.\n");
}
else{
if(pcb_blocked==pcb_blocked_rear)
pcb_blocked=pcb_blocked_rear=NULL;
else
pcb_blocked=pcb_blocked->next;
if(pcb_ready==NULL)
{
pcb_ready=pcb_ready_rear=p;
p->next=NULL;
}
else
{
pcb_ready_rear->next=p;
pcb_ready_rear=pcb_ready_rear->next;
p->next=NULL;
}
}
}//wakeup
6.先来先服务进程调度函数FCFS()
void FCFS()
{
if(pcb_ready==NULL)
{
printw("ready queue is empty,no process to run.\n");
}
else
{
pcb_run=pcb_ready;
if(pcb_ready==pcb_ready_rear)
{
pcb_ready=pcb_ready_rear=NULL;
}
else
{
pcb_ready=pcb_ready->next;
}
pcb_run->next=NULL;
}
}
7.时间片轮转进程调度函数RR( )
void RR()
{
PCB *p,*q;
if(pcb_ready==NULL){
printw("ready queue is empty , no proces to run\n");
pcb_run=NULL;
}
else if(pcb_ready==pcb_ready_rear)
{
pcb_ready->process_need_time=
pcb_ready->process_need_time-pcb_ready->process_time_slice;
if(pcb_ready->process_need_time>0)
{
pcb_run=pcb_ready;
printw("process runing\n");
}
else if(pcb_ready->process_need_time<=0){
pcb_ready=NULL;
pcb_run=NULL;
}
}
else{
p = pcb_ready;
q = pcb_ready_rear;
pcb_ready = pcb_ready ->next;
pcb_run = p;
p->process_need_time -= p->process_time_slice;
//进程未运行完
if(p->process_need_time>0){
pcb_ready_rear->next = p;
p->next = NULL;
}
else{
pcb_blocked_rear = p;
p->next = NULL;
}
}
}
8.优先数进程调度函数HPF( )
void HPF()
{
if (pcb_ready == NULL)
{
printw("ready queue is empty,no process to run.\n");
}
else
{
int priority_temp = pcb_ready->process_priority;
PCB *priority_temp_add = pcb_ready;
PCB *priority_temp_add2 = pcb_ready;
PCB *pcb_search = pcb_ready;
/*最高优先级查找*/
while (pcb_search->next != NULL)
{
if (pcb_search->next->process_priority > priority_temp)
{
priority_temp = pcb_search->next->process_priority;
priority_temp_add = pcb_search->next;
priority_temp_add2 = pcb_search;
}
pcb_search = pcb_search->next;
}
pcb_run = priority_temp_add;
if (priority_temp_add == priority_temp_add2)
{
pcb_ready = priority_temp_add->next;
priority_temp_add2->next = pcb_run->next;
pcb_run->next = NULL;
}
else if (pcb_run->next != NULL)
{
pcb_ready = priority_temp_add2;
priority_temp_add2->next = pcb_run->next;
pcb_run->next = NULL;
}
}
}
完整代码
#include "sc.h"
int main(int argc,char *argv[]){
char select;
initscr();
init_pcb_table();
bool end=false;
while(!end){
clear();
refresh();
printw("|----------MAIN MENU-------------|\n");
printw("| 1:first come first served |\n");
printw("| 2:round robin |\n");
printw("| 3:highest priority first |\n");
printw("| 4:multi_level feedback queue |\n");
printw("| 5:display ready process queue |\n");
printw("| 6:display blocked process queue |\n");
printw("| 7:display running queue |\n");
printw("| a:create a process |\n");
printw("| b:delete a process |\n");
printw("| c:block a process |\n");
printw("| d:wakeup a process |\n");
printw("| 8:exit |\n");
printw("|-----------------------------------|\n");
printw("select a function(1~8,a~d):");
refresh();
do{
select=(char)getch();
refresh();
}while(!((49<=select&&select<=56)||(97<=select&&select<=100)));
clear();
refresh();
switch(select){
case '1':
FCFS();
break;
case '2':
RR();
break;
case '3':
HPF();
break;
case '4':
MFBQ();
break;
case '5':
printw(" ready queue\n");
display_process_queue(pcb_ready);
break;
case '6':
printw(" blocked queue\n");
display_process_queue(pcb_blocked);
break;
case '7':
printw(" running queue\n");
display_process_queue(pcb_run);
break;
case 'a':
create_process();
break;
case 'b':
break;
case 'c':
block_process_by_name();
break;
case 'd':
wakeup_process();
break;
case '8':
printw("\n");
end=true;
}
printw("press any key to continue.\n");
getch();
refresh();
}
endwin();
return 0;
}
void init_pcb_table()
{
int i=0;
pcb_free=&pcb_table[0];
pcb_table[MAX_PROCESS-1].next=NULL;
pcb_table[MAX_PROCESS-1].process_number=0;
pcb_table[MAX_PROCESS-1].process_start_moment=0;
pcb_table[MAX_PROCESS-1].process_need_time=0;
pcb_table[MAX_PROCESS-1].process_time_slice=0;
pcb_table[MAX_PROCESS-1].process_priority=0;
strcpy(pcb_table[MAX_PROCESS-1].process_name,"");
for(i=0;i<MAX_PROCESS-1;i++){
pcb_table[i].next=&pcb_table[i+1];
pcb_table[i].process_number=0;
pcb_table[i].process_start_moment=0;
pcb_table[i].process_need_time=0;
pcb_table[i].process_time_slice=0;
pcb_table[i].process_priority=0;
strcpy(pcb_table[i].process_name,"");
}
}
void display_process_queue(PCB *queue)
{
PCB *p=queue;
int i=4;
move(1,1);
printw("|----------|----------|----------|----------|----------|----------|\n");
move(2,1);
printw("| name | number | start | need | slice | priority |\n");
move(3,1);
printw("|----------|----------|----------|----------|----------|----------|\n");
while(p!=NULL){
move(i,1);
printw("| ");
printw("%s",p->process_name);
move(i,12);
printw("| ");
printw("%d",p->process_number);
move(i,23);
printw("| ");
printw("%d",p->process_start_moment);
move(i,34);
printw("| ");
printw("%d",p->process_need_time);
move(i,45);
printw("| ");
printw("%d",p->process_time_slice);
move(i,56);
printw("| ");
printw("%d",p->process_priority);
move(i,67);
printw("|");
p=p->next;
i++;
}
move(i,1);
printw("|----------|----------|----------|----------|----------|----------|\n");
refresh();
}
void FCFS()
{
if(pcb_ready==NULL)
{
printw("ready queue is empty,no process to run.\n");
}
else
{
pcb_run=pcb_ready;
if(pcb_ready==pcb_ready_rear)
{
pcb_ready=pcb_ready_rear=NULL;
}
else
{
pcb_ready=pcb_ready->next;
}
pcb_run->next=NULL;
}
}
void RR()
{
PCB *p,*q;
if(pcb_ready==NULL){
printw("ready queue is empty , no proces to run\n");
pcb_run=NULL;
}
else if(pcb_ready==pcb_ready_rear)
{
pcb_ready->process_need_time=
pcb_ready->process_need_time-pcb_ready->process_time_slice;
if(pcb_ready->process_need_time>0)
{
pcb_run=pcb_ready;
printw("process runing\n");
}
else if(pcb_ready->process_need_time<=0){
pcb_ready=NULL;
pcb_run=NULL;
}
}
else{
p = pcb_ready;
q = pcb_ready_rear;
pcb_ready = pcb_ready ->next;
pcb_run = p;
p->process_need_time -= p->process_time_slice;
//进程未运行完
if(p->process_need_time>0){
pcb_ready_rear->next = p;
p->next = NULL;
}
else{
pcb_blocked_rear = p;
p->next = NULL;
}
}
}
void HPF()
{
if (pcb_ready == NULL)
{
printw("ready queue is empty,no process to run.\n");
}
else
{
int priority_temp = pcb_ready->process_priority;
PCB *priority_temp_add = pcb_ready;
PCB *priority_temp_add2 = pcb_ready;
PCB *pcb_search = pcb_ready;
/*最高优先级查找*/
while (pcb_search->next != NULL)
{
if (pcb_search->next->process_priority > priority_temp)
{
priority_temp = pcb_search->next->process_priority;
priority_temp_add = pcb_search->next;
priority_temp_add2 = pcb_search;
}
pcb_search = pcb_search->next;
}
pcb_run = priority_temp_add;
if (priority_temp_add == priority_temp_add2)
{
pcb_ready = priority_temp_add->next;
priority_temp_add2->next = pcb_run->next;
pcb_run->next = NULL;
}
else if (pcb_run->next != NULL)
{
pcb_ready = priority_temp_add2;
priority_temp_add2->next = pcb_run->next;
pcb_run->next = NULL;
}
}
}
void MFBQ()
{
}
PCB *create_process()
{
PCB *p=pcb_free;
if(p==NULL)
return NULL;
else
{
pcb_free=pcb_free->next;
clear();
refresh();
printw("please enter the following fields:\n");
printw("| name | start_moment | need_time | time_slice | priority |\n");
scanw("%s%d%d%d%d",
p->process_name,
&(p->process_start_moment),
&(p->process_need_time),
&(p->process_time_slice),
&(p->process_priority));
p->process_number=(process_number+1)%100;
process_number++;
p->next=NULL;
if(pcb_ready==NULL)
pcb_ready=pcb_ready_rear=p;
else
{
pcb_ready_rear->next=p;
pcb_ready_rear=p;
}
return p;
}
}
void block_process_by_name()
{
char process_name[20];
PCB *p=pcb_ready;
PCB *previous_p=pcb_ready;
if(p==NULL)
{
printw("ready queue is empty,no process can be blocked!\n");
return;
}
display_process_queue(pcb_ready);
printw("enter the process name you want to block:\n");
scanw("%s",process_name);
while(p!=NULL){
if(!strcmp(p->process_name,process_name))
break;
previous_p=p;
p=p->next;
}
if(p==NULL)
{
printw("no such a process in ready queue:%s\nyou typed the wrong name\n",
process_name);
return;
}
else
{
if(p==pcb_ready_rear)
{
pcb_ready_rear=previous_p;
}
previous_p->next=p->next;
if(pcb_blocked==NULL)
{
pcb_blocked=pcb_blocked_rear=p;
p->next=NULL;
}
else
{
pcb_blocked_rear->next=p;
pcb_blocked_rear=pcb_blocked_rear->next;
p->next=NULL;
}
}
}
void wakeup_process()
{
PCB *p=pcb_blocked;
if(pcb_blocked==NULL)
{
printw("blocked queue is empty,no process needs to be wakeuped.\n");
}
else{
if(pcb_blocked==pcb_blocked_rear)
pcb_blocked=pcb_blocked_rear=NULL;
else
pcb_blocked=pcb_blocked->next;
if(pcb_ready==NULL)
{
pcb_ready=pcb_ready_rear=p;
p->next=NULL;
}
else
{
pcb_ready_rear->next=p;
pcb_ready_rear=pcb_ready_rear->next;
p->next=NULL;
}
}
}//wakeup
运行结果:
主界面
FCFS:
1.创建四个进程后,显示就绪队列
2.运行一次调度算法后,显示就绪队列和运行进程队列;
3.运行二次调度算法后,显示就绪队列和运行进程队列;
4.运行四次调度算法后,显示就绪队列和运行进程队列;
RR:
1.创建四个进程后,显示就绪队列
2.运行一次调度算法后,显示就绪队列和运行进程队列;
3.运行二次调度算法后,显示就绪队列和运行进程队列
4.运行四次调度算法后,显示就绪队列和运行进程队列;
HPF:
1.创建四个进程后,显示就绪队列
2.运行一次调度算法后,显示就绪队列和运行进程队列;
3.运行二次调度算法后,显示就绪队列和运行进程队列
4.运行四次调度算法后,显示就绪队列和运行进程队列;