Sponsored Content
Full Discussion: To find CPU Usage
Top Forums Programming To find CPU Usage Post 96313 by asutoshch on Thursday 19th of January 2006 09:06:18 AM
Old 01-19-2006
To find CPU Usage

I am programatically trying to find CPU usage for a particular process. I am writing a C program for this. I am not sure if my approach is good at all. I first of all find the PID using getpid() method. Then I call top -f <filename> and then parse to reach the PID row. I then try to read the 10th field namely %CPU. But this is very tedious and likely to have many bugs in parsing process.
I had to read from file since top command does not terminate automatically. Can anyone, please suggest some better way to do?
Thanks a lot in advance.
 

10 More Discussions You Might Find Interesting

1. Programming

Monitor CPU usage and Memory Usage

how can i monitor usages of CPU, Memory, Hard disk etc. under SUN Solaries through a c program or java program i want to store that data into database so i can show it graphically thanks in advance (2 Replies)
Discussion started by: Gajanad Bihani
2 Replies

2. Shell Programming and Scripting

Getting CPU Usage

For analysis, I need to gather CPU usage from say 1:00 PM to 2:00 PM every 10 seconds on a given day. I heard this can be done with awk, but my UNIX is kind of rusty. (4 Replies)
Discussion started by: WG1
4 Replies

3. Programming

CPU usage and memory usage

Please tell me solaris functions/api for getting following information 1- Function that tells how much memory used by current process 2- Function that tells how much memory used by all running processes 3- Function that tells how much CPU is used by current process 4- Function that tells how... (1 Reply)
Discussion started by: mansoorulhaq
1 Replies

4. Solaris

Find any spike in CPU usage a hour ago

Folks, I need to find out from the system logs ( or elsewhere ) if the CPU utilization reached say 90% an hour ago from now. Is there a way out. (2 Replies)
Discussion started by: kavera
2 Replies

5. HP-UX

how can I find cpu usage memory usage swap usage and logical volume usage

how can I find cpu usage memory usage swap usage and I want to know CPU usage above X% and contiue Y times and memory usage above X % and contiue Y times my final destination is monitor process logical volume usage above X % and number of Logical voluage above can I not to... (3 Replies)
Discussion started by: alert0919
3 Replies

6. Solaris

current CPU usage, memory usage, disk I/O oid(snmp)

Hi, I want to monitor the current cpu usage, monitor usage , disk I/o and network utlization for solaris using SNMP. I want the oids for above tasks. can you please tell me that Thank you (2 Replies)
Discussion started by: S_venkatesh
2 Replies

7. Solaris

Multi CPU Solaris system shows 100% CPU usage.

Hello Friends, On one of my Solaris 10 box, CPU usage shows 100% using "sar", "vmstat". However, it has 4 CPUs and prstat and glance are not showing enough processes to justify high CPU utilization. ========================================================================= $ prstat -a ... (4 Replies)
Discussion started by: mahive
4 Replies

8. AIX

How to monitor the IBM AIX server for I/O usage,memory usage,CPU usage,network..?

How to monitor the IBM AIX server for I/O usage, memory usage, CPU usage, network usage, storage usage? (3 Replies)
Discussion started by: laknar
3 Replies

9. UNIX for Advanced & Expert Users

How to find out memory & cpu usage of a process

Hi, By using time command we can determine the execution time of a process or command. bash-2.04$ time ls -l total 0 -rw-r--r-- 1 admin tac 0 Oct 6 04:46 file1 -rw-r--r-- 1 admin tac 0 Oct 6 04:46 file2 real 0m0.002s user 0m0.000s sys 0m0.001s... (5 Replies)
Discussion started by: learn more
5 Replies

10. BSD

How to find the usage of Individual cpu core in FreeBsd server?

Hi, If anyone know Please answer me the following question : How to find the usage of individual core cpu usage in FreeBsd server? What command is used for that? Thanks, Maruthu (2 Replies)
Discussion started by: maruthu
2 Replies
CLOCK_GETCPUCLOCKID(3)					     Linux Programmer's Manual					    CLOCK_GETCPUCLOCKID(3)

NAME
clock_getcpuclockid - obtain ID of a process CPU-time clock SYNOPSIS
#include <time.h> int clock_getcpuclockid(pid_t pid, clockid_t *clock_id); Link with -lrt. Feature Test Macro Requirements for glibc (see feature_test_macros(7)): clock_getcpuclockid(): _XOPEN_SOURCE >= 600 || _POSIX_C_SOURCE >= 200112L DESCRIPTION
The clock_getcpuclockid() function obtains the ID of the CPU-time clock of the process whose ID is pid, and returns it in the location pointed to by clock_id. If pid is zero, then the clock ID of the CPU-time clock of the calling process is returned. RETURN VALUE
On success, clock_getcpuclockid() returns 0; on error, it returns one of the positive error numbers listed in ERRORS. ERRORS
ENOSYS The kernel does not support obtaining the per-process CPU-time clock of another process, and pid does not specify the calling process. EPERM The caller does not have permission to access the CPU-time clock of the process specified by pid. (Specified as an optional error in POSIX.1-2001; does not occur on Linux unless the kernel does not support obtaining the per-process CPU-time clock of another process.) ESRCH There is no process with the ID pid. VERSIONS
The clock_getcpuclockid() function is available in glibc since version 2.2. CONFORMING TO
POSIX.1-2001. NOTES
Calling clock_gettime(2) with the clock ID obtained by a call to clock_getcpuclockid() with a pid of 0, is the same as using the clock ID CLOCK_PROCESS_CPUTIME_ID. EXAMPLE
The example program below obtains the CPU-time clock ID of the process whose ID is given on the command line, and then uses clock_get- time(2) to obtain the time on that clock. An example run is the following: $ ./a.out 1 # Show CPU clock of init process CPU-time clock for PID 1 is 2.213466748 seconds Program source #define _XOPEN_SOURCE 600 #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <time.h> int main(int argc, char *argv[]) { clockid_t clockid; struct timespec ts; if (argc != 2) { fprintf(stderr, "%s <process-ID> ", argv[0]); exit(EXIT_FAILURE); } if (clock_getcpuclockid(atoi(argv[1]), &clockid) != 0) { perror("clock_getcpuclockid"); exit(EXIT_FAILURE); } if (clock_gettime(clockid, &ts) == -1) { perror("clock_gettime"); exit(EXIT_FAILURE); } printf("CPU-time clock for PID %s is %ld.%09ld seconds ", argv[1], (long) ts.tv_sec, (long) ts.tv_nsec); exit(EXIT_SUCCESS); } SEE ALSO
clock_getres(2), timer_create(2), pthread_getcpuclockid(3), time(7) COLOPHON
This page is part of release 3.27 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. Linux 2010-09-10 CLOCK_GETCPUCLOCKID(3)
All times are GMT -4. The time now is 01:17 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy