Thread: problem with the scheduler

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    17

    Unhappy problem with the scheduler

    Attached my code for a scheduler that schedules a task periodical. But in my code i have taken different periods and need to set the timer for those periods and i should use single timer for this . (means set the timer for only one time) how i can achieve this please help me soon.
    Attached Files Attached Files
    • File Type: c .c (4.1 KB, 161 views)

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    I might be missing something here but wouldn't your task need to consist of something more than a string? Say a struct with a timestamp, which you add to the end of a queue. Then periodically check the front of the queue if the timestamp of that struct has passed current time. BTW, it's probably better if you paste the code in code tags directly in the forum.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    17
    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.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    I believe a naive implementation would be to create a polling loop, get current time compare it to your tasks, sleep for a bit.

    Code:
    while(1) {
         //get current time
    
         //compare you time stamp to your tasks
    
         //sleep
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a class scheduler
    By tantan777 in forum C Programming
    Replies: 13
    Last Post: 03-09-2011, 10:40 AM
  2. scheduler in linux
    By anjana in forum Linux Programming
    Replies: 6
    Last Post: 05-29-2007, 12:59 AM
  3. new scheduler in linux
    By anjana in forum Linux Programming
    Replies: 1
    Last Post: 05-28-2007, 02:55 AM
  4. AT command (task scheduler)
    By ober in forum Tech Board
    Replies: 1
    Last Post: 07-13-2005, 09:24 AM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM