FCFS的C++实现

额,也不知道算不算是C++实现了...
#include<iostream>
//implementation of the FCFS CPU scheduling algorithm
using namespace std;
typedef struct PCB
{
	int p_id;//process id
	//int start_time;//the time process comes in//感觉不需要这个开始时间,FCFS的话都是按顺序来的
	int run_time;//the time process run 
	char state;//state of the process:R means ready , E means executed, F means finish
	struct PCB* next;// the next PCB 
}pcb;

pcb* initPcb();
//initialize the linked list of PCBs;
//return the pointer to PCBs list

void schedule(pcb* head);
//FCFS

int main()
{
	pcb* head = (pcb *)malloc(sizeof(pcb));//allocate a head pointer  which point to the PCBs list
	head = initPcb();
	schedule(head);
	cin.get();
	cin.get();
	return 0;
	
}


pcb* initPcb()//single linked list initialized
{
	int num;
	cout<<"enter how many processes there are:";
	cin>>num;
	pcb* phead=(pcb*)malloc(sizeof(PCB));
	pcb* p1;
	p1=phead;
	for(int i = 0;i<num;i++)
	{
		pcb* p=(pcb*)malloc(sizeof(pcb));
		cout<<"enter the following information for a PCB";
		cout<<"p_id:"<<endl;
		cin>>p->p_id;
		//cout<<"start_time(int(0~255)):"<<endl;
		//cin>>p->start_time;
		cout<<"run_time(int(0~100)):"<<endl;
		cin>>p->run_time;
		cout<<"state(char(R/E/F)):"<<endl;
		cin>>p->state;
		p1->next=p;
		p1=p;
		
	}
	p1->next=NULL;
	return phead;
}

void schedule(pcb* head)
{
	int cur_time=0;
	pcb* p = (pcb*)malloc(sizeof(pcb));
	p = head;
	p=p->next;
	while(p!=NULL)//the list is not empty
	{
		if(p->state=='R')
		{
			//cur_time=p->start_time;
			cout<<"process "<<p->p_id<<" is running..."<<endl;
			cur_time+=p->run_time;
			p->state='F';//this process is done;
			cout<<"process "<<p->p_id<<" is done!"<<endl;
			
		}
		p=p->next;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值