GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 22-Nov-2007, 06:10
gbellair gbellair is offline
New Member
 
Join Date: Nov 2007
Posts: 3
gbellair is on a distinguished road

Schedule task using C /C++ code


I have an executable(written in C/C++) which is running all the time. It is user interactive.Now within this executable, I need to perform a particular function, which will need to be done everyday once.. To be precise, everyday at a scheduled time, the executable needs to read a particular file and copy the entries of the file into an array(this file gets upadated everyday at a particular time).So now whenever this executable needs the file data, there shouldnot be the necessity to read that file, everytime , but simply read the array and thus saving time.
So can any of you help me to know, how to write a code in C/C++, such that within the executable, a particular function(which reads the file and copy its contents to array) is called everyday at the scheduled time.
I know shellscript wud be easy with cron.. But I am clueless in C.
  #2  
Old 22-Nov-2007, 13:11
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,693
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Schedule task using C /C++ code


Quote:
Originally Posted by gbellair
...I know shellscript wud be easy with cron...
It's none of my business why you don't want to use the remarkably powerful and flexible tool that comes with your system, so I won't ask...


I assume you are using Linux or some UNIX-like system.

Here's an example in C that runs on my Linux system. The couple of non-standard library functions should be the same on other Linux/GNU systems. If for some reason things aren't the same on yours, you can ask again.

My little example uses a SIGALRM signal created by an interval timer. You can put whatever periodic task you need to perform into the handler function if you want. The bottom line is that you can tell the system to create a signal at some time in the future, and deal with it as it happens, without having to constantly check system time inside your main program loop.

CPP / C++ / C Code:
/* 
 * A demo program for an interval timer.
 * Works for me on my various Linux systems.
 * (Your Mileage May Vary.)
 *
 * davekw7x
 *
 */

#include <stdio.h>
#include <string.h>
#include <time.h>

#include <signal.h>
#include <sys/time.h>

/* 
 * I'll make this global so that the alarm handler
 * function can change the interval if necessary
 */
struct itimerval it;


/* Put stuff here to perform periodic task.
 * Note that you might have to change the values
 * in the itimerval struct to compensate for
 * variations in "real" time-of-day time due
 * to the inexactness of Linux system time
 * versus wall clock time
 */
void sigalrm_handler(int sig)
{
    static unsigned count = 0;

    printf("Alarm number %5u at %d\n", ++count, time(0));

}

int main()
{
    char buffer[BUFSIZ];


    it.it_value.tv_sec     = 1;       /* start in 1 second      */
    it.it_value.tv_usec    = 0;
    it.it_interval.tv_sec  = 5;       /* repeat every 5 seconds */
    it.it_interval.tv_usec = 0;

    signal(SIGALRM, sigalrm_handler); /* Install the handler    */


    /* Here's your main program loop. The alarm handler
     * function does its thing regardless of this
     */
    printf("Started timer at %d\n\n", time(0));
    setitimer(ITIMER_REAL, &it, NULL);/* turn on interval timer */

    printf("Type any old thing at all.");
    printf("  To exit the program, enter a blank line.\n\n");
    while (fgets(buffer, sizeof(buffer), stdin) && (strlen(buffer) > 1)) {
        printf("You entered: %s\n", buffer);
    }

    printf("Bye\n");
    return 0;
}

Output:
Code:
Started timer at 1195762247 Type any old thing at all. To exit the program, enter a blank line. Alarm number 1 at 1195762248 This is a test You entered: This is a test Alarm number 2 at 1195762253 Another line You entered: Another line Alarm number 3 at 1195762258 Alarm number 4 at 1195762263 Bye

Regards,

Dave
  #3  
Old 26-Nov-2007, 00:13
gbellair gbellair is offline
New Member
 
Join Date: Nov 2007
Posts: 3
gbellair is on a distinguished road

Re: Schedule task using C /C++ code


Thanks Dave..
Well, I probably have got an idea what I shud be doing.. I still have to code to know if I am able to use the 'signal' concept.
However, I didnt get you when you mean other effective tools used in my System!! Well, the code is core C/C++ on UNIX platform.. and we dont use any other external S/W in our System.
Once again Thanks for giving the startup idea.. Probably I will come back to this thread, once I am through coding.

regards
GB
  #4  
Old 26-Nov-2007, 07:41
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,693
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Schedule task using C /C++ code


Quote:
Originally Posted by gbellair
...other effective tools used in my System
You don't have cron? Then why did you mention it?

Also, different operating systems (Linux is not UNIX, you know, and not all UNIX systems are the same) may have different ways to handle signals, alarms, interval timers, etc. So, since you don't have the same setup as mine, some experimentation might be required. (I have no way of testing the code that I posted other than with various Centos and Fedora Linux systems.)

Regards,

Dave
  #5  
Old 26-Nov-2007, 22:54
gbellair gbellair is offline
New Member
 
Join Date: Nov 2007
Posts: 3
gbellair is on a distinguished road

Re: Schedule task using C /C++ code


Ofcourse , I have cron ,for its HP-UX on which my code runs.. But I am not sure if I can use cron , as it has a diff functionality. Its an OS command to make a particular process start at a scheduled time. But my reqt was that,within an already running process, I need to do a certain function at the scheduled time. Pardon my ignorance, but do you think I could use cron in these circumstances?
Also, I am working with 'SIGALRM' and it quite works perfect. But I am facing a minor problem. My scheduled task shud be done at 4AM in mrng, since users should not be affected. Since, I would not know when my executable would be started ( as we only develop here and then send the executable to the production) , I am not sure of what should the value of it_value.tv_sec should be? Is there anyway , I make know to code that timer should initially start at 4am with an interval of 24 hrs??

Thanks in Adv
GB
  #6  
Old 27-Nov-2007, 08:22
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,693
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Schedule task using C /C++ code


Quote:
Originally Posted by gbellair
But my reqt was..
I didn't understand, exactly, that this was a requirement. (My fault for not reading more carefully.)

I mean, if your program is really running "all the time," then whether your program schedules the other task or cron schedules the other task didn't seem important to me, but maybe that's a bad assumption.

Quote:
Originally Posted by gbellair
but do you think..]
I still don't know, exactly, what you can use...
Quote:
Originally Posted by gbellair
My scheduled task shud be done ...

There are standard library functions that let you convert from the system time (the number of seconds since the epoch, returned by the time() function) to conventional calendar time (year, month, day, hours, minutes, seconds) and vice-versa.

The functions are prototyped in <time.h>.

Check the man page for localtime(). That can get you the "wall clock" time (hours, minutes, seconds) for the current system time. Simply calculate the number of seconds until the next desired execution time for your task and set the alarm.

Regards,

Dave
 
 

Recent GIDBlogFlickr uploads of IA pictures by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Classes and allocating memory BlueFireCO. C++ Forum 13 26-Jul-2007 20:31
How to sort random access file? wmmccoy0910 C Programming Language 12 04-Sep-2006 03:40
Here it is again! 35% - 40% off For Life! my-e-space Web Hosting Advertisements & Offers 0 20-Apr-2006 14:48
User defined headers davis Miscellaneous Programming Forum 6 16-Feb-2006 18:40
Problem with int mixed with char,... leitz C++ Forum 17 07-Dec-2004 20:56

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:39.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.