#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);

}

i need help to resolve vector librabry & its function..dont know how to convert it into c..task_vector.push_back(t) is making me confused

want to convert this program into c language :(

want to convert this program into c language :(

And it only took three posts to get to the root of the problem. :rolleyes: Anyway, you need to recognize that C doesn't have a dynamic array library like C++'s std::vector. So you really have three options:

  1. Find a third party library that does what you want.
  2. Simulate the behavior of std::vector.
  3. Just use an array and make the size "big enough for anything".

#3 is by far the simplest, but it's also wasteful and/or not scalable. However, if this is a homework assignment or otherwise non-critical program then that solution would likely be best.

thnks for replying..

it is non-critical program ..but will u tell me compatible code to follwing lines
line nos 36 & 83..

but will u tell me compatible code to follwing lines
line nos 36 & 83..

There's a lot going in in those lines, it's not as simple a translation as you probably think. That's why I suggested that you replace the vector entirely with an array.

need to convert following lines..i replaced it with arrays,

#include<vector.h>
#include<algorithm>

sort(vector.begin(),vector.end(),period)

Equivalent c language code ?

You'll have to write code manually that will sort the array. You can use Google to find various sorting algorithms you can use.

You can use qsort() in C programs.

You can make an array resize itself by calling realloc() to change its size. The array has to be initially declared as a pointer, something like this: int* array = NULL; You also need to have two more integers, one integer to keep track of the current number of elements in the array, and another to keep track of the next useable element. For example initially allocate 10 elements in the array, when those are used up you have to call realloc() to increse the array size before adding another value to the array. Since reallocating array sizes is time consuming task you should realloc() the array by some constant value to reduce the number of reallocations. The value of that constant value will depend on how large you expect the array to grow.

const int BlockSize = 50; // realloc size 
void allocarray(int** array, int data, int *nItems, int* curSize)
{
    if( (*nItems+1) > *curSize)
    {
        *array = realloc(*array, *curSize+BlockSize); // increase size of array     
        *curSize += BlockSize;
    }
    *(*array+*nItems) = data;
    (*nItems)++;

}
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.