Thread: Counting down real, CPU, Kernel time (PLEASE HELP)!

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

    Counting down real, CPU, Kernel time (PLEASE HELP)!

    Hi!. I have an exercise that i already have solved. BUT i have a problem and don't know if it is correct. What i mean is that i don't know how to check if it is correct...can anyone help me???? PLEASE guys it' s so important to me!

    What i don't understand is that the kernel time and the CPU time are the same or zero always. Why this is happening...?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/time.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <signal.h>
    #include <unistd.h>
    
    long unsigned factorial(unsigned int n);
    long unsigned bubbleSort(unsigned int n);
    long unsigned quickSearch(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 struct itimerval p_proft, c1_proft, c2_proft;
    
    int flag, parent_PID, child1_PID, child2_PID;
    
    void sig_handler(int signo){
    	printf("Ηρθε σήμα!\n");
    	switch(signo){
    		case SIGVTALRM:
    			flag = getpid();
    				if(flag==parent_PID){
    					//printf("parent\n");
    					p_virtt_secs++;
    				}
    				if(flag==child1_PID){
    					//printf("paidi1\n");
    					c1_virtt_secs++;
    				}
    				if(flag==child2_PID){
    					//printf("paidi2\n");
    					c2_virtt_secs++;
    				}
    				//printf("\np_virtt_secs: %ld\nc1_virtt_secs: %ld\nc2_virtt_secs: %ld\n", p_virtt_secs, c1_virtt_secs, c2_virtt_secs);
    			break;
    		case SIGALRM:
    			flag = getpid();
    				if(flag==parent_PID){
    					//printf("pateras\n");
    					p_realt_secs++;
    				}
    				if(flag==child1_PID){
    					//printf("paidi1\n");
    					c1_realt_secs++;
    				}
    				if(flag==child2_PID){
    					//printf("paidi2\n");
    					c2_realt_secs++;
    				}
    				//printf("\np_realt_secs: %ld\nc1_realt_secs: %ld\nc2_realt_secs: %ld\n", p_realt_secs, c1_realt_secs, c2_realt_secs);
    			break;
    		case SIGPROF:
    			flag = getpid();
    				if(flag==parent_PID){
    					//printf("pateras\n");
    					p_proft_secs++;
    				}
    				if(flag==child1_PID){
    					//printf("paidi1\n");
    					c1_proft_secs++;
    				}
    				if(flag==child2_PID){
    					//printf("paidi2\n");
    					c2_proft_secs++;
    				}
    				//printf("\np_proft_secs: %ld\nc1_proft_secs: %ld\nc2_proft_secs: %ld\n", p_proft_secs, c1_proft_secs, c2_proft_secs);
    			break;
    		default:
    			break;
    	}
    }
    
    
    
    /* Αυτά δεν θα μπούν στον κώδικά μας απλά για βοήθεια είναι! */
    /*
    struct itimerval {
    	struct timeval it_interval; // next value
    	struct timeval it_value;    // current value
    };
    */
    
    /*
    struct timeval {
    	long tv_sec;                // seconds
    	long tv_usec;               // microseconds
    };
    */
    
    int main(int argc, char **argv)
    {
    	long unsigned res = 0;
    	int pid1, pid2;
    	unsigned int funarg;
    	int status;
    
    	/* get command line argument, funarg */
    	if(argc != 2){ 	/* no main argument given */
    		printf("No main-argument given. Try again and you may be lucky...\n");
    		exit(1);
    	}
    	funarg = atoi(argv[1]); /* main arguments -> char MUST be integers */
    
    	/* initialize parent REAL timers */
    	p_realt.it_interval.tv_sec = 1;
    	p_realt.it_interval.tv_usec = 0;
    	p_realt.it_value.tv_sec = 1;
    	p_realt.it_value.tv_usec = 0;
    	
    	/* initialize parent VIRTUAL timers */	
    	p_virtt.it_interval.tv_sec = 1;
    	p_virtt.it_interval.tv_usec = 0;
    	p_virtt.it_value.tv_sec = 1;
    	p_virtt.it_value.tv_usec = 0;	
    	
    	/* initialize parent PROF timers */	
    	p_proft.it_interval.tv_sec = 1;
    	p_proft.it_interval.tv_usec = 0;
    	p_proft.it_value.tv_sec = 1;
    	p_proft.it_value.tv_usec = 0;	
    	
    	/* initialize child1 REAL timers */
    	c1_realt.it_interval.tv_sec = 1;
    	c1_realt.it_interval.tv_usec = 0;
    	c1_realt.it_value.tv_sec = 1;
    	c1_realt.it_value.tv_usec = 0;
    	
    	/* initialize child1 VIRTUAL timers */
    	c1_virtt.it_interval.tv_sec = 1;
    	c1_virtt.it_interval.tv_usec = 0;
    	c1_virtt.it_value.tv_sec = 1;
    	c1_virtt.it_value.tv_usec = 0;	
    	
    	/* initialize child1 PROF timers */
    	c1_proft.it_interval.tv_sec = 1;
    	c1_proft.it_interval.tv_usec = 0;	
    	c1_proft.it_value.tv_sec = 1;
    	c1_proft.it_value.tv_usec = 0;
    	
    	/* initialize child2 REAL timers */
    	c2_realt.it_interval.tv_sec = 1;
    	c2_realt.it_interval.tv_usec = 0;
    	c2_realt.it_value.tv_sec = 1;
    	c2_realt.it_value.tv_usec = 0;
    
    	/* initialize child2 VIRTUAL timers */
    	c2_virtt.it_interval.tv_sec = 1;
    	c2_virtt.it_interval.tv_usec = 0;
    	c2_virtt.it_value.tv_sec = 1;
    	c2_virtt.it_value.tv_usec = 0;	
    
    	/* initialize child2 PROF timers */
    	c2_proft.it_interval.tv_sec = 1;
    	c2_proft.it_interval.tv_usec = 0;
    	c2_proft.it_value.tv_sec = 1;
    	c2_proft.it_value.tv_usec = 0;
    	
    	/* enable your signal handlers for the parent */
    	signal(SIGALRM, sig_handler); 				// συναγερμός 
    	signal(SIGVTALRM, sig_handler); 			// λήξη εικονικού χρονιστή
    	signal(SIGPROF, sig_handler); 				// λήξη χρονιστή προφίλ	
    
    	/* set the parent's itimers */
    	setitimer(ITIMER_REAL, &p_realt, NULL); 		/* πραγματικός χρόνος πατέρα*/
    	setitimer(ITIMER_VIRTUAL, &p_virtt, NULL); 		/* χρόνος που εκτελέστηκε η διεργασία */
    	setitimer(ITIMER_PROF, &p_proft, NULL); 		/* χρόνος που χρησιμοποιήσε η ο πυρήνας την CPU */
    	
    	
    	pid1 = fork();
    	if(pid1 == 0){		/* child1 */
    	
    		child1_PID = getpid();
    		
    		/* enable child 1 signal handlers (disable parent handlers) */
    		signal(SIGALRM, sig_handler); 			// συναγερμός 
    		signal(SIGVTALRM, sig_handler);		 	// λήξη εικονικού χρονιστή
    		signal(SIGPROF, sig_handler);	 		// λήξη χρονιστή προφίλ	
    		
    		/* set the child 1 timers */
    		setitimer(ITIMER_REAL, &c1_realt, NULL); 	/* πραγματικός χρόνος πατέρα*/
    		setitimer(ITIMER_VIRTUAL, &c1_virtt, NULL); 	/* χρόνος που εκτελέστηκε η διεργασία */
    		setitimer(ITIMER_PROF, &c1_proft, NULL); 	/* χρόνος που χρησιμοποιήσε η ο πυρήνας την CPU */
    		
    		/* start yourfunction() at child1 */
    		//printf("BuubleSort function...\n");
    		res = bubbleSort(funarg);
    
    		/*Read the child 1 itimer values and report them */
    		getitimer(ITIMER_PROF, &c1_realt);
    		getitimer(ITIMER_REAL, &c1_virtt);
    		getitimer(ITIMER_VIRTUAL, &c1_proft);
    
    		printf("\n");
    		printf("About the CHILD-1...\n");
    		printf("Real -> %ld sec\n", c1_realt_secs);
    		printf("CPU(proft) -> %ld sec\n", c1_proft_secs);
    		printf("User(virtt) -> %ld sec\n",  c1_virtt_secs);
    		//printf("Kernel -> %ld sec\n", p_proft_secs-c1_virtt_secs);
    
    		fflush(stdout);
    		exit(0);
    	}else{
    		pid2 = fork();
    
    		if(pid2 == 0){	/* 2o child */
    			
    			child2_PID = getpid();
    
    			/* enable the child 2 signal handlers (disable parent handlers) */
    			signal(SIGALRM, sig_handler); 			// συναγερμός 
    			signal(SIGVTALRM, sig_handler); 		// λήξη εικονικού χρονιστή
    			signal(SIGPROF, sig_handler); 			// λήξη χρονιστή προφίλ	
    
    			/* set the child 2 itimers */
    			setitimer(ITIMER_REAL, &c2_realt, NULL); 	// πραγματικός χρόνος πατέρα
    			setitimer(ITIMER_VIRTUAL, &c2_virtt, NULL); 	// χρόνος που εκτελέστηκε η διεργασία
    			setitimer(ITIMER_PROF, &c2_proft, NULL); 	// χρόνος που χρησιμοποιήσε η ο πυρήνας την CPU
    
    			/* start yourfunction() at child1 */
    			//printf("\nFactorial function...\n");
    			res = factorial(funarg);
    
    			/* Read the child 1 itimer values and report them */
    			getitimer(ITIMER_PROF, &c2_realt);
    			getitimer(ITIMER_REAL, &c2_virtt);
    			getitimer(ITIMER_VIRTUAL, &c2_proft);
    			printf("About the CHILD-2...\n");
    			printf("Real -> %ld sec\n", c2_realt_secs);
    			printf("CPU(proft) -> %ld sec\n", c2_proft_secs);
    			printf("User(virtt) -> %ld sec\n",  c2_virtt_secs);
    			printf("Kernel -> %ld sec\n", p_proft_secs-c1_virtt_secs);
    			
    			printf("\n");
    			fflush(stdout);
    			exit(0);
    		}else{		/* parent */
    
    			parent_PID = getpid();
    
    			/* enable the parent signal handlers */
    			signal(SIGALRM, sig_handler); 			// συναγερμός 
    			signal(SIGVTALRM, sig_handler); 		// λήξη εικονικού χρονιστή
    			signal(SIGPROF, sig_handler); 			// λήξη χρονιστή προφίλ	
    
    			/* set the parent itimers */
    			setitimer(ITIMER_REAL, &p_realt, NULL); 	// πραγματικός χρόνος πατέρα
    			setitimer(ITIMER_VIRTUAL, &p_virtt, NULL); 	// χρόνος που εκτελέστηκε η διεργασία
    			setitimer(ITIMER_PROF, &p_proft, NULL); 	// χρόνος που χρησιμοποιήσε η ο πυρήνας την CPU
    			
    			/* start your function at the parent */
    			//printf("QuickSearch function...\n");
    			res = quickSearch(funarg);
    			
    			/* wait for the children to terminate */
    			//printf("Waitting to end the child1 & child2...\n");
    			waitpid(0, &status, 0);
    			waitpid(0, &status, 0);
    			
    			/* read the parent itimer values and report them */
    			getitimer(ITIMER_PROF, &p_realt);
    			getitimer(ITIMER_REAL, &p_virtt);
    			getitimer(ITIMER_VIRTUAL, &p_proft);
    			
    			printf("\n");
    			printf("About the PARENT...\n");
    			printf("Real -> %ld sec\n", p_realt_secs);
    			printf("CPU(proft) -> %ld sec\n", p_proft_secs);
    			printf("User(virtt) -> %ld sec\n",  p_virtt_secs);
    			printf("Kernel -> %ld sec\n", p_proft_secs-c1_virtt_secs);
    			
    			fflush(stdout);
    			exit(0);
    		}
    		printf("this line should never be printed\n");
    		printf("\n");
    	}
    }/* end of main */
    
    // ----------------------------------------------------------
    long unsigned factorial(unsigned int n) {
    	int i, flag = 1;
    		
    		for(i=1; i<n; i++){
    			flag = flag * i;
    		}
    return flag;
    }
    // ----------------------------------------------------------
    
    // ----------------------------------------------------------
    long unsigned int recurce(unsigned int n) {
    		if(n == 0){
    			return 0;
    		}else if(n == 1){
    			return 1;
    		}else{
    			return recurce(n-1) + recurce(n-2);
    		}
    }
    
    
    long unsigned int alwaysexecuting(unsigned int n) {
    	alarm(6);
    	while(1);
    }
    // ----------------------------------------------------------
    
    // -----------------------------------------------------------
    long unsigned int bubbleSort(unsigned array_size)
    {
      int i, j, temp;
      int *numbers;
     
      	numbers = (int *)malloc(array_size*sizeof(int));
      	if(numbers == NULL){
      		printf("Malloc failed ! ! ! \n");
      		return 1;
      	}
     
      	for (i = (array_size - 1); i > 0; i--){
        		for (j = 1; j <= i; j++){
    			if (numbers[j-1] > numbers[j]){
            			temp = numbers[j-1];
            			numbers[j-1] = numbers[j];
            			numbers[j] = temp;
          			}
        		}
      	}
    return 0;
    }
    // ----------------------------------------------------------
    
    // ----------------------------------------------------------
    long unsigned int quickSearch(unsigned n){
    	int flag, i;
    	struct timeval opcode;
    		
    		for(i=0; i<n; i++){
    			if(n>i){
    				for(i=0; i<n; i++){
    					gettimeofday(&opcode,NULL);
    				}
    				
    				return (n+1);
    			}
    		}
    return 1;
    }
    // ----------------------------------------------------------
    
    // ----------------------------------------------------------
    long unsigned int yourfunction12(unsigned int n){
    	int i;
    	FILE *pFile;
    
    	pFile = fopen ("myfile.txt","w");
    		if (pFile != NULL){
    			fputs ("fopen example\n", pFile);
    			fclose (pFile);
    			for(i=0; i<99; i++){ }
    		}else{
    			exit(1);
    		}
    		
    return 0;
    }
    // ----------------------------------------------------------

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Look into the time command (section 1 of the man pages). Your real time is pretty good, but your user and system look off (though it's hard to tell with only one-second resolution). Your CPU time should (I think) reflect the sum of user and kernel/sys time. Note that on a program like this, that doesn't do lots of I/O, you won't have much kernel time at all. Kernel time accumulates while you wait for a file, socket, etc to open, and things like fork/exec calls to spawn the new process (but not the new process itself). The parent shouldn't accumulate much sys time at all, since it's not really doing anything other than waiting for the children to execute and your quickSearch never makes it to the gettimeofday() call.

    Here is what I got when I ran your program:
    Code:
    $ gcc -Wall -o time time.c
    
    $ time ./time 50000
    About the CHILD-2...
    Real -> 0 sec
    CPU(proft) -> 0 sec
    User(virtt) -> 0 sec
    Kernel -> 0 sec
    
    Ηρθε σήμα!
    Ηρθε σήμα!
    Ηρθε σήμα!
    Ηρθε σήμα!
    Ηρθε σήμα!
    Ηρθε σήμα!
    Ηρθε σήμα!
    Ηρθε σήμα!
    Ηρθε σήμα!
    Ηρθε σήμα!
    Ηρθε σήμα!
    Ηρθε σήμα!
    Ηρθε σήμα!
    Ηρθε σήμα!
    Ηρθε σήμα!
    Ηρθε σήμα!
    Ηρθε σήμα!
    Ηρθε σήμα!
    Ηρθε σήμα!
    Ηρθε σήμα!
    
    About the CHILD-1...
    Real -> 7 sec
    CPU(proft) -> 3 sec
    User(virtt) -> 3 sec
    
    About the PARENT...
    Real -> 7 sec
    CPU(proft) -> 0 sec
    User(virtt) -> 0 sec
    Kernel -> 0 sec
    
    real    0m7.204s
    user    0m5.078s
    sys     0m0.023s
    There is also the times() function that tracks current user and sys times for a given process and all children in clocks ticks. Check section 2 of the man pages. You can use gettimeofday() to get the real (elapsed) time with millisecond resolution.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    i know the other functions and i have made some examples so as to see how working...but my problem is that i can't find a function that needs CPU AND OR Kernel time . Can you help me a little more?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help fast
    By blackice in forum C Programming
    Replies: 2
    Last Post: 01-31-2011, 01:19 PM
  2. Portable method of accessing CPU time for given process
    By saeculum in forum Linux Programming
    Replies: 3
    Last Post: 03-13-2009, 03:44 PM
  3. Is this really true or it's just science fiction?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 145
    Last Post: 04-09-2002, 06:17 PM
  4. Programming Puns
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 44
    Last Post: 03-23-2002, 04:38 PM
  5. Real Time?
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 09-23-2001, 03:33 PM