额,也不知道算不算是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;
}
}