473,403 Members | 2,323 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Outputing top to a file from a forked process

158 100+
Hello Bytes,

CPU Info: Debian Lenny x86

I am trying to use the output of top to return cpu usage.
- Im am actually trying to find a simple solution to gather cpu and mem usage, I have found top is the simplest to me.

My code works great when I run it from main, however as soon as my process forks to the background when the below code runs it does not work anymore.

Any clues why it acts differently from forked vs non forked?


Thanks

Expand|Select|Wrap|Line Numbers
  1.     system("top -n 2 >& /var/tmp/topCpu");
  2.     FILE *pfp;
  3.     char line[100];
  4.     string line2;
  5.     string per = "";
  6.     if ((pfp = fopen("/var/tmp/topCpu", "r")) == NULL) {
  7.         fprintf(stderr, "POPEN Error\n");
  8.         exit(1);
  9.     }
  10.     for (fgets(line, sizeof (line), pfp); fgets(line, sizeof (line), pfp);) {
  11.         line2 = line;
  12.  
  13.         size_t found;
  14.         found = line2.find("Cpu(s):");
  15.         if(found < line2.length()){
  16.             per = "";
  17.             for(int i=0;i<strlen(line);i++){
  18.                 if(i >=  27 && i <= 32 && line[i] != '%'){
  19.                     per += line[i];
  20.                 }
  21.             }
  22.         }
  23.     }
  24.     fclose(pfp);
  25.  
  26.     return atof(per.c_str());
  27.  
Aug 10 '10 #1
2 1184
weaknessforcats
9,208 Expert Mod 8TB
I suspect you want to run your CPU usage on a separate thread rather than a separate process. That is, pthread instead of fork.

I think you are getting the CPU usage of the process but not the process with your main().
Aug 10 '10 #2
kardon33
158 100+
No, i am not trying to get the cpu usage of any process but the entire system.

I am getting gibberish when ran from process not still active in stdin. But I cant figure out why.
Aug 10 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: John Lin | last post by:
Howdy, I want to know how to tell if a forked process is done. Actually, my real question is that I want to run a shell script inside of a python script, and after the shell script has...
2
by: RL | last post by:
Hello Perl gurus, 1. I have a web page where I can push a button (dospawn.html). 2. This button calls a CGI script (spawnboss.cgi) 3. spawnboss.cgi calls a forking perl script (forkme.pl) 4....
2
by: Ulrich Sprick | last post by:
Hi all, I have a problem that should be quite common, but I'm not (yet) familiar with process synchronization in .NET: There are several applications that write to a single file on disk. The apps...
2
by: clusardi2k | last post by:
Hello, I fork a child process. How can I tell a debugger which process to follow? Thank you, Christopher Lusardi
1
by: Alexander N. Spitzer | last post by:
I am trying to write a program that will fork a process, and execute the given task... the catch is that if it runs too long, I want to clean it up. this seemed pretty straight forward with a...
2
by: James Colannino | last post by:
Hey everyone. I'm writing a small application in Python that uses os.fork() to create a separate process in which another application is run in the background. The problem is that I need to know...
2
by: ary | last post by:
hi all I have a problem with working file when i upload a file in IIS I can'd Delete it. if I try to delete it in debug mode , it successfully worked. my error message "the process...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.