Sponsored Content
Full Discussion: getrusage()
Operating Systems HP-UX getrusage() Post 302122770 by jim mcnamara on Thursday 21st of June 2007 09:53:39 AM
Old 06-21-2007
Code:
#include <stdlib.h>
#include <sys/resource.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>     


void process(struct rusage *p, char *when)
{
	printf("%s\n", when);
	printf(" /* user time used */                   %8d  %8d\n",  p->ru_utime.tv_sec,p->ru_utime.tv_usec   );
	printf(" /* system time used */                 %8d  %8d\n",  p->ru_stime.tv_sec,p->ru_stime.tv_usec   );
	printf(" /* integral shared memory size */      %8d\n",  p->ru_ixrss           );
	printf(" /* integral unshared data  */          %8d\n",  p->ru_idrss           );
	printf(" /* integral unshared stack  */         %8d\n",  p->ru_isrss           );
	printf(" /* page reclaims */                    %8d\n",  p->ru_minflt          );
	printf(" /* page faults */                      %8d\n",  p->ru_majflt          );
	printf(" /* swaps */                            %8d\n",  p->ru_nswap           );
	printf(" /* block input operations */           %8d\n",  p->ru_inblock         );
	printf(" /* block output operations */          %8d\n",  p->ru_oublock         );
	printf(" /* # of characters read/written */     %8d\n",  p->ru_ioch            );
	printf(" /* messages sent */                    %8d\n",  p->ru_msgsnd          );
	printf(" /* messages received */                %8d\n",  p->ru_msgrcv          );
	printf(" /* signals received */                 %8d\n",  p->ru_nsignals        );
	printf(" /* voluntary context switches */       %8d\n",  p->ru_nvcsw           );
	printf(" /* involuntary  */                     %8d\n",  p->ru_nivcsw          );

}


int main()
{
	  int ret;
	  char *buf;
	  int i=0;
	  int who= RUSAGE_SELF;
	  struct rusage usage;
	  struct rusage *p=&usage;

	  ret=getrusage(who,p);
	  process(p, "-------------before");
	  /* do stuff here */
      ret=getrusage(who,p);
	  process(p, "\n\n-------------after we run foo1");    

	return 0;
}

Note getrusage() works only for a process and its children.
 

We Also Found This Discussion For You

1. Programming

fork-getrusage

Hello everybody! I wrote the following code: ... int main() { pid_t pid; for (int i=0;i<100;i++) { pid=fork(); if(pid==0) {execl("md5sum","myprog",NULL);exit(1)} else if(pid>0) { waitpid(pid,&status,0);getrusage(RUSAGE_CHILDREN,&usage);} The... (3 Replies)
Discussion started by: nicos
3 Replies
times(3UCB)					     SunOS/BSD Compatibility Library Functions					       times(3UCB)

NAME
times - get process times SYNOPSIS
/usr/ucb/cc [ flag ... ] file ... #include <sys/param.h> #include <sys/types.h> #include <sys/times.h> int times(tmsp) register struct tms *tmsp; DESCRIPTION
The times() function returns time-accounting information for the current process and for the terminated child processes of the current process. All times are reported in clock ticks. The number of clock ticks per second is defined by the variable CLK_TCK, found in the header <limits.h>. A structure with the following members is returned by times(): time_t tms_utime; /* user time */ time_t tms_stime; /* system time */ time_t tms_cutime; /* user time, children */ time_t tms_cstime; /* system time, children */ The children's times are the sum of the children's process times and their children's times. RETURN VALUES
Upon successful completion, times() returns 0. Otherwise, it returns -1. SEE ALSO
cc(1B), time(1), time(2), getrusage(3C), wait(3C) NOTES
Use of these interfaces should be restricted to only applications written on BSD platforms. Use of these interfaces with any of the system libraries or in multi-threaded applications is unsupported. The times() function has been superseded by getrusage(3C). SunOS 5.11 30 Oct 2007 times(3UCB)
All times are GMT -4. The time now is 08:31 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy