Code:
#include<sys/time.h>
#include<stdio.h>
#include<time.h>
#include<string.h>
#include"stdlib.h"
//extern int g_stop_application;
int SetTimer(struct timeval *, int);
int CheckTimer(struct timeval *, int);
int SetTimer(struct timeval *tv, int usec)
{
int msec;
gettimeofday(tv,NULL);
msec=(tv->tv_usec)*1000;
msec +=(usec*1000);
return 1;
}
int CheckTimer(struct timeval *tv, int usec)
{
struct timeval ctv;
gettimeofday(&ctv,NULL);
int msec;
if(((ctv.tv_usec)*1000 >= (tv->tv_usec)*1000) || ((ctv.tv_sec)*0.001 >= (tv->tv_sec)*0.001))
{
gettimeofday(tv,NULL);
msec=(tv->tv_usec)*1000;
msec +=(usec*1000);
return 1;
}
else
return 0;
}
int main()
{
int i,j,temp,check;
struct timeval tv;
char Task[5][8]={ "task_1","task_2","task_3","task_4","task_5" },temp1[5][8];
int msec[5]={60,40,10,50,80};
for(i=0;i<5;i++)
{
printf("The Tasks in scheduler is %s with period %d\n",Task[i],msec[i]);
}
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(msec[i]>msec[j])
{
temp=msec[i];
msec[i]=msec[j];
msec[j]=temp;
strcpy(temp1[i],Task[i]);
strcpy(Task[i],Task[j]);
strcpy(Task[j],temp1[i]);
}
}
}
for(i=0;i<5;i++)
{
printf("The scheduled tasks in scheduler is %s with period %d\n\n",Task[i],msec[i]);
}
SetTimer(&tv,10);
while(1)
{
for(i=0;i<5;i++)
{
check=CheckTimer(&tv,10);
if (check==1)
{
printf("%s starting at %d milli sec\n",Task[i],msec[i]);
}
}
/*if(g_stop_application ==1)
{
break;
}
}
return 0;
}
-- here is my code..
ya i agree with you that a task contain more than a string but here i am developing a application stub so i took string as the reference. and i want my code to be periodic and run the task periodically for different periods setted by the timer this is my requirement and i am failing to do this.