LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-03-2009, 06:29 AM   #1
trups8
LQ Newbie
 
Registered: Feb 2009
Posts: 3

Rep: Reputation: 0
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...
 
Old 02-03-2009, 06:46 AM   #2
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
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.
 
Old 02-03-2009, 07:00 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
'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?
 
Old 02-03-2009, 07:01 AM   #4
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 178Reputation: 178
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.
 
Old 02-03-2009, 07:15 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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.
 
Old 02-03-2009, 07:27 AM   #6
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 178Reputation: 178
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.
 
Old 02-06-2009, 05:19 AM   #7
trups8
LQ Newbie
 
Registered: Feb 2009
Posts: 3

Original Poster
Rep: Reputation: 0
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..
 
Old 02-07-2009, 12:42 AM   #8
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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
 
Old 02-07-2009, 06:14 AM   #9
shyamkumar1986
LQ Newbie
 
Registered: Feb 2009
Posts: 19

Rep: Reputation: 1
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)

Last edited by shyamkumar1986; 02-09-2009 at 08:39 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Make a program can collect linux command output regularly and send to a tftp server fragon Programming 2 04-07-2008 04:49 PM
Help me understand my top output neocontrol Linux - Server 5 02-21-2008 05:46 PM
problem with output of ' top -n 1 ' command NoopurTiwari Linux - Newbie 1 05-29-2007 02:45 AM
How would I transfer the output of a command or a program to here, eliminating typin royeo Linux - Newbie 9 07-07-2006 09:09 PM
Output of 'top' as backgrond Burgin Linux - General 2 06-09-2005 05:18 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration