Thread: working with system time

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    230

    working with system time

    Hey, after 6 months i come back programming again in C...i now have a lesson "operating systems" and i have a book of tanenbaum(a bit big!!)...So,

    i have an exercise to solve and i need help. I know quite well C but i have a big problem with this exercise cause i do not know where to start.

    Here is the code that we have been given,

    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:

    i must use ITIMER_REAL to make my gettimeoftheday(). I must set up the signal every second. Use the signals so as to know when it is zero and count how many sec are. This is the first part. And i' ll be happy if anyone could help me a little cause i have no idea where to start.

    THANKS EVERYONE...

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    A google search on "itimer_real" returns a pretty good explanation on its use on the very first link: Setting an Alarm - The GNU C Library
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    thank you vary much and i will read this BUT can anyone help me a little how to start? what to do first? explain maybe what i must do...please...thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  2. Working with Time
    By mrafcho001 in forum C++ Programming
    Replies: 5
    Last Post: 08-28-2006, 08:22 PM
  3. The space time continueimnms mm... (rant)
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 06-27-2004, 01:21 PM
  4. Cprogramming System Time
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 08-06-2003, 02:28 PM
  5. Setting the System Time
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-01-2001, 11:18 AM