![]() |
|
#1
|
|||
|
|||
Schedule task using C /C++ codeI 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
|
|||
|
|||
Re: Schedule task using C /C++ codeQuote:
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:
Output: Code:
Regards, Dave |
|
#3
|
|||
|
|||
Re: Schedule task using C /C++ codeThanks 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
|
|||
|
|||
Re: Schedule task using C /C++ codeQuote:
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
|
|||
|
|||
Re: Schedule task using C /C++ codeOfcourse , 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
|
|||
|
|||
Re: Schedule task using C /C++ codeQuote:
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:
Quote:
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 GIDBlog
Flickr uploads of IA pictures by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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