How to periodically execute a function in C ??


 
Thread Tools Search this Thread
Top Forums Programming How to periodically execute a function in C ??
# 8  
Old 12-18-2007
curious

What poor implementation? Did I make a mistake in the code? Does it not work?
# 9  
Old 12-18-2007
Quote:
Originally Posted by WebKruncher
What poor implementation?
Using a high speed polling loop with a 10 micro second delay (apparently to avoid a race condition) to schedule tasks that occur in seconds.

That means for every second, you will wake up and execute the loop 100,000 times before you will ever find anything to do. If you have to delay 10 seconds as was originally requested, your loop would have spun a million times before it had to do anything useful.

Simply using a variable for the usleep() parameter that was the number of microseconds between now and the next timed event would have been an excellent solution.
# 10  
Old 12-18-2007
I disagree

Quote:
Originally Posted by porter
Using a high speed polling loop with a 10 micro second delay (apparently to avoid a race condition) to schedule tasks that occur in seconds.

This can be tuned anyway the user wants.

That means for every second, you will wake up and execute the loop 100,000 times before you will ever find anything to do.

Simply using a variable for the usleep() parameter that was the number of microseconds between now and the next timed event would have been an excellent solution.
True, but then the function can only act as a cron manager. The object is meant to be used from within a game loop. That seems far more useful than a discrete utility designed only to sleep for specified number of milli seconds. Besides, the simple sleep method may not take into account how long it takes to execute the function before triggering again.

Anyway, it's just one of many ways to accomplish the task at hand. I didn't mean to strike a nerve.

I spent 3 months on something like this a few years ago. QA beat up on it pretty hard, it seemed to hold up well.
# 11  
Old 12-19-2007
Quote:
Originally Posted by WebKruncher
The object is meant to be used from within a game loop.
It may be fine code for a single user/single threaded/single application Playstation but not on a multiuser multiprocessing platform.
# 12  
Old 12-19-2007
Also it's bad practice on a battery powered device as the goal is to do nothing as efficiently as possible, ie let the device go to sleep for the longest period.
# 13  
Old 12-19-2007
timing is everything :)

Quote:
Originally Posted by porter
Also it's bad practice on a battery powered device as the goal is to do nothing as efficiently as possible, ie let the device go to sleep for the longest period.
interesting - I didn't even realize this was for use in a battery powered device. I guess there are a billion things it could be used for, and there are probably billion different ways to do it.

So, what is really the best (hopefully somewhat platform independent) way to accomplish this? Is there one? A project that I worked on a few years ago would have been best done if I had known how to really take advantage of a Sun kernel, but I didn't, and neither did anyone else at the shop, so we just used the clock. It held up well enough, but, we did consider exploring options using traps and signals.
# 14  
Old 12-20-2007
WebKruncher, porter could be more diplomatic, but he does have a point. You are proposing something like:

Code:
trigger=time(NULL)+90;
while (1) {
         if (time(NULL)>trigger) {
                    trigger=time(NULL)+90;
                     do_something();
                     }
         usleep(10);
}

That is not a great way to arrange for something to happen every 90 seconds on any platform. If a process has nothing to do for 90 seconds, then it should do nothing during that 90 seconds. This is true on every platform. The problem is that you are busy looping waiting for 90 seconds to pass. If you replace "usleep(10)" with some code that does useful important work, then you basicly have a technique I would use to display a progress message as a very lengthy program runs. I don't care if the message is several seconds late. But the trigger test is in a main loop that is doing something useful. Take out the time test and my program remains useful. But take out your trigger test and you are left with:
while(1) usleep(10);
That puts quite a load on a system for no good reason.

If there is nothing to do in the loop but wait for 90 seconds to pass, I would simply use:
Code:
while (1) {
        do_something();
        sleep(90);
}

On every system I know, this is a much better solution than spinning in a loop waiting for 90 seconds to pass.

Why do want that loop running during the 90 second delay? Do you realize that the system is doing a lot of work that has no effect? If you are determined that you absolutely must have a loop spinning during the 90 second delay, you could improve the situation quite a bit by simply increasing usleep constant, say to 500000. Or replace it with sleep(1) or even sleep(10). You are still forcing the OS to run your process periodically. But at least not as often.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Execute function as soon as input is provided in menu drive shell script

Hi All, I have a menu driven scripts. As you know while running the script we have to input the option such as 1,2, and 3 to execute function accordingly. but after selecting the input we have to press Enter. My requirement is to execute function as soon as we press the option. Is there... (5 Replies)
Discussion started by: kiran_j
5 Replies

2. Shell Programming and Scripting

Need help to write a function in shell scripting to execute sql files

Hi, I am new to shell scripting and i need to write a automation script to execute sql files. I need to check the table if it is there in system tables and need to write a function to call the .sql files. For ex. I have a.sql,b.sql,c.sql files, where the sql file contains DELETE and INSERT... (1 Reply)
Discussion started by: Samah
1 Replies

3. Shell Programming and Scripting

Want to use function and execute the below query in script.

#!/bin/bash function getDeliveredDispatches(firstDateTime, lastDateTime, limit) { var cursor = db.dispatches.find( {$and: }} ]}, {"deliveryGroupId" : 1, "batchId": 1, "statusHistory.code" : 1} ); var wrongDispatchesIds = ; print("Number of dispatches selected based on filter = " +... (2 Replies)
Discussion started by: neel2462
2 Replies

4. Shell Programming and Scripting

MQ depth Periodically

Hi I am trying to a write a script which gives message queue depth for every 5 mins in a file. Commands that I use are runmqsc QM_Name display ql(*) curdepth Since I can use only MQSC commands I need help on how to fetch the output on to a file after executing display command. (3 Replies)
Discussion started by: jhilmil
3 Replies

5. Shell Programming and Scripting

Execute a function in background and then suspend it

Here is some back ground on the script. The script is to poll an arbitrary number of DB's. To do this I am creating a function that takes the file_path to the DB and the min poll interval as arguments. The function will be called for each DB and then ran in the background. The function I was... (6 Replies)
Discussion started by: ryandavison
6 Replies

6. UNIX for Dummies Questions & Answers

[ASK]execute shell with function in solaris

dear all i need your advice in shell with solaris i have testing script like this #!/usr/bin/bash function test(){ echo "testing only" } ## execute function ## test but if i running always got error like this test.sh: syntax error at line 1: `(' unexpected who can i running this... (7 Replies)
Discussion started by: zvtral
7 Replies

7. Shell Programming and Scripting

understand the function to execute

Gurus, Can you please tell me why this script is executing but i am getting no result...also can you tell me what is this gettime() doing? in a script i wrote this hour.SH -------------------------- gettime() { date '+%H' | { read hour TIME=${hour} } } : ----------------------... (2 Replies)
Discussion started by: RubinPat
2 Replies

8. UNIX for Advanced & Expert Users

Unable to execute a function using trap

I have a script A which calls script B. I wrote a function in script A to be executed when Kill command is issued for script A and I invoke that function using the trap command.The function identifies all child process running under script A (in this case script B) and kills the child process and... (3 Replies)
Discussion started by: smohandass
3 Replies

9. Shell Programming and Scripting

How to execute local function in awk

Hi All, Can you please tell me how to execute local function written in a shell script with awk. i tried with system command but its giving an error. (1 Reply)
Discussion started by: krishna_gnv
1 Replies

10. Shell Programming and Scripting

How can I execute own ksh function in find -exec

Hi, I wrote a smiple ksh function send_notification() { ... } and want to execute it on each file, matched by the find command. I tried: find / -name "*.err" -mtime -8 -exec send_notification {} \; but it doesn't work. What should I do? I work in ksh on Hp-Ux. Regards, Pit (11 Replies)
Discussion started by: piooooter
11 Replies
Login or Register to Ask a Question