处理机调度算法C++实现

一、实验目的

在多到程序或多任务系统中,系统同时处于就绪状态的进程有若干个。也就是说能运行的进程数远远大于处理机个数。为了系统中的各进程有条不紊的运行,必须选择某种调度策略,以选择一进程占用处理机。要求学生设计一个模拟单处理机调度的算法,以巩固和加深处理机调度的概念。

二、 实验内容

处理机管理是操作系统中非常重要的部分。为深入理解进程管理部分的功能,设计几个调度算法,模拟实现处理机的调度。

FCFS(先来先服务)

#include<iostream>
#include<string>
#define N 5
using namespace std;
int time=0;
int sum_turnaround_time=0;

 class PCB
 {
     public:
        PCB *point;
        string pname;
        int run_time;
        int has_run_time;
        int arrive_time;
        char pstate;
 };

 void Pqueue(PCB *head,int &pcount, PCB &p)
 {
     if(head==NULL)
    {
        head=&p;
    }
     else
     {
         head[pcount-1].point=&p;
     }
 }
 void run(PCB *head,int &pcount)
 {
        time++;
        if(time<head->arrive_time)
            time=head->arrive_time;
        head->run_time--;
        head->has_run_time++;
        cout<<head->pname<<" is running, and it has run "<<head->has_run_time<<", and the rest time is "<<head->run_time<<endl;
        if(head->run_time==0)
        {
            head->pstate='C';
            cout<<"the "<<head->pname<<" has finished, and its turnaround time is "<<time-head->arrive_time<<endl;
            sum_turnaround_time+=time-head->arrive_time;
            for(int i=0;i<pcount;i++)
                if(head[i].pstate=='R')
                    cout<<"The process "<<head[i].pname<<" is ready."<<endl;
            cout<<"----------------------------------------------------------"<<endl;
            pcount--;
            if(pcount==0)
                cout<<"The all process have finished!"<<endl;

            else
            {
                head=head->point;
                run(head, pcount);
            }
        }
        else
            run(head, pcount);


 }

 int main()
 {
     PCB* head=new PCB();
     head=NULL;

     int pcount=0;
     PCB P[10];
     for(int i=0; i<N; i++)
     {

         if(i==0)
         {
             cout<<"please input a process's name: ";
             cin>>P[i].pname;
             cout<<"please input a process's time of arriving: ";
             cin>>P[i].arrive_time;
             cout<<"please input a process's time of running: ";
             cin>>P[i].run_time;
             cout<<"----------------------------------------------------------"<<endl;
             P[i].pstate='R';
             P[i].point=NULL;
             P[i].has_run_time=0;
             pcount++;

             head=&P[i];
         }
         else
         {
             cout<<"please input a process's name: ";
             cin>>P[i].pname;
             cout<<"please input a process's time of arriving, and it should be later than "<<P[i-1].arrive_time<<" : ";
             cin>>P[i].arrive_time;
             cout<<"please input a process's time of running: ";
             cin>>P[i].run_time;
             cout<<"----------------------------------------------------------"<<endl;
             P[i].pstate='R';
             P[i].point=NULL;
             P[i].has_run_time=0;
             pcount++;

             head[pcount-2].point=&P[i];
         }
     }

     run(head, pcount);
     cout<<"----------------------------------------------------------"<<endl<<"The average turnaround time is "<<sum_turnaround_time/N<<endl;
     return 0;
 }

实验结果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

动态优先级调度

#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
#define N 5
using namespace std;

int stime = 0;

 class PCB
 {
     public:
        PCB *point;
        string pname;
        int run_time;
        int prank;
        int runned_time;
        char pstate;

 };

bool cmp(PCB a, PCB b)
{
    return a.prank<b.prank;
}

 void run(PCB *head, PCB* running_process, int &pcount)
 {

        cout<<running_process->pname<<" is running and it has run "<<running_process->runned_time<<" and the rest stime is "<<running_process->run_time<<" and its rank is "<<running_process->prank<<endl;

        running_process->run_time--;
        running_process->prank++;
        stime++;
        running_process->runned_time++;

        int temp=0;
        if(pcount>1)
        {

            cout<<"these processes is ready: ";
            for(int i=1; i<pcount; i++)
                cout<<head[i].pname<<" ";
            cout<<endl;
            sort(head, head+pcount, cmp);
            for(int i=1; i<pcount; i++)
            {
                head[i-1].point=&head[i];
            }
            running_process=head;


            /*
            for(int i=1; i<pcount; i++)
                if(head[temp].prank>head[i].prank)
                    temp=i;
            running_process=&head[temp];
            */

        }




        if(running_process->run_time==0)
        {
            running_process->pstate='C';
            //cout<<setw(20)<<setfill(' ')<<left<<head->pname<<setw(20)<<setfill(' ')<<left<<stime<<setw(20)<<setfill(' ')<<left<<stime<<setw(20)<<setfill(' ')<<left<<stime+head->run_time<<endl;
            cout<<running_process->pname<<" is finished!"<<endl;
            pcount--;
            if(pcount!=0)
            {
                head=head->point;
                running_process=head;
                run(head, running_process, pcount);
            }
            else
                cout<<"The all process have finished!"<<endl;
        }
        else
            run(head, running_process, pcount);


 }

int main()
 {
     PCB* head=new PCB();
     head=NULL;

     PCB* running_process=new PCB();
     running_process=NULL;

     int pcount=0;
     PCB P[N];
     for(int i=0; i<N; i++)
     {

        cout<<"please input a process's name: ";
        cin>>P[i].pname;
        cout<<"please input a process's rank: ";
        cin>>P[i].prank;
        cout<<"please input a process's stime of running: ";
        cin>>P[i].run_time;
        cout<<"----------------------------------------------------------"<<endl;
        P[i].pstate='R';
        P[i].point=NULL;
        P[i].runned_time=0;
        pcount++;
     }

     sort(P, P+pcount, cmp);
     head=&P[0];
     running_process=&P[0];

     for(int i=1; i<pcount; i++)
     {
         P[i-1].point=&P[i];
     }
     //cout<<setw(20)<<setfill(' ')<<left<<"process"<<setw(20)<<setfill(' ')<<left<<"start_time"<<setw(20)<<setfill(' ')<<left<<"end_time"<<setw(20)<<setfill(' ')<<left<<"zhouzhuan"<<endl;
     run(head, running_process, pcount);
     return 0;
 }

实验结果(动态展示处理过程)

在这里插入图片描述
在这里插入图片描述

时间片轮转

//书上的代码
#include<stdio.h>
#include <iostream>
#include<conio.h>

using namespace std;

typedef struct pcb {
	char pname[50];		//进程名
	char state;			//进程状态
	int runtime;		//估计运行时间
	int arrivetime;		//到达时间
	struct pcb* next;	//链接指针
}PCB;

PCB head_input;
PCB head_run;
PCB* pcb_input;
static char R = 'r', C = 'c';
unsigned long current;				//记录系统当前时间
void inputprocess();				//建立进程函数
int readyprocess();					//建立就绪队列函数
int readydata();					//判断进程是否就绪函数
int runprocess();					//运行进程函数
FILE* f;

//定义就绪队列函数
int readyprocess() {
	while (1) {
		if (readydata() == 0)			//判断是否就绪函数
			return 1;
		else
			runprocess();				//运行进程函数
	}
}

//定义判断就绪队列是否有进程函数
int readydata() {
	if (head_input.next == NULL) {
		if (head_run.next == NULL)
			return 0;
		else
			return 1;
	}
	PCB* p1, * p2, * p3;
	p1 = head_run.next;
	p2 = &head_run;
	while (p1 != NULL) {
		p2 = p1;
		p1 = p2->next;
	}
	p1 = p2;
	p3 = head_input.next;
	p2 = &head_input;
	while (p3 != NULL) {
		if (((unsigned long)p3->arrivetime <= current) && (p3->state == R)) {
			printf("Time slice is %8d(time %4d);Process %s start,\n",
				current, (current + 500) / 1000, p3->pname);
			fprintf(f, "Time slice is %8d(time %4d);Process %s start,\n",
				current, (current + 500) / 1000, p3->pname);
			p2->next = p3->next;
			p3->next = p1->next;
			p1->next = p3;
			p3 = p2;
		}
		p2 = p3;
		p3 = p3->next;
	}
	return 1;
}

//定义运行进程函数
int runprocess() {
	PCB* p1, * p2;
	if (head_run.next == NULL) {
		current++;
		return 1;
	}
	else {
		p1 = head_run.next;
		p2 = &head_run;
		while (p1 != NULL) {
			p1->runtime--;
			current++;
			if (p1->runtime <= 0) {
				printf("Time slice is %8d(time %4d);Process %s end. \n", current, (current + 500) / 1000,
					p1->pname);
				fprintf(f,"Time slice is %8d(time %4d);Process %s end. \n", current, (current + 500) / 1000,
					p1->pname);
				p1->state = C;
				p2->next = p1->next;
				delete p1;
				p1 = NULL;
			}
			else {
				p2 = p1;
				p1 = p2->next;
			}
		}
		return 1;
	}
}

