CPU Usage of a process


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting CPU Usage of a process
# 1  
Old 07-15-2011
CPU Usage of a process

Hi guys,

I am currently writing a JAVA script to monitor certain unix processes through JConsole.

Upon having lots of trouble with runtime.exec, i decided to bypass the top/ps command call and just get the information straight from /proc/*pid*/whatever.

Now i can pull back any information from any of the files I want, and the current way i determine the CPU usage of a process is as follows:

Add the UTime and STime of that process from /proc/pid/stat then divide my pidCpu by UTime + STime + NTime from /proc/stat, then multiply that by 100, should give me the % cpu usage a process is using, right?

Theory being if I get the jiffies assigned to my process, I can divide that by the total jiffies the cpu assigns.

However, my results seem to vary from the ones gathered from top and ps. What am I doing wrong?

Any input is appreciated.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Tricky situation with process cpu usage - AIX

OS: AIX so we frequently receive a lot of cpu related alerts. all types of checks have been created to keep an eye on the cpu but a lot of these checks make too much noise as the CPU is always being seen as high. the system and application owners say there's no issue with the cpu. so now,... (6 Replies)
Discussion started by: SkySmart
6 Replies

2. UNIX for Dummies Questions & Answers

Average CPU and RAM usage for a process

Hi, I will be creating a process myself and I want to know the average CPU and RAM used by the process over the lifetime of the process. I see that there are various tools available(pidstat) for doing , I was wondering if it possible to do it in a single command while creation. Thanks in... (3 Replies)
Discussion started by: koustubh
3 Replies

3. AIX

Problem with nmon, actual CPU usage per process

Hi all, I am currently having trouble to get nmon to print me the actual CPU usage for an interval for a process. According to the manual, something like # time nmon -t -C cron -s 5 -c 2 -F outfile real 0m0.98s user 0m0.03s sys 0m0.04s should print out at least the process... (15 Replies)
Discussion started by: zaxxon
15 Replies

4. AIX

How to trace cpu/memory usage for a process

I don't know when the process will start and end, I need write a script to trace it's cpu/memory usage when it is runing. How to write this script? (2 Replies)
Discussion started by: rainbow_bean
2 Replies

5. 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

6. Shell Programming and Scripting

CPU Usage for a particular process

Hi, I have a shell script. But, upon execution of the same, the cpu usage is sometimes getting 100 % (checked executing top command). At that point of time, my process hangs, doesn't run anymore. I need to kill it manually. My concern is, is there any default method, by which I can check... (1 Reply)
Discussion started by: jitendriya.dash
1 Replies

7. UNIX for Dummies Questions & Answers

process cpu usage

Trying to come up with a command that will show all processes sorted from highest cpu usage to lowest. Any ideas? (9 Replies)
Discussion started by: cwsmichigan
9 Replies

8. UNIX for Advanced & Expert Users

Finding CPU usage by a Unix Process

Hi, I am designing a load balancer for an application. I am trying to find out the CPU usage by a specifc Unix process (PID is known). I guess I can use ps command to find that. can somebody help me in finding what exact command I should use to find? It is on AIX 5.3. Regards Asutosh (2 Replies)
Discussion started by: asutoshch
2 Replies

9. AIX

CPU usage of a process

I'm trying to monitor the CPU usage of a process and output that value to a file or variable. I know topas or nmon can tell me this in interactive mode but what I need is topas-looking output that allows me to write to a file after a discrete interval. Unlike nmon data collection to a file on top... (5 Replies)
Discussion started by: robot23
5 Replies

10. Solaris

Process CPU usage in Solaris 10

Hi All, Please let me know the command (expect top) to view the cpu usage of every process in Solaris 10. Thanks in Advance, Arun (1 Reply)
Discussion started by: arun.viswanath
1 Replies
Login or Register to Ask a Question
SETPGID(P)						     POSIX Programmer's Manual							SETPGID(P)

NAME
setpgid - set process group ID for job control SYNOPSIS
#include <unistd.h> int setpgid(pid_t pid, pid_t pgid); DESCRIPTION
The setpgid() function shall either join an existing process group or create a new process group within the session of the calling process. The process group ID of a session leader shall not change. Upon successful completion, the process group ID of the process with a process ID that matches pid shall be set to pgid. As a special case, if pid is 0, the process ID of the calling process shall be used. Also, if pgid is 0, the process ID of the indicated process shall be used. RETURN VALUE
Upon successful completion, setpgid() shall return 0; otherwise, -1 shall be returned and errno shall be set to indicate the error. ERRORS
The setpgid() function shall fail if: EACCES The value of the pid argument matches the process ID of a child process of the calling process and the child process has success- fully executed one of the exec functions. EINVAL The value of the pgid argument is less than 0, or is not a value supported by the implementation. EPERM The process indicated by the pid argument is a session leader. EPERM The value of the pid argument matches the process ID of a child process of the calling process and the child process is not in the same session as the calling process. EPERM The value of the pgid argument is valid but does not match the process ID of the process indicated by the pid argument and there is no process with a process group ID that matches the value of the pgid argument in the same session as the calling process. ESRCH The value of the pid argument does not match the process ID of the calling process or of a child process of the calling process. The following sections are informative. EXAMPLES
None. APPLICATION USAGE
None. RATIONALE
The setpgid() function shall group processes together for the purpose of signaling, placement in foreground or background, and other job control actions. The setpgid() function is similar to the setpgrp() function of 4.2 BSD, except that 4.2 BSD allowed the specified new process group to assume any value. This presents certain security problems and is more flexible than necessary to support job control. To provide tighter security, setpgid() only allows the calling process to join a process group already in use inside its session or create a new process group whose process group ID was equal to its process ID. When a job control shell spawns a new job, the processes in the job must be placed into a new process group via setpgid(). There are two timing constraints involved in this action: 1. The new process must be placed in the new process group before the appropriate program is launched via one of the exec functions. 2. The new process must be placed in the new process group before the shell can correctly send signals to the new process group. To address these constraints, the following actions are performed. The new processes call setpgid() to alter their own process groups after fork() but before exec. This satisfies the first constraint. Under 4.3 BSD, the second constraint is satisfied by the synchronization property of vfork(); that is, the shell is suspended until the child has completed the exec, thus ensuring that the child has completed the setpgid(). A new version of fork() with this same synchronization property was considered, but it was decided instead to merely allow the parent shell process to adjust the process group of its child processes via setpgid(). Both timing constraints are now satisfied by having both the parent shell and the child attempt to adjust the process group of the child process; it does not matter which succeeds first. Since it would be confusing to an application to have its process group change after it began executing (that is, after exec), and because the child process would already have adjusted its process group before this, the [EACCES] error was added to disallow this. One non-obvious use of setpgid() is to allow a job control shell to return itself to its original process group (the one in effect when the job control shell was executed). A job control shell does this before returning control back to its parent when it is terminating or sus- pending itself as a way of restoring its job control "state" back to what its parent would expect. (Note that the original process group of the job control shell typically matches the process group of its parent, but this is not necessarily always the case.) FUTURE DIRECTIONS
None. SEE ALSO
exec() , getpgrp() , setsid() , tcsetpgrp() , the Base Definitions volume of IEEE Std 1003.1-2001, <sys/types.h>, <unistd.h> COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2003 Edition, Standard for Information Technol- ogy -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html . IEEE
/The Open Group 2003 SETPGID(P)