ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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
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!
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.