ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I'm not very sure this is the most correct way, but from a little browsing through "man proc" I'd say something like:
- First get the PID of the process you want to get the CPU percentage of.
- Then parse /proc/<PID>/stat and get the fields "utime" and "stime" from that file.
- Add those two values and call the result "pidcpu", i.e.: pidcpu = utime + stime
- Parse /proc/stat and add the first 3 fields from the first line ("cpu") together, call this result "totalcpu"
- finally: percentage = 100 * pidcpu / totalcpu
i might end up using ps if i cant figure out how to do this through /proc. i had a quick look at the man page for ps but i couldnt figure out how to show cpu usages, how can this be done?
i might end up using ps if i cant figure out how to do this through /proc. i had a quick look at the man page for ps but i couldnt figure out how to show cpu usages, how can this be done?
I'd try having a look at the source of 'ps' instead of the man page. 'ps' uses /proc for sure. You can get it here: http://procps.sourceforge.net/
What I need for my project (and his different project) is how to access that cpu/kernel filesystem and access this is C++,
If nobody knows, say so.
Like that ramdom number thing, I'll have to figure out a formula myself!
Well, can't get everything fresh baked on a plate without working for it!
Originally posted by mimithebrain
That does not help,
You didn't try.
Quote:
What I need for my project (and his different project) is how to access that cpu/kernel filesystem and access this is C++,
The answers in this thread were already referring to C(++).
Quote:
If nobody knows, say so.
If you don't know how to open a file in C(++)....say so, instead of re-asking the original question, which is quite a different from "how to read a file in C(++)?"
When I attempt to read file in proc with "kwrite", it's blank.
Some files in /proc may be empty, to other files you may have not persmission to read. But the files you need for cpu-usage will be readable by normal users. For example, /proc/self/stat should not be empty, and readable. If it is still empty, I can only think of only one other reason: your kernel does not support the /proc file-system, or it isn't mounted. This not very likely for the main linux distributions though.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.