LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-22-2006, 12:02 PM   #1
hubabuba
Member
 
Registered: Jan 2002
Location: UK
Distribution: Slack 14.1
Posts: 193

Rep: Reputation: 30
How to get the program to terminate after sertain length of time?


Hi!

I want my program to run for 5 minutes (or any other length of time), during which it will be doing a task, then it must terminate after 5 minutes or any other length of time. Is there such function that allows time setting?

I am not shure, but would time() or timer() functions be used for this?

Tnanks
 
Old 04-22-2006, 12:58 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Probably the easiest way is to use a shell script:
1. The script starts the program in the background (&) and saves the PID ($!)
2. The program executes while the script sleeps N seconds
3. At the end of N seconds, the script does a "kill $!"

Here's an example:
Code:
MYPROG &
PID=$!
sleep 5
kill -9 $PID
A second approach is for your C/C++ program to set a signal handler for SIGALRM, then call "alarm ()". Here's an example:
http://www.cs.usfca.edu/benson/cs326.../src/sigalrm.c

'Hope that helps .. PSM
 
Old 04-22-2006, 01:05 PM   #3
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
U might be able to use the time function as well. if doing it in C of course. U could get the current time and then add 5 min to it then have a while loop check it every tick till it matches the correct value.

This might be problematic and there is probably a better way like paul said above.
 
Old 04-23-2006, 01:52 PM   #4
hubabuba
Member
 
Registered: Jan 2002
Location: UK
Distribution: Slack 14.1
Posts: 193

Original Poster
Rep: Reputation: 30
Thanks, sorry I forgot to mention, I'm using C.
 
Old 04-23-2006, 02:29 PM   #5
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Just throwing out another option...

You could make the program multi-threaded. Create a thread that just sleeps the desired amount of time, and then communicates in some manner (global variable, a system "kill" command, or whatever) that time is up to the other thread(s).

It's basically the same as paulsm4's sigalarm example. For me, I never became comfortable with signals and generally avoid them. That's just me though.
 
Old 04-23-2006, 10:19 PM   #6
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
The problem with having the program monitor its own time is that if the program runs wild you can't trust the monitoring.

You might want to do it from outside the program, perhaps using a shell script that runs maybe once a second until a time condition on the program being monitored is satisfied. When that time condition is satisfied, the shell script kills the program and exits.

Perhaps something like this:
Code:
progID="myprog"
mypid=`eval ps ax|grep "$progID"|grep -iv "grep"| awk '{print $1}'`
maxtime="5"
mins=0
killmins=5
while [ $mins -lt $killmins ]; do
  usePID=`eval ps ax|grep "$progID"|grep -iv "grep"| awk '{print $4}'`
  hours=`eval echo "$usePID" | awk -F\: '{print $1}'`
  mins=`eval echo "$usePID" | awk -F\: '{print $2}'`
  sleep 1
done
kill "$mypid"
By the way. I actually tested this and it works. It considers CPU time, not wallclock time.

Last edited by jiml8; 04-23-2006 at 10:22 PM.
 
Old 04-23-2006, 11:06 PM   #7
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
Since I became amused by this script, I wrote another variant of it. This variant runs on wallclock time. It starts the program and kills it after a specified number of seconds.

It could be made more efficient; I should call up the current time just once at the beginning and once each time through the loop, saving it into a variable, then extracting hours minutes and seconds via awk.
Code:
#!/bin/bash
#sample script to start a program, permit it to run for a predefined amount of wallclock time, then kill it.  Specify time in seconds
#
progID="myprog"
$progID &
mypid=`eval ps ax|grep "$progID"|grep -iv "grep"| awk '{print $1}'`
echo "mypid $mypid"
maxtime="5"
mins=0
secs=0
killsecs=5
startsecs=`eval date | awk '{print $4}'| awk -F\: '{print $3}'`
starthours=`eval date |awk '{print $4}'| awk -F\: '{print $1}'`
startmins=`eval date |awk '{print $4}'| awk -F\: '{print $2}'`
while [ $secs -lt $killsecs ]; do
  cursecs=`eval date | awk '{print $4}'| awk -F\: '{print $3}'`
  curhours=`eval date |awk '{print $4}'| awk -F\: '{print $1}'`
  curmins=`eval date |awk '{print $4}'| awk -F\: '{print $2}'`
  secs=$(( (curhours-starthours)*3600 + (curmins-startmins)*60 - startsecs + cursecs ))
  sleep 1
done
kill "$mypid"

Last edited by jiml8; 04-23-2006 at 11:08 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Time Sync Program David@330 Linux - General 3 02-13-2006 04:34 AM
Terminate Hung Program Ingla Linux - General 3 11-15-2005 08:13 AM
storing text in a text file for a specified length of time. mrobertson Programming 7 08-02-2005 10:27 AM
length of "last edited" time synaptical LQ Suggestions & Feedback 12 07-28-2005 03:16 PM
program with thread and time melinda_sayang Programming 2 05-28-2005 04:16 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration