Scope confusion

Scope confusion

Post by niels.heirbaut.use.. » Sat, 17 Sep 2005 01:00:45



Hi,

I created a script that registers the current CPU time for a given
process ID and it's children. It uses a global variable to add all the
CPU times to. But the end result is always zero, while in-between
results show the expected values.

The script:

-----

#!/bin/sh

# This program takes a process ID, finds all subprocesses and
calculates the
# TOTAL CPU time used. The program is written as a pure Bourne Shell
script and
# should therefore work on all POSIX systems.

CPUTIME=0

add_cputime()
{
    CPUT=$1
    OIFS=$IFS
    IFS=:
    set -- $CPUT
    CPUTIME=`expr $CPUTIME + 3600 \* $1 + 60 \* $2 + $3`
    IFS=$OIFS

Quote:}

lookup_descendents()
{
    # First register the CPU time of the current process
    cat $TMPFILE | grep "^$1 " | \
    while read LDPID LDPPID LDUSER LDTIME LDARGS ; do
        echo "before : $CPUTIME"
        add_cputime $LDTIME
        echo "after : $CPUTIME"
    done

    # Find the descendents of the current process and register their
CPU times
    cat $TMPFILE | grep " $1 " | \
    while read LDPID LDPPID LDUSER LDTIME LDARGS ; do
        if [ "$LDPID" != "$LDPPID" ] ; then
            lookup_descendents $LDPID
        fi
    done

Quote:}

# Argument handling
if [ "$1" = "" ] ; then
    echo "No Process ID given"
    exit 1
else
    STARTPID=$1
fi

# Get the normalized output from ps
TMPFILE=/tmp/$0.$$
PS="ps -ea -o pid -o ppid -o user -o time -o args"
$PS | tail +2 | sed 's/^ *//g' > $TMPFILE

# Start registering the total CPU time
lookup_descendents $STARTPID
echo $CPUTIME

# Cleanup
rm -f $TMPFILE

----

If I use the script on a process with 2 subprocesses I get the
following results:

./myscript.sh 12345
before : 0
after : 0
before : 0
after : 0
before : 0
after : 326
0

As said all the intermittent results are as expected, but I have no
idea why $CPUTIME is reset back to 0 at the end.

Can anybody see what I am doing wrong?

Regards,

Niels

 
 
 

Scope confusion

Post by j.. » Sat, 17 Sep 2005 01:17:51



> I created a script that registers the current CPU time for a given
> process ID and it's children. It uses a global variable to add all
> the CPU times to. But the end result is always zero, while
> in-between results show the expected values.

> The script:

#!/bin/sh

[...]

Quote:> lookup_descendents()
> {
>     # First register the CPU time of the current process
>     cat $TMPFILE | grep "^$1 " | \
>     while read LDPID LDPPID LDUSER LDTIME LDARGS ; do
>         echo "before : $CPUTIME"
>         add_cputime $LDTIME
>         echo "after : $CPUTIME"
>     done

>     # Find the descendents of the current process and register their
> CPU times
>     cat $TMPFILE | grep " $1 " | \
>     while read LDPID LDPPID LDUSER LDTIME LDARGS ; do
>         if [ "$LDPID" != "$LDPPID" ] ; then
>             lookup_descendents $LDPID
>         fi
>     done
> }

[...]

Quote:> As said all the intermittent results are as expected, but I have no
> idea why $CPUTIME is reset back to 0 at the end.

> Can anybody see what I am doing wrong?

If this is a real bourne shell, take a look at

http://home.comcast.net/~j.p.h/cus-faq-2.html#33

Joe
--
I never veer to starboard 'cuz I never sail at all
   - The Pirates Who Don't do Anything

 
 
 

1. sockaddr var. and scope problem !!!

Hello,

When I declare the sockaddr_in variable to be used with sendto and
recvfrom functions outside the main function, the program works. When I
move the declaration inside the main function, the program doesn't work
as expected. I've attached the code - take a look and tell me what you
think.

Martin

2. P9000 server question (VIPER VLB)

3. NETBIOS-DD-SERVER and NETBIOS-SCOPE dhcpd.conf ????

4. No core dumps?

5. java thread -- process or system contention scope?

6. ppp with microcom - Need advise!

7. 2.6 question: What are "versioned/scoped" libraries?

8. Seyon?

9. Make question: (How are MACROS scoped?)

10. Network Using Scope ID

11. Scope of a variable in a shell script

12. Using Linux to query MS DHCP Scopes?

13. maxuproc scope