Int getrusageint who struct rusage usage reports on

This preview shows page 19 - 20 out of 20 pages.

int getrusage(int who, struct rusage *usage); Reports on resource usage by yourself and your children Also set in pid_t wait4(pid_t pid, int *stat_loc, int options, struct rusage *rusage); struct rusage { struct timeval ru_utime; /* user time used */ struct timeval ru_stime; /* system time used */ long ru_maxrss; /* maximum resident set size */ long ru_ixrss; /* integral shared memory size */ long ru_idrss; /* integral unshared data size */ long ru_isrss; /* integral unshared stack size */ long ru_minflt; /* page reclaims */ long ru_majflt; /* page faults */ long ru_nswap; /* swaps */ long ru_inblock; /* block input operations */ long ru_oublock; /* block output operations */ long ru_msgsnd; /* messages sent */ long ru_msgrcv; /* messages received */ long ru_nsignals; /* signals received */ long ru_nvcsw; /* voluntary context switches */ long ru_nivcsw; /* involuntary context switches */ };
Image of page 19

Subscribe to view the full document.

#include <iostream> #include <string> #include <sys/wait.h> #include <sys/types.h> #include <unistd.h> #include <signal.h> #include <sys/types.h> #include <sys/signal.h> #include <time.h> #include <sys/time.h> using namespace std; void handler (int status); /* definition of signal handler */ // Global variable so signal handler can see it pid_t pID; // This example shows how to use execve and signals main() { struct sigaction sigold, signew; struct itimerval rttimer; struct itimerval old_rttimer; signew.sa_handler=handler; sigemptyset(&signew.sa_mask); sigaddset(&signew.sa_mask,SIGCHLD); sigaddset(&signew.sa_mask,SIGALRM); signew.sa_flags = SA_RESTART; sigaction(SIGCHLD,&signew,&sigold); sigaction(SIGALRM,&signew,&sigold); // Now set up the interval timer rttimer.it_value.tv_sec = 10; rttimer.it_value.tv_usec = 0; rttimer.it_interval.tv_sec = 10; rttimer.it_interval.tv_usec = 0; setitimer (ITIMER_REAL, &rttimer, &old_rttimer); pID = fork(); if (pID == 0) // child { // Code only executed by child process // exec the program char *argvToChild[5]; char *envToChild[5]; argvToChild[0] = "foo"; argvToChild[1] = "bar"; argvToChild[2] = NULL; envToChild[0] = "SERVER_NAME=gunga"; envToChild[1] = "GUMBO=GIMBO"; envToChild[2] = NULL; printf("Child about to exec\n"); execve("loop.pl",argvToChild,envToChild); perror("Child should not return"); } else // parent { // Code only executed by parent process for(;;); } } void handler (int status) { int stat; int err; struct rusage child_ru; printf("received signal %d\n",status); if(status == SIGCHLD) { err = wait4(pID,&stat,0,&child_ru); printf("wait returned %d status %X exit status %d\n",err,stat, WEXITSTATUS(stat)); printf("user time %d, system time %d\n",child_ru.ru_utime.tv_sec,child_ru.ru_stime.tv_sec); } if(status == SIGALRM) { printf("This guy %d is toast\n",pID); kill(pID,SIGKILL); } }
Image of page 20
You've reached the end of this preview.
  • Winter '08
  • Clement,M
  • Exit, Parent process, int status, int stat

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern