LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Output of a C program which contains Linux's TOP command (https://www.linuxquestions.org/questions/programming-9/output-of-a-c-program-which-contains-linuxs-top-command-701885/)

trups8 02-03-2009 06:29 AM

Output of a C program which contains Linux's TOP command
 
Hi,

I want to continuously check the cpu usage of a particular C process, and log its CPU usage to a file.

For this I have created another C program as follows. PID of my main C program is 12274. This another c programs (dire.c) runs the Linux's 'TOP' command for monitoring CPU usage of that process, and log that usages to /root/trupti file.

==================================================================
#include<stdio.h>
#include<stdlib.h>
#include<time.h>


main()
{

int pid=12274;
char string1[8192];
sprintf(string1,"top -p %d |grep %d >> /root/trupti",pid,pid);
system(string1);
}
====================================================================


Now the problem is, when i run this C program from command prompt(as ./dire, where dire is object file of this program), than it works properly and CPU usage line gets log to /root/trupti file

But when I run this process as background process by command './dire &' , the CPU usage doesn't get logged to /root/trupti

Even I tried to send the running process to backgound by using 'ctrl +z' and 'bg' command , but still the CPU usage of the TOP command doesn't get logged.


Please suggest me the solution for this problem, of if any other way to continuously log the CPU usage of Main C process to a file.
Waiting for reply frnds...

jiml8 02-03-2009 06:46 AM

Top is intended to be interactive. If you try to run top in the background, it won't. Try top & and see what happens.

I can't give you the details without a lot of work; probably someone else here can, but that appears to be your problem.

Hko 02-03-2009 07:00 AM

'top' gets the CPU-usage information from files in /proc. In your program you can do the same and will not have the problem you described.

You can read how to do this in in this forum thread.

By the way, why write a shell script in C?

wje_lq 02-03-2009 07:01 AM

Quote:

Top is intended to be interactive.
Not particularly. Reading the man page will show you what you need to know:
Code:

      -b : Batch mode operation
            Starts top in 'Batch mode', which could be useful for sending
            output from top to other programs or to a file.  In this
            mode, top will not accept input and runs until the iterations
            limit you've set with the '-n' command-line option or until
            killed.

Code:

      -n : Number of iterations limit as:  -n number
            Specifies the maximum number of iterations, or frames, top
            should produce before ending.


Hko 02-03-2009 07:15 AM

If you want do this in C, It probably easiest (and best) to call times(2). Then you do not even need to open-read-parse files from /proc.

wje_lq 02-03-2009 07:27 AM

Quote:

It probably easiest (and best) to call times(2).
That only works for the current process or its descendants. Apparently trups8 wishes to monitor an arbitrary process.

trups8 02-06-2009 05:19 AM

Hii friends,

Thanks a lot for ur replies... Problem is solved using -b -n option of top command..which is suggested by wje_lq...thanks..

paulsm4 02-07-2009 12:42 AM

Trups -

1. You really want to consider using "/proc". Unlike spawning off a full-fledged process (your program), that spawns yet another process ("top"), reading from /proc will incur much less overhead ... and isn't as likely to interfere with exactly the system performance values you're trying to read.

2. Assuming that you control the source code for the program you're trying to monitor (the process that's using high CPU), and assuming it's written in C, you also want to consider using:

- gdb: step into the code as it's misbehaving
- gprof: profile what parts of the code are getting executed

Just a couple of suggestions .. PSM

shyamkumar1986 02-07-2009 06:14 AM

A quick suggestion - why dont you just write a simple shell script to do the task.
(you can schedule it to run via cronjob too)


All times are GMT -5. The time now is 03:00 PM.