LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-11-2010, 09:20 PM   #1
mark.cooray
LQ Newbie
 
Registered: Nov 2010
Posts: 4

Rep: Reputation: 0
Measuring the Time That a Program is Running in Shell Script


Hi,

I have a script that executed 100000 C Programs, reads the commands from a file and executes them.

There is a requirement to measuer the time that each of this 100K C programs running.

If it exceeds 120 sec I want to Kill that Process.

Any Idea to embed this in my shell script?
 
Old 11-11-2010, 10:19 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
For each one, how about something like this (cannot be used in parallel with others because of race condition between starting the prog and getting its PID)
Code:
function launch_prog {
    local my_child_PID
    prog=$1 
    prog &
    my_child_PID=$!
    sleep 120
    kill $my_child_PID
}
 
Old 11-11-2010, 11:18 PM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
I would only add one thing to catkin's suggestion.

You are launching 100,000 programs. It's very likely that multiple programs will reuse PIDs. If your individual programs finish fast enough, it is conceivable that you could kill the wrong process. Take for example:
Code:
Time   0: Program 1 starts - uses PID 90 - 120 second timer begins
Time 2-4: <Other programs launched>
Time   5: Program 1 completes (timer continues to run)
Time   6: <Other programs launched>
...
Time 115: Program 32769 starts - uses PID 90
Time 120: Original timer for Program 1 ends - kills PID 90 - PID 90 now belongs to Program 32769 - Program 32769 improperly killed after 5 seconds
Depending on how quickly each of the individual programs in your 100,000 run, the above sequence may (or may not) be likely. You may need to perform some additional checking before you kill a specific process (like verifying each PID's start time before killing).

Last edited by Dark_Helmet; 11-11-2010 at 11:20 PM.
 
Old 11-11-2010, 11:58 PM   #4
joec@home
Member
 
Registered: Sep 2009
Location: Galveston Tx
Posts: 290

Rep: Reputation: 70
Quote:
Originally Posted by mark.cooray View Post
Hi,

I have a script that executed 100000 C Programs, reads the commands from a file and executes them.

There is a requirement to measuer the time that each of this 100K C programs running.

If it exceeds 120 sec I want to Kill that Process.

Any Idea to embed this in my shell script?
I can at least point in the right direction regarding parallel time measurement. I had a problem forking off different instances and measuring time and had to do some funky echo commands to make it work. Hope this at least give ssome of the structure you are looking for

Code:
time (
WEBSITE='put_your_webite_here.com' ;\
LOAD='10'
COMMAND(){
    time -p curl $WEBSITE > /dev/null 2>&1 &
} ;\
TIME=$(
    for i in `seq 1 $LOAD` ;\
        do COMMAND ;\
    done  2>&1 \
    |grep real \
    |awk '{sum += $2} END {print sum}';\
) ;\
TIME=$(echo 'scale=4;' $TIME '/' $LOAD | bc) ;\
echo 'The average time for' $LOAD 'web users to access' $WEBSITE 'is' $TIME 'seconds' ;\
unset COMMAND LOAD TIME WEBSITE;\
) ;\
 
Old 11-12-2010, 01:22 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by Dark_Helmet View Post
I would only add one thing to catkin's suggestion.
...

You may need to perform some additional checking before you kill a specific process (like verifying each PID's start time before killing).
Very good point

AFAIK the elapsed time of the process is in the /proc/$$/sched file in the se.exec_start line.
 
  


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
Measuring time elapsed in a program gcc and g++ sd9 Linux - Software 2 07-08-2009 11:38 PM
running a shell script which invokes another shell program!!! raghu123 Programming 1 08-04-2008 10:10 AM
running a C program from shell script jkeertir Linux - Newbie 3 05-19-2008 06:05 AM
Running shell script within a C or C++ program Quantum0726 Programming 2 06-15-2005 10:14 PM
running a program from shell script Suinatsa Programming 10 04-14-2005 12:25 PM


All times are GMT -5. The time now is 05:45 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration