LINUX C language programming - Time concept

xiaoxiao2021-03-06  35

This chapter we learn Linux time representation and calculation functions

1. Time representation

2. Measurement of time

3. Use of the timer

-------------------------------------------------- ------------------------------

1. Time is in the program, we often output the current time of the system, such as the output of 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 from 0 o'clock from January 1, 1970. Stored in the Time_T structure. However, the return value of this function has no practical meaning for us. At this time we use the second function to use the number of seconds Transforming into a string. The return type of this function is fixed: a possible value. Thu DEC 7 14:58:59 2000 This string is fixed for 26

2. Time measurement Sometimes we want to calculate the time execution. For example, we have to perform time analysis of the algorithm. This time you can use the following function.

#include

INT getTimeOfDay (struct timeval * TV, struct timezone * tz);

Strut timeval {

Long TV_sec; / * seconds * /

Long TV_usec; ​​/ * microseconds * /

}

GetTimeOfDay saves time in structure TV.TZ usually use null instead.

#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)

TPEND.TV_USEC-TPSTART.TV_USEC;

TimeUse / = 1000000;

Printf ("Used Time:% F / N", TIMEUSE);

exit (0);

}

The execution time of this program output function, we can use this to perform system performance test, or the efficiency analysis of the function algorithm. A output result on my machine is: use Time: 0.556070

3. The timer uses the Linux operating system to provide three internal interval timers for each process.

ITIMER_REAL: Reduces the actual time. When you get a SIGALRM signal.

ITIMER_VIRTUAL: Reduces the effective time (time execution time). Generate a SigVTALRM signal.

ITIMER_PROF: Reduces the effective time and system time of the process (time for process schedule). This often uses one of the usage to calculate system kernel time and user time. Generate SigProf signals.

The specific operation function is:

#include

INT GetitiMER (int which, struct itimal * 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 intervals. Save the setitiMER function in Value Set the time value of the interval timer. Save the old values ​​in OldVal. Which means which one in the three timers. ItiMERVAL structure IT_Value is a reduced time that issues a corresponding signal when this value is 0. Then set to it_interval value. # Include

#include

#include

#include

#include

#define prompt "Time has passed for two seconds / N / A"

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);

}

The program will output a prompt every two seconds in two seconds.

转载请注明原文地址:https://www.9cbs.com/read-82283.html

New Post(0)