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
bash-2.04$
Similar way can we find out the memory and cpu usage? I know i can monitor it while it is running but that is not my requirement.

Thanks,
Anand

Some versions of unix have the rusage command. Check that out.

If you have a BSD version of ps or your ps accepts system V or BSD style parameters then running:

$ ps -aux

will show you the CPU and memory usage of running processes quoted as a percentage.

The top command also shows these plus resident and virtual memory usage.

Thanks for the responses..

My system does not have rusage.

I can use ps to get the data but my actual requirement is to find memory and cpu usage after the execution is over like 'time ls -l' command does.

try the command called top ..otherwise you could downlaod a tool called powertop and give the command powertop on the command line once it is installed

You will have to write C code this primitive example using getrusage(). Note -- not all systems fully support all of getrusage():

/* rusage.c
    usage:
    rusage  [executable_file_or_script]
*/
#include <sys/types.h>
#include <stdlib.h>
#include <sys/resrouces.h>
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
#include <errno.h>

char cmd[ARG_MAX]={0x0};

int dump(void)
{
		struct rusage s;
		struct rusage *p=&s;
		
  	getrusage(RUSAGE_CHILDREN, p);
		fprintf(stdout, "maximum resident set size              =%ld\n", p->ru_maxrss);
		fprintf(stdout, "integral resident set size             =%ld\n", p->ru_idrss);
		fprintf(stdout, "page faults not requiring physical I/O =%ld\n", p->ru_minflt);
		fprintf(stdout, "page faults requiring physical I/O     =%ld\n", p->ru_majflt);
		fprintf(stdout, "swaps                                  =%ld\n", p->ru_nswap);
		fprintf(stdout, "block input operations                 =%ld\n", p->ru_inblock);
		fprintf(stdout, "block output operations                =%ld\n", p->ru_oublock);
		fprintf(stdout, "messages sent                          =%ld\n", p->ru_msgsnd);
		fprintf(stdout, "messages received                      =%ld\n", p->ru_msgrcv);
		fprintf(stdout, "signals received                       =%ld\n", p->ru_nsignals);
		fprintf(stdout, "voluntary context switches             =%ld\n", p->ru_nvcsw);
		fprintf(stdout, "involuntary context switches           =%ld\n", p->ru_nivcsw);
  	return 1;
}

int main(int argc, char *argv[])
{
    char *p=cmd;
    int i=1;
    ptrdiff_t d=ARG_MAX;

    for(i=1; i < argc; i++)
    {
        size_t len=strlen(argv);
        if( d - len > ARG_MAX)
        {
           sprintf(p, "%s ", argv);
           d-=len;
           p+=(len +1);
        }
        else
        {
        	errno=E2BIG;
        	perror("Invalid parameter list");
        	exit(1);
        }
    }
    if(*cmd)
    {
	  	system(cmd);
	  	dump();
	  }
	  else
	  {
	  	errno=EINVAL;
	  	perror("cannot execute command");
	  	exit(1);
	  }
    return 0;
}