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

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;


void read_input(vector<task> &task_vector) {
    char input[1024], *tok;
    int N,counter,task_id,period,wcet,index;
    fgets(input,1024,stdin) ;
    N = atoi(input);
    index = N;
    while ( index>0) {
        task t;
        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";
            tok  = strtok(NULL,",");
        }while (tok);
            cout<<"FORMAT ERROR IN INPUT FILE\n";
        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);


void print_task_vec(vector<task> &v)

    int n = v.size();
    int i;



    for (i=0;i<n;i++) { 

void EDF_Scheduler(vector<task> &task_vec,int sim_time) { 

    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) {
                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;
                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;
                task_vec[i].ceu = 0;



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 :(

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,



Equivalent c language code ?

Edited 3 Years Ago by mike_2000_17: Fixed formatting

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;


Edited 3 Years Ago by Ancient Dragon

This article has been dead for over six months. Start a new discussion instead.