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

📄 suanfa.cpp

📁 C编写的进程调度算法
💻 CPP
字号:
// Suanfa.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


#define MAX 5
#include<stdio.h>
#include<stdlib.h>

int total_time=20;
int time_slice=3;

typedef struct process { // 进程控制块
	char pname[10];
	int WaitTime;
	int BurstTime;
	int priority; // 数字越小优先级越高
	struct process *next;
}PROCESS;

//typedef struct process PROCESS;

PROCESS * in_queue(PROCESS *head,PROCESS *p); //声明

PROCESS *init() //进程初始化
{
	int i=0;
	char a;
	PROCESS *head_new; //队列的队头
	head_new=(struct process*)malloc(sizeof(struct process));
	if(!head_new) exit(1);
	head_new=NULL;


	do
	{
		struct process *s;
		printf("initialize the process:\n");
		s=(struct process *)malloc(sizeof(struct process));
		if(!s) exit(1);
		printf("please input the pname:WaitTime: BurstTime: priority:\n");
		scanf("%c",&(s->pname));
		scanf("%d",&(s->WaitTime));
		scanf("%d",&(s->BurstTime));
		scanf("%d",&(s->priority));
		s->next=NULL;
		in_queue(head_new,s);
		i++;

		printf("do u want to insert process more ?? 'Y'or'N'\n");
		printf("----------------------------------------'\n");

		scanf("%c",&a);
		scanf("%c",&a);
		// if(a=='Y'||a=='y') continue;
		// else if(a=='N'||a=='n') break;
	}while((i<MAX) &&(a=='Y'||a=='y'));

	return head_new;
}

///////////////////////////////////////////////////////////

PROCESS *in_queue(PROCESS *head,PROCESS *p) //入队函数
{
	if(head==NULL)
	{
		head=p;
		p->next=NULL;
	}
	else 
	{
		p->next=head;
		head=p;
	}
	// printf("the process insert into the mothball queue :\n");
	return head;

}

/////////////////////////////////////////////////////////////
/*
void new_queue() //后备队列 先来先服务方式进入就绪
{

return *head_new;
}
*/

PROCESS *FCFS_process()
{
	PROCESS *p,*q,*a; //a用来记录选中结点的前一个结点
	q=p=init(); //这里是不是有个问题??
	while(p->next!=NULL)
	{
		a=p;
		if(p->WaitTime>=q->WaitTime)
		{
			q=p;
			p=p->next;
		}
	}

	q->WaitTime--;
	if(q->WaitTime==0) //如果等待时间为0则把该进程从后备队列中移除
	{
		a->next=p->next;
		free(p);
	}
	return q; //选择等待时间最久的)
}


//////////////////////就绪队列,入口函数为就绪队列的头指针/////////////


int count=0;
PROCESS *ready_queue(PROCESS *head) //就绪队列 优先级进入运行 4道
{ 
	PROCESS *p;
	while(count<4)
	{
		p=FCFS_process();
		p->next=head->next;
		head=p;
		count++;

		printf("the process has inserted into the ready queue :\n");
	}
	return head;
}


//insert_ready() //
PROCESS *high_priority(PROCESS *P) //选择优先级最高的进程
{
	PROCESS *q,*p; //问题,入口形参中
	q=p;

	while(p->next!=NULL)
	{ 
		if(q->priority>p->priority)
		q=p;
		p=p->next;
	}
	return q;
}


PROCESS *pick_ready(PROCESS *a) //从就绪队列中选择进程运行
{
	PROCESS *p=ready_queue(a);
	PROCESS *b=high_priority(p);
	return b;
}

void run(PROCESS *a) //运行一个时间片
{
	while((time_slice>0)&&(a->BurstTime>0)) //指针用-> 变量用.
	{
		printf("the process %c is runing",a->pname);
		printf("the process BurstTime time is %d ",a->WaitTime);
		printf("the process BurstTime time is %d ",a->BurstTime);
		printf("the process priority is %d ",a->priority);
		a->BurstTime--;
		time_slice--;
	} 
	a->priority--;
	total_time--;
	/*
	if(a->runtime>0)
	return a;
	else return NULL;*/
}

void main()
{
	PROCESS *p;
	PROCESS *head=init();
	while(total_time!=0)
	{
		ready_queue(head);
		p=pick_ready(head);

		if(p->BurstTime==0)
		{
			p=p->next;
			free(p);
			count--;
		}
		run(p);
		time_slice=3;

	}
}

⌨️ 快捷键说明

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