#include <iostream>
#include <list>
#include <string>
using namespace std;
#include <time.h>
#include <stdio.h>
/**********************************************************************************/
struct PCB_time
{
int PCB_time_m;
int PCB_time_s;
};
struct PCB
{
int pid;
PCB_time arrive_time;
int request_time; //minitues
PCB_time start_time;
};
void Display(list<PCB> PCB_list);
void main( void )
{
list<PCB> PCB_list;
PCB thisPCB;
time_t now;
time(&now);
struct tm when;
PCB_time showTime;
showTime.PCB_time_m = 0;
showTime.PCB_time_s = 0;
int process_num = 0;
cout << "input the process num\n";
cin>>process_num;
cout <<"please input the infomation the request time is seconds\n";
while(process_num--)
{
time(&now);
when = *localtime( &now );
cin>>thisPCB.pid;
cin>>thisPCB.request_time;
thisPCB.arrive_time.PCB_time_m = when.tm_min;
thisPCB.arrive_time.PCB_time_s = when.tm_sec;
PCB_list.push_back(thisPCB);
}
PCB_list.front().start_time = PCB_list.front().arrive_time;
while(1)
{
/***************************show the processes********************************************/
time(&now);
when = *localtime( &now );
if(when.tm_min > showTime.PCB_time_m ||
(when.tm_min == showTime.PCB_time_m && when.tm_sec >= (showTime.PCB_time_s + 1)))
{
if(PCB_list.size() == 0)
{
break;
}
Display(PCB_list);
showTime.PCB_time_m = when.tm_min;
showTime.PCB_time_s = when.tm_sec;
}
/***************************show the processes********************************************/
while(1)
{
PCB_time endTime;
time(&now);
when = *localtime( &now );
if(PCB_list.size() == 0)
{
break;
}
/***************************get the ended time**********************************/
endTime.PCB_time_m = PCB_list.front().start_time.PCB_time_m;
endTime.PCB_time_s = PCB_list.front().start_time.PCB_time_s;
if(PCB_list.front().start_time.PCB_time_s + PCB_list.front().request_time >=60)
{
endTime.PCB_time_m++;
endTime.PCB_time_s = PCB_list.front().start_time.PCB_time_s
+ PCB_list.front().request_time - 60;
}
else
{
endTime.PCB_time_s = endTime.PCB_time_s
+ PCB_list.front().request_time;
}
/********************************end getting the ended time*********************/
time(&now);
when = *localtime( &now );
if((when.tm_min > endTime.PCB_time_m) || ( when.tm_sec >= endTime.PCB_time_s))
{
PCB_list.pop_front();
PCB_list.front().start_time.PCB_time_m = when.tm_min;
PCB_list.front().start_time.PCB_time_s = when.tm_sec;
}
break;
}
}
}
void Display(list<PCB> PCB_list)
{
cout<<"now the processes are:\n";
for(list<PCB>::iterator it = PCB_list.begin(); it!= PCB_list.end(); it++)
{
cout<<"("<<(*it).pid << " " <<(*it).arrive_time.PCB_time_m << ":"
<<(*it).arrive_time.PCB_time_s <<" "
<<(*it).request_time <<")\n";
}
}