system("ps -al"); Question

This is a discussion on system("ps -al"); Question within the C Programming forums, part of the General Programming Boards category; Here's the deal, I got some knowledge on C, but now some processes stuff have come in which is pretty ...

  1. #1
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235

    system("ps -al"); Question

    Here's the deal, I got some knowledge on C, but now some processes stuff have come in which is pretty new. I've figured much of the problem, but im wondering is there anyway to get the processes info display as below, especially the one in Blue. I did a system ("ps -al"); and got the one way below, but i dont seem to find anything on the man pages to do that sort that's in blue, is it doable?:

    Code:
    ./prog2a: Printing System Process chart using "ps -al"
    F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
    0 S 1000 3879 3099 0 78 0 - 331 schedu tty2 00:00:00 script
    1 S 1000 3880 3879 0 77 0 - 364 schedu tty2 00:00:00 script
    
    ##
    Next line is the parent process
    ##
    0 R 1000 3888 3881 0 79 0 - 325 - pts/0 00:00:00 prog2a
    ##
    Next line is the child process
    ##
    1 S 1000 3889 3888 0 79 0 - 325 clock_ pts/0 00:00:00 prog2a
    0 R 1000 3890 3888 0 82 0 - 626 - pts/0 00:00:00 ps 
    Got this with system("ps -al");
    Code:
    
    F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
    0 S  4888 11415 11315  0  80   0 -  1325 -      pts/0    00:00:00 ssh
    0 S  4888 11440 11422  0  80   0 -  1325 -      pts/1    00:00:00 ssh
    0 R  4888 11600 11463  0  80   0 -   390 -      pts/2    00:00:00 l
    1 R  4888 11601 11600  0  80   0 -   391 -      pts/2    00:00:00 l
    0 R  4888 11602 11601  0  80   0 -   443 -      pts/2    00:00:00 sh
    0 R  4888 11603 11602  0  80   0 -   607 -      pts/2    00:00:00 ps
    
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  2. #2
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    How did you get the one on top, if you don't mind me asking?

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So Linux has things like getpid and getppid.

  4. #4
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by esbo View Post
    How did you get the one on top, if you don't mind me asking?
    O right, sorry the top one was an example the teacher posted as an example to follow. How the program should look like when it runs. But i solved it I'll post the solution i got with the help of a friend, if those from my class would care to follow: Here it is:

    First have a variable of type char with a name, for instance mine was command, you guys should use something different ok:

    Code:
    char cmd[50];
    
    printf("#########################################################################\n"
                "Next line parent process\n"
                   "#########################################################################\n");
         sprintf(command,"%s %d","ps -lp ",getppid());  //parent chart
         system(command);
         printf("#########################################################################\n"
                "Next line is the child process\n"
                "#########################################################################\n");
         sprintf(command,"%s %d","ps -al | grep",getpid()); //child chart
         system(command);
    There that will print only the parent and the child process chart, so thats basically what i have.

    !

    Use this site for other reads, basically what i used: http://h71000.www7.hp.com/commercial.../5763p012.html
    Last edited by Matus; 01-23-2009 at 12:53 PM.
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  5. #5
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by tabstop View Post
    So Linux has things like getpid and getppid.
    Yup yes i know tabstop, but this was a bit different from what i wanted, though the ideas still lied between those two things yes!
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  6. #6
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Oh yea one more thing that might be confusing to my classmates, if u still having problems with execl, what u needed to do was, first of all execute partb.c of the program, gcc it, after u did that you will use execl this way

    Code:
    execl("programexecutablenamefrompartb","Arg1","Arg2",(char *)NULL);
    That should do the trick of running, program partb in the first program, if anyone from C board has other recommendations cool, I'd more than welcome more info.
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You know popen is better for this, since you can then get the output and parse it yourself:
    Code:
    char cmmd[]="ps", opt[]="-al", string[256], buffer[1024];
    sprintf(string, "%s %s", cmmd, opt);
    FILE *pin=popen(string, "r");
    while (get_a_line(pin, buffer)>0) {
             [do stuff line by line, eg. parse, format, print with the output of the command]
    }
    You need some function (get_a_line) to read from FILE *pin exactly as if it were a txt file open for reading (like FILE *fstin).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21