4) Introduction to Linux programming--time concept

Source: Internet
Author: User

4) Introduction to Programming--Time Concept Preface: The concept of time in Linux This chapter we study the use of time representation of Linux and the measurement timer for calculating function time1. Time is expressed in the program, we often want to output the current time of the system, such as we use the date command output results. We can use the following two functions at this time #include<time.h>; time_t time (time_t*Tloc); Char*ctime (Consttime_t *clock); The time function returns the number of seconds since 0 o'clock January 1, 1970. Stored in the time_t structure. However, the return value of this function is of little practical significance to us. At this point we use the second function to convert the number of seconds to a string: The return type of this function is fixed: One possible value is. Thu Dec7  -: -: -  -The length of this string is fixed to2. Measurement of time sometimes we have to calculate the time of the program execution. For example, we want to do time analysis on the algorithm. You can use the following function at this time. #include<sys/time.h>; intGettimeofday (structTimeval *tv,structTimeZone *TZ); Strut Timeval {LongTv_sec;/*Number of seconds*/ LongTv_usec;/*Number of microseconds*/ }; Gettimeofday save time in the structure TV. TZ generally we use NULL instead. #include<sys/time.h<#include<stdio.h<#include<math.h<voidfunction () {unsignedinti,j;Doubley; for(i=0;i< +; i++)  for(j=0;j< +; j + +) y=sin ((Double) (i); } main () {structtimeval tpstart,tpend;floattimeuse; Gettimeofday (&tpstart,null); function (); Gettimeofday (&tpend,null); Timeuse=1000000* (TPEND.TV_SEC-TPSTART.TV_SEC) +tpend.tv_usec-tpstart.tv_usec; Timeuse/=1000000; printf ("used time:%f\n", Timeuse); Exit (0); This program outputs the execution time of the function, which we can use to test the performance of the system, or the efficiency of the function algorithm. One of the outputs on my machine is: Used time:0.556070 3. The timer uses the Linux operating system to provide 3 internal interval timers for each process. Itimer_real: Reduce the actual time. When the time comes, a sigalrm signal is emitted. Itimer_virtual: Reduces the effective time (time of Process Execution). Generate SIGVTALRM signal. Itimer_prof: Reduce the process's effective time and system time (for process scheduling). This is often used with the above one for computing kernel time and user time. Generates SIGPROF signals. The specific operation function is: #include<sys/time.h>; intGetitimer (intWhich,structItimerval *value); intSetitimer (intWhich,structItimerval *newval,structItimerval *oldval); structItimerval {structtimeval It_interval;structtimeval It_value;} The Getitimer function gets the time value of the interval timer. Save in value the Setitimer function sets the interval timer to a time value of newval. and saves the old value in Oldval.  which indicates which of the three timers are used. The it_value in the itimerval structure is the reduced time, and when this value is 0, the corresponding signal is sent out. Then set the value to It_interval. #include<sys/time.h>; #include<stdio.h>; #include<unistd.h>; #include<signal.h>; #include<string.h>; #definePROMPT "Time has passed for two seconds \n\a"Char*prompt=PROMPT; unsignedintLen;voidPrompt_info (intSigno) {Write (Stderr_fileno,prompt,len);} voidInit_sigaction (void) { structsigaction Act; Act.sa_handler=prompt_info; Act.sa_flags=0; Sigemptyset (&act.sa_mask); Sigaction (Sigprof,&act,null); } voidInit_time () {structitimerval value; value.it_value.tv_sec=2; Value.it_value.tv_usec=0; Value.it_interval=value.it_value; Setitimer (Itimer_prof,&value,null); } intMain () {Len=strlen (prompt); init_sigaction (); Init_time (); while(1); Exit (0);   This program outputs a hint after each execution for two seconds. 

4) Introduction to Linux programming--time concept

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.