#include<iostream>
#include<vector>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#define READY 1
using namespace std;
typedef struct task_node {
int task_id;
int period;
int wcet;
int arrival_time;
int deadline;
int state;
int ceu;
int slacktime;
}task;
void read_input(vector<task> &);
void print_task_vec(vector<task> &);
bool comparebyperiod(const task&,const task&);
bool comparebydeadline(const task&,const task&);
bool comparebyslacktime(const task&,const task&);
void EDF_Scheduler(vector<task> &task_vec,int sim_time) ;
int main()
{
vector<task> task_vec;
int n=10;
read_input(task_vec);
print_task_vec(task_vec);
EDF_Scheduler(task_vec,n);
}
void read_input(vector<task> &task_vector) {
char input[1024], *tok;
int N,counter,task_id,period,wcet,index;
memset(input,0,1024);
fgets(input,1024,stdin) ;
N = atoi(input);
index = N;
while ( index>0) {
task t;
memset(input,0,1024);
fgets(input,1024,stdin);
tok = strtok(input,",");
counter = 0;
do {
if (counter == 0)
task_id = atoi(tok);
if (counter == 2)
period = atoi(tok);
if (counter == 1)
wcet = atoi(tok);
if (counter > 2){
cout<<"FORMAT ERROR IN INPUT FILE\n";
exit(-1);
}
counter++;
tok = strtok(NULL,",");
}while (tok);
if(counter<2){
cout<<"FORMAT ERROR IN INPUT FILE\n";
exit(-1);
}
t.task_id = task_id;
t.period = period;
t.wcet = wcet;
t.arrival_time = 0;
t.deadline = t.arrival_time + t.period;
t.ceu = 0;
t.slacktime = (t.deadline - 0) - ( t.wcet - t.ceu);
task_vector.push_back(t);
index--;
}
}
void print_task_vec(vector<task> &v)
{
int n = v.size();
int i;
cout<<"----------------------------------\n";
cout<<"Task\tPeriod\tWCET\tatime\tdead\tslack\n";
for (i=0;i<n;i++) {
cout<<v[i].task_id<<"\t"<<v[i].period<<"\t"<<v[i].wcet<<"\t"<<v[i].arrival_time<<"\t"<<v[i].deadline<<"\t"<<v[i].slacktime<<endl;
}
cout<<"----------------------------------\n";
}
void EDF_Scheduler(vector<task> &task_vec,int sim_time) {
sort(task_vec.begin(),task_vec.end(),comparebydeadline);
print_task_vec(task_vec);
int i,time = 0;
int n = task_vec.size();
int curr_process;
for(i=0;i<n;i++) {
task_vec[i].arrival_time = time;
task_vec[i].deadline = task_vec[i].arrival_time + task_vec[i].period;
task_vec[i].state = READY;
task_vec[i].ceu = 0;
}
while (time < sim_time) {
cout<<"TIME "<<time<<endl;
curr_process = 0;
if(curr_process > -1) {
if(task_vec[curr_process].ceu < task_vec[curr_process].wcet) {
task_vec[curr_process].ceu++;
cout<<"\tEXECUTING TASK "<<task_vec[curr_process].task_id<<endl;
}
if (task_vec[curr_process].ceu == task_vec[curr_process].wcet) {
cout<<"\tTASK COMPLETED "<<task_vec[curr_process].task_id<<endl;
task_vec[curr_process].arrival_time += task_vec[curr_process].period;
task_vec[curr_process].deadline = task_vec[curr_process].arrival_time + task_vec[curr_process].period;
task_vec[curr_process].state = READY;
sort(task_vec.begin(),task_vec.end(),comparebyperiod);
task_vec[curr_process].ceu = 0;
}
}
for(i=0;i<n;i++) {
if(task_vec[i].deadline < time) {
cout<<"\tTASK "<<i+1<<" missed deadline"<<endl;
task_vec[i].arrival_time += task_vec[i].period;
task_vec[i].deadline = task_vec[i].arrival_time + task_vec[i].period;
task_vec[i].state = READY;
sort(task_vec.begin(),task_vec.end(),comparebyperiod);
task_vec[i].ceu = 0;
}
}
time++;
sort(task_vec.begin(),task_vec.end(),comparebydeadline);
//print_task_vec(task_vec);
}
print_task_vec(task_vec);
}
pinkesh25gar 0 Newbie Poster
pinkesh25gar 0 Newbie Poster
pinkesh25gar 0 Newbie Poster
deceptikon 1,790 Code Sniper Team Colleague Featured Poster
pinkesh25gar 0 Newbie Poster
deceptikon 1,790 Code Sniper Team Colleague Featured Poster
pinkesh25gar 0 Newbie Poster
Tumlee 42 Junior Poster in Training
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.