C language programming in Linux-time concept

Source: Internet
Author: User
C language programming in Linux-concept of time-general Linux technology-Linux programming and kernel information. The following is a detailed description. 1. Time Representation

In the program, we often need to output the current time of the system, for example, the output result using the date command. At this time, 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 Dec7 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. At this time, we can use the following function. # include

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
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 analyze the efficiency of the function algorithm. One output result on my machine is: Used Time: 0.556070.
3. Timer usage


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 (for the scheduling time of the process). This is often used with the previous one to calculate the system kernel time and user time and generate SIGPROF signals.


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. It is stored in the value.
The setitimer function sets the interval timer time value to newval. Saves 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, the corresponding signal is sent. Then it is set to the it_interval value.

Example:

# Include
# Include
# Include
# Include
# Include

# Define PROMPT "two seconds have elapsed \ 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 );
}
Execution result: a prompt is displayed every two seconds after the program is executed.
Related Article

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.