/* Some System Accounting Utilities, with Examples Here is a list of some shell commands and library functions available in Unix for controling and monitoring processes. Consult man pages for full details. This information applies to Solaris and with minor exceptions to Linux and other Unix variants. Shell Commands: whoami : prints login name of current user who : prints information for all logged-in users ps : prints process information. "ps -ecf" will give priority classification and other accounting information time : use before a command, as in "time ./a.out" upon completion, it will report the user time, system time, real time, and cpu utilization used by the process. nice : You can be "nice" to other processes by incrementing the nice value. E.g, "nice +5 ./a.out" will decrement the initial process priority by 5 units. priocntl: priority control on Solaris. Restricted use by non-administrators. kill : kill a process. To terminate a process, use the "ps" command to find the process id number. For example, if you want to kill process 5000, type "kill -9 5000". Killing the wrong process can be costly even when you're not root, so be careful. & : Use the & symbol after a command to run the process in the background. For example, "netscape &" will start netscape but still give you the command prompt. "./a.out &; a.out" will start two ./a.out processes concurrently. The ";" symbol used here is just a way to enter multiple commands. Library Functions The range of library functions is far more numerous than shell commands. Be sure to consult the man page (man - a ) to find details on how to use it, and what header files needs to be included. getlogin : returns location of string holding user name of current user getuid : returns a numerical user id getpid : returns process id number of current process getppid : returns parent process id of current process getpriority : returns a number indicating priority of process. setpriority : sets priority of process (restricted access) clock : returns cpu time for process - see example for usage. times : uses a struct to return information concerning user and system time consumed by a process. The following sample program illustrates the use of these commands. Study it and run it so you can use it in your own programs. */ /* include headers used by library functions: */ #include #include #include #include #include #include #include int main(int argc, char* argv[]) { int i,j, priority; struct tms timings; // used by times double cputime; // store value returned by clock pid_t procid, parentid; // process id and parent process id clock(); // start clock for process for(i=0;i<200000;i++) { printf("%s%d ",argv[1],i); } times(&timings); cputime = ((double)clock())/CLOCKS_PER_SEC; printf("CLK_TCK (clock tick) unit value == 1/%d second\n",CLK_TCK); printf("User time (in clock ticks) == %d\n",timings.tms_utime); printf("System Call time (in clock ticks) == %d\n",timings.tms_stime); printf("Total CPU time == %.2f seconds\n",cputime); procid = getpid(); parentid = getppid(); priority = getpriority(PRIO_PROCESS,procid); printf("\nProcess id == %d\n",procid); printf("Parent id == %d\n",parentid); printf("Process priority == %d\n",priority); printf("Current user is %s\n",getlogin()); printf("Calling user id == %d\n",getuid()); exit(0); } /* Compile and run this program in Solaris tcsh with: gcc cguide2.c time ./a.out A ./a./out A &; nice +5 ./a.out B What do the figures tell you? */