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.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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.