void inputprocess() {
	PCB* p1, * p2;
	int num;
	unsigned long max = 0;
	printf("How many process do you want to run:");
	fprintf(f,"How many process do you want to run:");
	scanf("%d", &num);
	fprintf(f, "%d\n", &num);
	p1 = &head_input;
	p2 = p1;
	p1->next = new PCB;
	p1 = p1->next;
	for (int i = 0; i < num; i++) {
		printf("No. %3d process input pname:", i + 1);
		fprintf(f, "No. %3d process input pname:", i + 1);
		scanf("%s", p1->pname);
		fprintf(f, "%s\n", p1->pname);
		printf("                      runtime:");
		fprintf(f, "                      runtime:");
		scanf("%d", &(p1->runtime));
		fprintf(f, "%d\n", &(p1->runtime));
		printf("                      arrivetime:");
		fprintf(f, "                      arrivetime:");
		scanf("%d", &(p1->arrivetime));
		fprintf(f, "%d\n", &(p1->arrivetime));
		p1->runtime = (p1->runtime) * 1000;
		p1->arrivetime = (p1->arrivetime) * 1000;
		p1->state = R;
		if ((unsigned long)(p1->arrivetime) > max)
			max = p1->arrivetime;
		p1->next = new PCB;
		p2 = p1;
		p1 = p1->next;
	}
		delete p1;
		p1 = NULL;
		p2->next = NULL;
}

//定义建立进程函数
void inputprocess1() {
	PCB* p1, * p2;
	int num;
	unsigned long max = 0;
	printf("How many process do you want to run:");
	fprintf(f, "How many process do you want to run:");
	scanf("%d", &num);
	fprintf(f, "%d\n", &num);
	pcb_input = new PCB;
	p1 = pcb_input;
	for (int i = 0; i < num; i++) {
		printf("No. %3d process input pname:", i + 1);
		fprintf(f, "No. %3d process input pname:", i + 1);
		scanf("%s", p1->pname);
		printf("                      runtime:");
		fprintf(f, "                      runtime:");
		scanf("%d", &(p1->runtime));
		fprintf(f, "%d\n", &(p1->runtime));
		printf("                      arrivetime:");
		fprintf(f, "                      arrivetime:");
		scanf("%d", &(p1->arrivetime));
		fprintf(f, "%d\n", &(p1->arrivetime));
		//p1->runtime = (p1->runtime);
		//p1->arrivetime = (p1->arrivetime);
		p1->runtime = (p1->runtime) * 1000;
		p1->arrivetime = (p1->arrivetime) * 1000;
		p1->state = R;
		if (i != num - 1) {
			p1->next = new PCB;
			p1 = p1->next;
		}
		else {
			p1->next = pcb_input;
		}
	}
	p1 = pcb_input;
	while (p1->next != pcb_input) {
		printf("process name is %s\n", p1->pname);
		fprintf(f,"process name is %s\n", p1->pname);
		p1 = p1->next;
	}
	printf("process name is %s\n", p1->pname);
	fprintf(f, "process name is %s\n", p1->pname);
}

//定义运行进程函数
void runprocess1() {
	pcb* pre, * cur;
	if (pcb_input == NULL)
		return;
	else {
		cur = pcb_input;
		pre = cur->next;
		while (pre->next != cur) {			//find the last node in the list
			pre = pre->next;
		}

		while ((cur->runtime >= 0) || (cur->next != cur)) {
			if (current < (unsigned long)cur->arrivetime) {
				pre = cur;
				cur = cur->next;
			}
			else {
				if (current == (unsigned long)cur->arrivetime) {
					printf("Time slice is %8d(time %4d);Process %s end. \n", current, (current + 500) / 1000,
						cur->pname);
					fprintf(f,"Time slice is %8d(time %4d);Process %s end. \n", current, (current + 500) / 1000,
						cur->pname);
				}
				cur->runtime--;
				if (cur->runtime < 0) {
					printf("Time slice is %8d(time %4d);Process %s end. \n", current, (current + 500) / 1000,
						cur->pname);
					fprintf(f, "Time slice is %8d(time %4d);Process %s end. \n", current, (current + 500) / 1000,
						cur->pname);

					if (cur == cur->next) {
						delete cur;
						cur = NULL;
						//break;
						return;
					}
					else {
						pre->next = cur->next;
						pcb* tmp = cur;
						delete tmp;
						cur = pre->next;
					}
				}
				else {
					cur->runtime--;
					pre = cur;
					cur = cur->next;
				}

			}
			current++;
		}
			
	}
}

