请使用FCFS算法模拟进程调度。假设系统有n (n>=2) 个进程,给定进程的到达时间以及需要执行的时间长短,给出相应的执行顺序。数据以文件方式给出,文件名为data.fcfs,格式为:
文件共n (N1…Nn)行.
在接下来的n行中有三个数据,第Nn个进程的进程名,到达时间,执行时间
请根据给定数据,根据进程执行顺序输出每个进程的开始时间Tb和结束时间Te到文件outfcfs.json中。
输出格式 : 按照json文件格式输出。
Json文件格式实例:
```
{
‘进程名1‘ : {
‘Tb’ : t1 ,
‘Te’: t2 ,
},
‘进程名2‘ : {
‘Tb’ : t1 ,
‘Te’: t2 ,
},
{
…
},
…
}
`
#include <iostream>
#include <queue>
#include <stdio.h>
using namespace std;
struct node{
char name[2];
int arrive;
int server;
bool operator <(const node & a)const
{
return arrive > a.arrive;
}
}ap[6];
int main()
{
freopen("data.fcfs","r",stdin);
priority_queue<node> q;
int i=0;
while(cin>>ap[i].name)
{
cin>>ap[i].arrive;
cin>>ap[i].server;
q.push(ap[i]);
cout<<ap[i].name<<ap[i].arrive<<ap[i].server<<endl;
}
int time =0;
cout<<"{\n\n";
while(!q.empty())
{
int start;
int finish;
node a = q.top();
q.pop();
time = max(time,a.arrive);
start = time;
time +=a.server;
finish = time;
cout<<" '"<<a.name<<"':{\n";
cout<<"\t'Tb':"<<start<<",\n";
cout<<"\t'Te':"<<finish<<",\n\t},\n";
}
cout<<"\n}";
fclose(stdin);
return 0;
}