/* Using door mechanism */

#include <door.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/times.h>
#include <limits.h>
#include "Lib.c"

typedef struct Record{

       int dum[1000];
}RECORD;

void 
increment(void *cookie, char *dataptr, size_t datasize,
		  door_desc_t *descptr, size_t ndesc)
{
	int i;
	RECORD *arg;

	
	arg = (RECORD *)dataptr;

	for (i = 0; i < 1000; i++)
	    arg->dum[i] = 0;

	door_return((char *)arg, sizeof(RECORD), NULL, 0);
}

int
main(int argc, char **argv){

	int n;
	int i;
	int fd;
	RECORD ival, oval;
	door_arg_t  arg;
	int pid;
        myTime mytime;
	struct tms buffer;
	long realtime;

	realtime = times(&buffer);
	printf("\n   o Real Time is %ld", realtime);
        printf("\n   o System Level Time is %ld", buffer.tms_stime);
        printf("\n   o User Level Time is %ld", buffer.tms_utime);
        printf("\n   o Child System Level Time is %ld", buffer.tms_cstime);
        printf("\n   o Child User Level Time is %ld", buffer.tms_cutime);


	
	if (argc != 2){
	      fprintf(stdout, "\nusage: d <integer>");
	      exit(0);
	}
	
	n = atoi(argv[1]);

	/* create door descriptor */ 
	fd = door_create(increment, NULL, 0);

        /* Server ends before here */

	if (pid = fork() == 0) {	/* Child */
                BeginTiming(0);
		arg.data_ptr = (char *) &ival; /* data arguments */
		arg.data_size = sizeof(RECORD);   /* size of data arguments */
		arg.desc_ptr = NULL;
		arg.desc_num = 0;
		arg.rbuf = (char *) &oval;     /* data results */
		arg.rsize = sizeof(RECORD);       /* size of data results */
		for (i = 0; i < n; i++){
			door_call(fd, &arg);
			/*fprintf(stdout, "\nChild calling...\t%d", ival = *((int *)arg.rbuf));*/
		}
                mytime = EndTiming(0);
         printf("\n Running time accounted as follows: ");
			    printf("\n   o System Level running time of thread creation is %lf",
			    mytime.stime);
				       printf("\n   o User Level running time of thread creation is %lf\n",
				       mytime.utime);

		exit(0);
	} /* Child */

	waitpid(pid, NULL, 0);
               realtime = times(&buffer);
		       printf("\n   o Real Time is %ld", realtime);
			       printf("\n   o System Level Time is %ld", buffer.tms_stime);
				       printf("\n   o User Level Time is %ld", buffer.tms_utime);
					       printf("\n   o Child System Level Time is %ld", buffer.tms_cstime);
						       printf("\n   o Child User Level Time is %ld", buffer.tms_cutime);


	return(1);

}
