Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Emulating UNIX 'top' on AIX

by Transient (Hermit)
on May 02, 2005 at 15:08 UTC ( [id://453274]=perlquestion: print w/replies, xml ) Need Help??

Transient has asked for the wisdom of the Perl Monks concerning the following question:

AIX doesn't have a 'top' command. Well, I'd like to create an emulation for it using perl and "ps guw" piped to sort. I have the following so far:
... my $pid = fork(); # spawn child if ( $pid ) { # parent while ( wait != -1 ) { while (<>) { print "Got $_ on STDIN\n"; } } } else { # child while ( 1 ) { print $header; print `ps guw | sort -n -r -k $sort_option | head -$num_lines`; print "\nSort: $sort_option : Lines: $num_lines : Sleep: $sleep_ti +me\n"; sleep $sleep_time; print `clear`; } }
Pretty rudimentary, but what I would like to do is have the parent process listen for STDIN, like top does, and send a SIGHUP to the child to reset it's sort options, lines or whatnot. The while (<>) { doesn't seem to be working like I'd hoped. Any ideas on this one?

Replies are listed 'Best First'.
Re: Emulating UNIX 'top' on AIX
by jfroebe (Parson) on May 02, 2005 at 15:52 UTC

    Hi,

    Why not install the GNU version of top? http://directory.fsf.org/procps.html

    Jason L. Froebe

    Team Sybase member

    No one has seen what you have seen, and until that happens, we're all going to think that you're nuts. - Jack O'Neil, Stargate SG-1

      This isn't an option due to access restrictions. As I said previously, I also would like to do this simply for proof of concept :)
Re: Emulating UNIX 'top' on AIX
by ajt (Prior) on May 02, 2005 at 16:23 UTC

    If you get anywhere you should submit it to PPT: Unix Reconstruction Project.

    I had a quick peek, and they don't have top done, so anything you get to work will be accepted I'm sure.

    Good Luck.


    --
    ajt
Re: Emulating UNIX 'top' on AIX
by tcf03 (Deacon) on May 02, 2005 at 15:15 UTC
    What about topas?
    Ted
    --
    "Men have become the tools of their tools."
      --Henry David Thoreau
      Doesn't seem to work on my system, for some reason. :-S

      Besides - it's a fun little utility and I've got nothing better to do :)
Re: Emulating UNIX 'top' on AIX
by ikegami (Patriarch) on May 02, 2005 at 15:40 UTC

    Is the problem that <> blocks? Try Term::ReadKey

    By the way, <> does not necessarily read from STDIN. <STDIN> does.

      Well, I got around the blocking issue. I used waitpid $pid, WNOHANG and I can now read in from STDIN.

      The next issue is to read character by character instead of line by line. Is there any pure Perl way to do this?
        Ok, found some stuff about getc from the docs... using that seems to work!

        I'll post the code at the end for critique and future use.

        No. <FILE>, getc, read and sysread all block until a newline is read (even if they only return one character at a time). That's why I mentioned Term::ReadKey.

        Well, maybe you can do something with PerlIO. I don't know anything about that.

Re: Emulating UNIX 'top' on AIX
by Transient (Hermit) on May 02, 2005 at 16:57 UTC
    Ok. So now I am able to fork, process the system calls and read characters using the parent.

    My next problem is how to communicate an option change to the child. I was originally considering SIGHUP, but I had forgotten that it is primarily used where information is stored in a file.

    I could fork for each sleep cycle, but that seems wasteful.

    Or I could explicitly kill the child and respawn a new one with new options. Which, for some reason, seems like cheating.

    Another option would be to use only a single process and determine a way to make any input to be a signal interrupt which would need to be processed (and I'm not sure this would work).

    Any other ideas out there?
      You could
      • setup a communication pipe between the two processes and use a non-blocking select to determine if information is there to be read...
      • or use SIGHUP, regardless of what anyone thinks it's reserved for...
      • do the kill/respawn thing, but I think that's going to be widely regarded as an ugly option
      • I think a fork per sleep cycle is even worse than the above option.
      • Do the single process thing, with a non-blocking select to determine if there is input to read
      Personally, I believe the first or last options are the better options...

      -Scott

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://453274]
Approved by tcf03
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (7)
As of 2024-04-25 11:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found