//TO DO 
int main() {
	f = fopen("result.txt", "w");
	printf("\ntime 1=1000 time slice\n");
	fprintf(f,"\ntime 1=1000 time slice\n");
	current = 0;
	inputprocess();
	readyprocess();
	//inputprocess1();
	//runprocess1();
	getch();
	fclose(f);
	return 0;
}

运行结果

在这里插入图片描述

  • 10
    点赞
  • 91
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: 模拟处理机调度算法C是指将任务队列中的作业根据它们的剩余执行时间进行排序,选择剩余执行时间最短的作业来运行,从而达到最短处理时间的目的。 算法C的具体实现步骤如下: 1. 将所有的作业按照顺序加入任务队列。 2. 对于每个时间片,从任务队列中选择剩余执行时间最短的作业。 3. 运行该作业一个时间片,并更新该作业的剩余执行时间。 4. 如果该作业的剩余执行时间为0,则将该作业从任务队列中移除。 5. 重复步骤2-4,直到所有作业都被处理完。 算法C的优点是能够获得最短的处理时间,因为它总是选择剩余执行时间最短的作业来运行。这可以保证作业的响应时间和完成时间都能够得到最小化的保证。 然而,算法C也存在一些缺点。首先,它只考虑了作业的剩余执行时间,而没有考虑作业的优先级、紧迫程度等其他因素。其次,在某些情况下,算法C可能会导致某些作业长时间等待,从而降低了系统的整体效率。 综上所述,算法C是一种以最短处理时间为目标的调度算法。它虽然能够获得较短的响应时间和完成时间,但在某些情况下可能会存在效率问题。因此,在实际应用中,我们需要根据具体的场景和需求选择适合的调度算法。 ### 回答2: 模拟处理机调度算法C是一种常用的调度算法,主要针对多道程序设计环境下的处理机分配问题。该算法根据进程的优先级和执行时间确定处理机的调度顺序。 具体而言,模拟处理机调度算法C的步骤如下: 1. 首先,根据进程的优先级来对进程队列进行排序,优先级高的进程排在前面。 2. 然后,根据排好序的进程队列,按照进程的优先级高低依次分配处理机。即,优先级高的进程先执行。 3. 在进程执行的过程中,根据进程的执行时间进行时间片轮转。当一个进程执行的时间达到了时间片的限制时,将处理机分配给下一个优先级最高的进程。 4. 如果有多个进程具有相同的优先级,可以采用轮流调度的方式,即每个进程轮流分配处理机,每个进程执行同样的时间片。 5. 当所有进程都执行完毕时,结束调度算法。 模拟处理机调度算法C的特点是能够保证高优先级的进程优先执行,从而可以更好地满足多道程序设计环境下的实时性要求。此外,该算法还可以避免某些低优先级进程长时间占用处理机资源,从而提高整个系统的效率。 然而,模拟处理机调度算法C也存在一些不足之处。由于优先级高的进程可能长时间占用处理机资源,可能导致其他低优先级进程的运行较慢甚至出现饥饿现象。另外,该算法没有考虑进程的响应时间,可能导致一些高优先级的进程等待时间过长。 综上所述,模拟处理机调度算法C是一种常用的调度算法,适用于多道程序设计环境。它通过优先级和执行时间的综合考虑来分配处理机资源,可以满足实时性要求,但需要注意饥饿和响应时间的问题。 ### 回答3: 模拟处理机调度算法C是一种优先级调度算法,即根据作业的优先级来确定任务的执行顺序。该算法的核心思想是通过比较作业的优先级,选择优先级最高的作业先执行,以提高处理机的利用率和系统的响应速度。 算法C的步骤如下: 1. 初始化调度队列,将所有作业按照优先级从高到低依次加入队列。 2. 从队列中选择优先级最高的作业,将其分配给处理机执行。 3. 处理机执行该作业一段时间后,检查是否有更高优先级的作业进入队列。 - 如果有,将当前作业暂停,将新作业加入队列并分配给处理机执行。 - 如果没有,继续执行当前作业,直到该作业完成或出现更高优先级的作业。 4. 重复步骤2和步骤3,直到所有作业完成。 模拟处理机调度算法C的优点是能够根据作业的优先级进行灵活调度,确保高优先级作业的及时执行,从而提高系统的性能和响应速度。然而,这种算法可能存在优先级反转的问题,即一个低优先级的长作业会阻塞高优先级的短作业,从而降低系统的效率。 为了解决优先级反转的问题,可以引入抢占式优先级调度算法,如模拟处理机调度算法P。该算法在作业进入队列时会进行实时的优先级比较,如果出现更高优先级的作业,则立即将当前作业暂停,将新作业分配给处理机执行,以保证高优先级作业的及时处理。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值