⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scheduler.cpp

📁 对进程调度与作业调度的模拟,进程调度算法:采用的是最高优先数优先的高度算法(即把处理机分配给优先数最高的进程和先来先服务算法。分区分配和回收算法:采用首次适应算法和最佳适应算法实现动态分区。其中
💻 CPP
字号:
// scheduler.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdio.h"
#include "malloc.h"

struct PCB 
{
	char name[10];
	char status;
	int priority;
	int nTime;
	int rTime;
	struct PCB* link;
}*ready=0,*t;

void sort()
{
	PCB *first,*next;
	int insert = 0;
	if (ready==0 || (t->priority)>(ready->priority))
	{//优先级大者链入链头
		t->link = ready;
		ready = t;
	}
	else
	{
		first = ready;
		next = first->link;
		while (next != 0)
		{
			if ((t->priority)>(next->priority))
			{
				first->link = t;
				t->link = next;
				next = 0;
				insert = 1;
			}
			else
			{
				first = first->link;
				next = next->link;
			}
		}
		if (insert == 0) first->link = t;
	}
}

void create()
{
	int i,n;
	printf("\n 请输入要创建的进程数:"); 
	scanf("%d",&n); 
	for (i = 0;i < n;i++) 
	{ 
		printf("\n进程号No.%d:\n",i); 
		t = (PCB *) malloc (sizeof(PCB)); 
		printf("\n输入进程名:"); 
		scanf("%s",t->name); 
		printf("\n输入进程优先级数:"); 
		scanf("%d",&t->priority); 
		printf("\n输入进程运行时间:"); 
		scanf("%d",&t->nTime); 
		printf("\n"); 
		t->rTime = 0;
		t->status = 'W'; 
		t->link = 0; 
		sort(); /* 调用sort函数*/ 
	} 
} 

int length() 
{
	int i = 0; PCB *pr=ready;
	while (pr != 0)
	{
		i++;
		pr=pr->link;
	}
	return(i); 
}

void display(PCB *thread) /*显示当前进程*/ 
{ 
	printf("\n threadName \t state \t priority \t needTime \t runtime \n"); 
	printf(" |%s\t\t",thread->name); 
	printf(" |%c\t",thread->status); 
	printf(" |%d\t\t",thread->priority); 
	printf(" |%d\t\t",thread->nTime); 
	printf(" |%d\t\t",thread->rTime); 
	printf("\n"); 
}
 
void check() /*查看进程*/ 
{ 
	PCB *thread; 
	printf("\n **** 当前正在运行的进程是:%s",t->name); /*显示当前运行进程*/ 
	display(t); 
	thread = ready; 
	printf("\n ****当前就绪队列状态为:\n"); /*显示就绪队列状态*/ 
	while (thread != 0) 
	{ 
		display(thread);
		thread = thread->link;
	} 
} 

void destroy() /*撤消进程*/ 
{ 
	printf("\n 进程 [%s] 已完成.\n",t->name); 
	free(t); 
} 

void running() /*就绪进程*/ 
{
	(t->rTime)++; 
	if (t->rTime == t->nTime) 
		destroy();
	else 
	{ 
		(t->priority)--; 
		t->status = 'W'; 
		sort(); 
	} 
}
 
void main() /*主函数*/ 
{ 
	int len,h=0; 
	char ch; 
	create(); 
	len=length(); 
	while ((len != 0) && (ready != 0)) 
	{ 
		ch=getchar(); 
		h++; 
		printf("\n The execute number:%d \n",h); 
		t = ready; 
		ready = t->link; 
		t->link = 0; 
		t->status = 'R'; 
		check(); 
		running(); 
		printf("\n 按任一键继续......"); 
		ch=getchar(); 
	} 
	printf("\n\n 进程已经完成.\n"); 
	ch=getchar(); 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -