Linux Processes

Hi guys! i want to access the details of processes in linux,like their CPU usage.how much RAM they consume and stuffs like that.can u tell me how can i get those details???

Comments

  • you can see it with utilities such as [icode]ps[/icode] and [icode]top[/icode]. You can also look at individual process statistics and properties in /proc/<pid>/
  • you can see it with utilities such as [icode]ps[/icode] and [icode]top[/icode]. You can also look at individual process statistics and properties in /proc/<pid>/

    no no! i guess u got me wrong!I want to programatically calculate the CPU % of each process.so ps and top are ruled out as i dont think using them is a good idea.

    coming to /proc/<pid>,i know there is a file called stat which contains all the information i needed to process the CPU % but the thing is i really dont know how to calculate this from that file.what is the exact calculation to be done to get that result?and sorry for not asking in detail in the starting!
  • If ps produces the info you want then just redirect its output to a file and use that. Or you could just pipe the data directly into your program.
  • If ps produces the info you want then just redirect its output to a file and use that. Or you could just pipe the data directly into your program.

    well that does work but the thing is! I don’t think using a shell command in a C program is an efficient way! If PS and TOP could calculate this then we should also be able to calculate this.thats why Im asking!
  • It's true, it's not efficient. But with today's fast computers it doesn't make much difference. 20 years ago when computers were very very slow I'd say go for the most efficient way. BTW: I don't know how to make those calculations either.
  • It's true, it's not efficient. But with today's fast computers it doesn't make much difference. 20 years ago when computers were very very slow I'd say go for the most efficient way. BTW: I don't know how to make those calculations either.


    hmmm:-/ ya thats a valid point! hmm also that reduces code maybe a hundred lines.All right! I agree.just one more thing.you sure all linux distros and their variants support ps and top commands?cuz i dont want to end up changing the code for each type of distro!
  • I won't guarantee that 100% of all distributions have those commands, but I have not come across one that doesn't. I suppose some embedded systems may not have them.
  • I won't guarantee that 100% of all distributions have those commands, but I have not come across one that doesn't. I suppose some embedded systems may not have them.

    thank u so much!! :)
  • Sorry, but doing exec() to run 'ps' on linux systems is the most horrible thing I've heard in a while :)
    Your own suggestion to use /proc/pid/stat is not so bad and should be fairly easy to implement. Just a matter of reading a single line and filtering out the information you want according to the following layout -> http://www.lindevdoc.org/wiki//proc/pid/stat
  • Patsie wrote:
    Sorry, but doing exec() to run 'ps' on linux systems is the most horrible thing I've heard in a while :)
    Your own suggestion to use /proc/pid/stat is not so bad and should be fairly easy to implement. Just a matter of reading a single line and filtering out the information you want according to the following layout -> http://www.lindevdoc.org/wiki//proc/pid/stat

    @patsie! yeah! after some searches i found that using the proc filesystem is the best way.(still haven't figured out the formula though).but i was working on a subtask of this application that im writing. It is to find the pid of a process say firefox.exe by its name.I know pidof <processname> does this! I wrote a small snippet that gets the pid of a particular process by reading the stat file.its so easy to use /proc.also iterating through /proc is much faster than iterating through root or any other directory!.So using /proc is the best decesion. The only thing i need is the formula to calcuate CPU usage of a process from cputime in user mode and kernel mode of a process(in jiffies).there are different formulas over the internet each giving a diffent answer.but I dont know which is correct.Also I really need a valid justification of any of those formulas which they are lacking!please help if u know that formula!! thanks!! guys for ur quick response!
  • FYI!! im writing this as a Daemon so the less it interacts with shell the better it is.so i think i should rule out using shell commands!!
Sign In or Register to comment.