#include <stdio.h>
#include <stdlib.h>
#include<LPC214x.h>
#define ledon IO1SET
#define ledoff IO1CLR
void delay(int x);
#define READY 1
typedef int (*compfn)(const void*, const void*);
struct task {
int task_id;
int period;
int wcet;
int arrival_time;
int deadline;
int state;
int ceu;
};
int sim_time=10;
struct task array[3] = { { 1,4,1,0,4,1,0 },
{ 2,6,2,0,6,1,0 },
{ 3,12,3,0,12,1,0 } };
void printarray(void);
int compare(struct task *, struct task *);
void EDF_scheduler(void);
int main(void)
{
IODIR1 = 0x00FF0000;
printf("List before sorting:\n");
printarray();
qsort((void *) &array, // Beginning address of array
3, // Number of elements in array
sizeof(struct task), // Size of each element
(compfn)compare ); // Pointer to compare function
printf("\nList after sorting:\n");
printarray();
EDF_scheduler();
}
int compare(struct task *elem1, struct task *elem2)
{
if ( elem1->deadline < elem2->deadline)
return -1;
else if (elem1->deadline > elem2->deadline)
return 1;
else
return 0;
}
void printarray(void)
{
int i;
for (i = 0; i < 3; i++)
printf("%d %d %d %d %d %d %d %d\n",
i+1, array[i].task_id, array[i].period,array[i].wcet,
array[i].arrival_time,array[i].deadline,array[i].state,array[i].ceu);
}
void EDF_scheduler(void)
{
while(1)
{
int i,time=0;
int curr_process;
while(time<sim_time)
{
printf("%d\n",time);
curr_process=0;
if(curr_process > -1)
{
if(array[curr_process].ceu < array[curr_process].wcet )
{
array[curr_process].ceu++;
if(array[curr_process].task_id ==1 )
{
ledon = (1<<16); // Turn ON P1.16 – P1.23
delay(9000);
ledoff= (1<<16); // Turn OFF P1.16 – P1.23
}
if(array[curr_process].task_id ==2 )
{
ledon = (1<<17); // Turn ON P1.16 – P1.23
delay(9000);
ledoff= (1<<17);
}
if(array[curr_process].task_id ==3 )
{
ledon = (1<<18); // Turn ON P1.16 – P1.23
delay(9000);
ledoff= (1<<18);
}
}
if(array[curr_process].ceu == array[curr_process].wcet )
{
if(array[curr_process].task_id ==1 )
{
ledon |= (1<<19); // Turn ON P1.16 – P1.23
delay(9000);
ledoff= (1<<19); // Turn OFF P1.16 – P1.23
}
if(array[curr_process].task_id ==2 )
{
ledon= (1<<20); // Turn ON P1.16 – P1.23
delay(9000);
ledoff= (1<<20);
}
if(array[curr_process].task_id ==3 )
{
ledon = (1<<21); // Turn ON P1.16 – P1.23
delay(9000);
ledoff= (1<<21);
}
array[curr_process].arrival_time +=array[curr_process].period;
array[curr_process].deadline = array[curr_process].arrival_time+array[curr_process].period;
array[curr_process].state = READY;
qsort((void *) &array, // Beginning address of array
3, // Number of elements in array
sizeof(struct task), // Size of each element
(compfn)compare );
array[curr_process].ceu=0;
}
}
for(i=0;i<3;i++)
{
if(array[i].deadline < time)
{
printf("\tTASK %d",i+1);
printf("missed deadline\n");
array[i].arrival_time += array[i].period;
array[i].deadline=array[i].arrival_time + array[i].period;
qsort((void *) &array, // Beginning address of array
3, // Number of elements in array
sizeof(struct task), // Size of each element
(compfn)compare ); // Pointer to compare function
}
}
time++;
qsort((void *) &array, // Beginning address of array
3, // Number of elements in array
sizeof(struct task), // Size of each element
(compfn)compare ); // Pointer to compare function
}
}
}
void delay(int x)
{
unsigned int k,l;
for(k = x;k > 0;k--)
for(l = 0;l < x;l++);
}
pinkesh25gar 0 Newbie Poster
pinkesh25gar 0 Newbie 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.