Code:
#include <sys/time.h>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
long unsigned int yourfunction(unsigned int n);
static long p_realt_secs = 0, c1_realt_secs = 0, c2_realt_secs = 0;
static long p_virtt_secs = 0, c1_virtt_secs = 0, c2_virtt_secs = 0;
static long p_proft_secs = 0, c1_proft_secs = 0, c2_proft_secs = 0;
static struct itimerval p_realt, c1_realt, c2_realt;
static struct itimerval p_virtt, c1_virtt, c2_virtt;
static sturct itimerval p_proft, c1_proft, c2_proft;
main(int argc, char **argv){
long unsigned res = 0;
int pid1, pid2;
unsigned int funarg;
int status;
/* get command line argument, funarg */
...
/* initialize parent, child1 and child2 timer values */
...
/* enable your signal handlers for the parent */
signal(SIGALRM,...);
signal(SIGVTALRM,...);
signal(SIGPROF,...);
/* set the parent's itimers */
...
pid1 = fork();
if(pid1 == 0){
/* enable child 1 signal handlers (disable parent handlers) */
/* set the child 1 timers */
...
/* start yourfunction() at child 1*/
res = yourfunction(funarg);
/*Read the child 1 itimer values and report them
getitimer(ITIMER_PROF,...);
getitimer(ITIMER_REAL,...);
getitimer(ITIMER_VIRTUAL,...);
printf("\n");
printf("Child 1 res = %ld, real time = %ld sec, %ld msec\n", res, c1_realt_secs, ...);
printf("Child 1 res = %ld, cpu time = %ld sec, %ld msec\n", res, c1_proft_secs,...);
printf("Child 1 res = %ld, user time = %ld sec, %ld msec\n", res, c1_virtt_secs,...);
printf("Child 1 res = %ld, kernel time = %ld sec, %ld msec\n", res, c1_proft_secs-c1_virtt_secs,...);
fflush(stdout);
exit(0);
}else{
pid2 = forl();
if(pid2 == 0){
/* enable the child 2 signal handlers (disable parent handlers) */
...
/* set the child 2 itimers*/
...
}else{ // this is the parent
/* start your function at the parent */
res = yourfunction(funarg);
/* wait for the children to terminate */
waitpid(0, &status, 0);
waitpid(0, &status, 0);
/* read the parent itimer values and report them */
...
}
printf("this line should never be printed\n");
} /* end of main */
long unsigned int yourfunction(unsigned int n){
...
}
and i have to do: