4) Introduction to Linux programming-time concept

Source: Internet
Author: User

4) Introduction to Linux programming-time concept
Time concept in Linux
In this chapter, we will learn about Linux time representation and computing functions.
Time Representation
Time Measurement
Timer usage
1. Time indicates that in the program, we often need to output the current time of the system. For example, we use the date command
In this case, we can use the following two functions:
# Include <time. h>

Time_t time (time_t * tloc );
Char * ctime (const time_t * Clock );
The time function returns the number of seconds since, January 1, January 1, 1970. It is stored in the time_t structure. However, this function returns
The return value has no practical significance for us. In this case, we use the second function to convert the number of seconds to a string.
.. The return type of this function is fixed: A string with a possible value of. Thu dec 7 14:58:59 2000
The length is fixed to 26
2. Sometimes we need to calculate the execution time of the program. For example, we need to analyze the algorithm time.
... You can use the following function at this time.
# Include <sys/time. h>

Int gettimeofday (struct timeval * TV, struct timezone * tz );
Strut timeval {
Long TV _sec;/* seconds */
Long TV _usec;/* Number of microseconds */
};
Gettimeofday stores the time in the Structure TV. tz Is generally replaced by null.
# Include <sys/time. H <
# Include <stdio. H <
# Include <math. H <
Void function ()
{
Unsigned int I, J;
Double Y;
For (I = 0; I <1000; I ++)
For (j = 0; j <1000; j ++)
Y = sin (double) I );
}
Main ()
{
Struct timeval tpstart, tpend;
Float timeuse;
Gettimeofday (& tpstart, null );
Function ();
Gettimeofday (& tpend, null );
Timeuse = 1000000 * (tpend. TV _sec-tpstart. TV _sec) +
TV _usec-tpstart. TV _usec;
Timeuse/= 1000000;
Printf ("used time: % F/N", timeuse );
Exit (0 );
}
This program outputs the execution time of the function. We can use this to test the system performance or calculate the function.
The output result on my machine is: used time: 0.556070.
3. The Linux operating system provides three internal interval timers for each process.
Itimer_real: reduces the actual time. The sigalrm signal is sent at that time.
Itimer_virtual: reduce the effective time (process execution time). Generate the sigvtalrm signal.
Itimer_prof: reduce the effective time and system time of the process (the time used for process scheduling ).
Used to calculate the system kernel time and user time and generate sigprof signals.
The specific operation functions are:
# Include <sys/time. h>
Int getitimer (INT which, struct itimerval * value );
Int setitimer (INT which, struct itimerval * newval,
Struct itimerval * oldval );
Struct itimerval {
Struct timeval it_interval;
Struct timeval it_value;
}
The getitimer function gets the time value of the interval timer. Save it in the value. The setitimer function sets the interval timer.
And save the old value in oldval. Which indicates which of the three timers is used.
The it_value in the itimerval structure is the time for reduction. When the value is 0, a corresponding signal is sent. However
And then set it to it_interval.
# Include <sys/time. h>
# Include <stdio. h>
# Include <unistd. h>
# Include <signal. h>
# Include <string. h>
# Define prompt "the time has passed for two seconds/n/"
Char * prompt = prompt;
Unsigned int Len;
Void prompt_info (INT signo)
{
Write (stderr_fileno, prompt, Len );
}
Void init_sigaction (void)
{
Struct sigaction Act;
Act. sa_handler = prompt_info;
Act. sa_flags = 0;
Sigemptyset (& act. sa_mask );
Sigaction (sigprof, & act, null );
}
Void init_time ()
{
Struct itimerval value;
Value. it_value. TV _sec = 2;
Value. it_value. TV _usec = 0;
Value. it_interval = value. it_value;
Setitimer (itimer_prof, & Value, null );
}
Int main ()
{
Len = strlen (prompt );
Init_sigaction ();
Init_time ();
While (1 );
Exit (0 );
}
This program will output a prompt every two seconds after execution.

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.