Answer» I need some help with a c program that I am writing to create my own unix SHELL (for a class assignment).
At one point in the program, I need to print out some process statistics, but only for the pid of my shell process. If I just use the system("ps -o pid,ppid,pcpu,pmem,etime,user,command"); command, I get the statistics for all processes. I've read every man page on ps that I can find. I know that -p and --pid are supposed to be flags that I can use to only print stats for that pid.
My PID variable is declared at the beginning of my main function as pid_t PID; And the line of code above my system() line is: PID = getpid(); In the system(), I have tried "ps -p PID", "ps --pid PID", I've tried with the variable name in quotes, with a $ in front, I've tried converting it to an integer first before the system() line, I've even tried changing the variable name from all lower case to all caps. I've tried everything I can think of, but no luck.
Does anyone know how do get the ps process to WORK with my PID variable? Any help would be very very very MUCH appreciated! This program is due Tuesday and I've been working on this particular problem for days. I'm ready to pull all my hair out!
Thank you very much.
Sarah homework? anyway, post your code if you need ppl here to help. Well, the only code relevent to my particular question is as follows:
pid_t PID; PID = getpid(); system("ps -o pid,ppid,pcpu,pmem,etime,user,command");
The 3rd line there prints out process statistics for all process running. I need it to only print for the process with id PID. So I need to find a way to use the ps command with the variable PID. I have tried -p and --pid with no luck, but I thought maybe there was some special syntax for it that I'm not familiar with.
The current output is: PID PPID %CPU %MEM ELAPSED USER COMMAND 8431 8430 0.0 0.4 04:06 xx -csh 8479 8431 0.0 0.0 00:01 xx ./xxshell 8480 8479 0.0 0.1 00:00 xx ps -o pid,ppid,pcpu,pmem,etime,user,c
I need to only print the line with my shell's PID (in this case, 8479).
Thanks for the help!
SarahQuote ... pid_t PID; PID = getpid(); system("ps -o pid,ppid,pcpu,pmem,etime,user,command"); .... I need to only print the line with my shell's PID (in this case, 8479). .....
any reasons why you need to do this in C? well. if you are really into C, i don't think you need to do a system() .... there are functions in C , like getppid() , getpgrp() etc (do a "man getpid" ).... sorry don't do Unix programming much, so no comments on how to get memory usage and such... another way is to learn how they do it by looking at the ps source code.
If you don't mind doing in shell, the $$ variable SHOWS the process id of the current process
eg test script Code: [Select]#!/bin/sh while [ 1 = 1 ] do echo $$ sleep 10 ps ..... |grep $$ ###print the shell pid done
Unfortunately, the assignment says we have to use C (or C++) to do this program. I tried using grep, but I didn't quite get the desired results.
system("ps -o pid,ppid,pcpu,pmem,etime,user,command | grep $$");
gives the output:
9657 9654 0.0 0.1 00:00 xx sh -c ps -o pid,ppid,pcpu,pmem,etime,user,command | grep $$ 9658 9657 0.0 0.1 00:00 xx ps -o pid,ppid,pcpu,pmem,etime,user,command 9659 9657 0.0 0.1 00:00 xx grep 9657
So I've lost the little header colums and I'm still getting stuff I don't want to show (child processes that have my process as the ppid).
I'll be emailing the teacher tonight to see if he can (or is able to without giving away the answer) help me with this.
Thank you again for your help with this. It is much appreciated.
Sarah
|