Time concept in Linux

Source: Internet
Author: User

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, the output result of the date command. In this case, we can use the following two functions:

# Include

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. Stored in the time_t structure. However, the return value of this function has no practical significance for us. In this case, we use the second function to convert seconds to strings. The return type of this function is fixed: a possible value is. Thu dec 7 14:58:59 2000 the string length is fixed to 26
2. Time Measurement 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

Int gettimeofday (struct timeval * TV, struct timezone * tz );

Strut timeval {
Long TV _sec;/* seconds */
Long TV _usec;/* Number of microseconds */
};

Gettimeofday saves the time in the Structure TV. Tz Is generally replaced by null.

# Include
# Include
# Include

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: % FN", timeuse );
Exit (0 );
}

This program outputs the execution time of the function. We can use this to test the system performance or analyze the efficiency of the function algorithm. One 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. Then the sigalrm signal is sent.
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 (for the time used for process scheduling ). This is often used with the previous one to calculate the system kernel time and user time. Generate a sigprof signal.
The specific operation functions are:

# Include
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 obtains the time value of the interval timer. The setitimer function is saved in value to set the interval timer time value to newval. 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. Set it to it_interval.

# Include
# Include
# Include
# Include
# Include

# Define prompt "the time has passed for two seconds na"

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.

 

Reference: http://www.chinalinuxpub.com/read.php? WID = 356

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.