Unix Programming - measureing host loads every 10 sec until an application is finished.

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > July 2004 > measureing host loads every 10 sec until an application is finished.





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author measureing host loads every 10 sec until an application is finished.
pj

2004-07-16, 8:50 pm

Hi,

I want to measure host loads (using system(uptime)) every 10 seconds
while I am running an application which is written in C, and write the
host
load average during the application execution to a file right after
the application terminates.

i.e.,
--- Application is started
--- measure load average (using `uptime`) every 10 seconds
during the execution
--- Application is finished.
--- calculate the load average and print it out

My question is how I can run system(uptime) every 10 seconds during
the application execution using one processor machine? I was thinking
of having two threads, but the thread blocks the other until it is
terminated which is not what I want. I need to run two jobs (uptime
every 10 seconds and an application executable) simultaneously and
measureing uptime should be finished when the application is finished.
any suggestions? thanks for any help in advance
Juha Laiho

2004-07-17, 2:48 am

nsf470@yahoo.com (pj) said:
>I want to measure host loads (using system(uptime)) every 10 seconds
>while I am running an application which is written in C, and write the
>host
>load average during the application execution to a file right after
>the application terminates.
>
>i.e.,
>--- Application is started
> --- measure load average (using `uptime`) every 10 seconds
> during the execution
>--- Application is finished.
> --- calculate the load average and print it out


Print the load avg to where -- to a file?

I'd do that by forking the load-monitoring functionality into a child
process, having that process listen for some signal, and having the main
process to send that signal to the load-monitoring child once the real
processing finishes.

Note that depending on your system you might be able to use getloadavg()
function instead of calling an external program and parsing the output.
--
Wolf a.k.a. Juha Laiho Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)
Jens.Toerring@physik.fu-berlin.de

2004-07-17, 2:48 am

pj <nsf470@yahoo.com> wrote:
> I want to measure host loads (using system(uptime)) every 10 seconds
> while I am running an application which is written in C, and write the
> host
> load average during the application execution to a file right after
> the application terminates.


> i.e.,
> --- Application is started
> --- measure load average (using `uptime`) every 10 seconds
> during the execution
> --- Application is finished.
> --- calculate the load average and print it out


> My question is how I can run system(uptime) every 10 seconds during
> the application execution using one processor machine? I was thinking
> of having two threads, but the thread blocks the other until it is
> terminated which is not what I want. I need to run two jobs (uptime
> every 10 seconds and an application executable) simultaneously and
> measureing uptime should be finished when the application is finished.
> any suggestions? thanks for any help in advance


Do something like this:

1) Install handler for SIGCHLD signal - in the handler you just set
a global variable (should be of type 'volatile sig_atomic_t')
2) fork() and exec() the application you want to really run
3) Have a loop in which you sleep(3) for 10 seconds. When sleep ends
and the signal handler hasn't been run (as you can see from the
value of the global variable) do your system( "uptime >> file" )
and go back to sleep
4) If the SIGCHLD signal has come in (which will also interrupt
the sleep() function) open the file with the uptime-data,
calculate your load average, print it out and exit.

Regards, Jens
--
\ Jens Thoms Toerring ___ Jens.Toerring@physik.fu-berlin.de
\__________________________ http://www.toerring.de
pj

2004-07-18, 5:55 pm

Jens.Toerring@physik.fu-berlin.de wrote in message news:<2ls6liFgaoeqU1@uni-berlin.de>...
> pj <nsf470@yahoo.com> wrote:
>
>
>
> Do something like this:
>
> 1) Install handler for SIGCHLD signal - in the handler you just set
> a global variable (should be of type 'volatile sig_atomic_t')
> 2) fork() and exec() the application you want to really run
> 3) Have a loop in which you sleep(3) for 10 seconds. When sleep ends
> and the signal handler hasn't been run (as you can see from the
> value of the global variable) do your system( "uptime >> file" )
> and go back to sleep
> 4) If the SIGCHLD signal has come in (which will also interrupt
> the sleep() function) open the file with the uptime-data,
> calculate your load average, print it out and exit.
>
>



Thanks for great replies. I tried to implement the program using
signal, but I found that the load is recorded twice when myprog is
started, and once when myprog is ended.. I couldn't figure out why
it's not recording the load every 10 seconds.. following is the
program code and the load output

------------------------------
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>

volatile sig_atomic_t done =0;

void handler (int sig)
{
if (sig == SIGCHLD)
done = 1;
}

int main () {

pid_t childPId;

char *prog = "/home/jangs/ligo/geo-inst-1.2_search_region_DEV/local_execution/ComputeFStatistic_i";
char *newargv[] = {"/home/jangs/ligo/geo-inst-1.2_search_region_DEV/local_execution/ComputeFStatistic_i",
"--Freq", "150.000000", "--FreqBand", "0.008", "--Alpha", "4.648662",
"--AlphaBand", "0.0005", "--dAlpha", "1e-05", "--Delta", "-0.506131",
"--DeltaBand", "5e-05", "--dDelta", "5e-05", "--dFreq", "1e-07",
"--Fthreshold", "14.237000", "--EphemDir", ".", "--EphemYear", "03",
"--IFO", "1", "--DataDir", ".", "--BaseName", "out", 0};

switch (childPId = fork ()) {
case -1: printf("fork failed\n"); exit (-1);
case 0:
execv (prog, newargv);
default:
signal (SIGCHLD, handler);
while (!done) {
system ("uptime >> data"); sleep(10);
}
}
}

---------------------------

data file (load should be recorded every 10 seconds during `prog`
execution, but it writes the load avg twice when `prog` is started,
and once when it ends..
% more data
17:00:09 up 9 days, 3:33, 14 users, load average: 0.82, 0.68, 0.37
17:00:09 up 9 days, 3:33, 14 users, load average: 0.82, 0.68, 0.37
17:02:53 up 9 days, 3:36, 14 users, load average: 1.04, 0.86, 0.49
Jens.Toerring@physik.fu-berlin.de

2004-07-18, 5:55 pm

pj <nsf470@yahoo.com> wrote:
> Thanks for great replies. I tried to implement the program using
> signal, but I found that the load is recorded twice when myprog is
> started, and once when myprog is ended.. I couldn't figure out why
> it's not recording the load every 10 seconds.. following is the
> program code and the load output


The problem is probably that system(3) also fork()s child processes,
and you don't distinguish between the SIGCHLD signals from these
children from the one you really are interested in. That can't be
the complete explanation of your result but I also reproduce them
(with your program I only got a single line in the 'data' file).
Here's a version of your program that should do the trick (at least
if signal() does not reset the signal handler to the default, in
that case you better use sigaction(), which is recommended anyway):

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>

volatile sig_atomic_t done = 0;
pid_t child_pid;

void handler( int sig )
{
pid_t dead_pid;

while ( ( dead_pid = waitpid( -1, NULL, WNOHANG ) ) > 0 )
if ( dead_pid == child_pid )
done = 1;
}

int main ( void )
{
char *prog = "/usr/local/bin/fsc2";
char *newargv[ ] = { prog,
"/home/jens/Lab/fsc2/tests/cw_simul.edl",
NULL };

signal( SIGCHLD, handler );

switch ( child_pid = fork( ) )
{
case -1:
printf( "fork failed\n" );
exit( EXIT_FAILURE );

case 0:
execv ( prog, newargv );
_exit( EXIT_FAILURE );

default:
while ( ! done )
{
system ( "uptime >> data" );
sleep( 10 );
}
}

return EXIT_SUCCESS;
}

Regards, Jens
--
\ Jens Thoms Toerring ___ Jens.Toerring@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com