In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what are the time-related functions and structures under linux". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Time type. There are 6 common time types under Linux: time_t,struct timeb, struct timeval,struct timespec,clock_t, struct tm.
(1) time_t is a long integer, which is generally used to indicate the number of seconds since 1970.
The type is defined in.
It is generally obtained by time_t time = time (NULL);.
(2) struct timeb structure: there are two main members, one is second, the other is millisecond, the accuracy is millisecond.
Struct timeb
{
Time_t time
Unsigned short millitm
Short timezone
Short dstflag
}
Get timeb by the function int ftime (struct timeb * tp);.
Success returns 0, failure returns-1.
(3) struct timeval has two members, one is second, the other is subtle.
Struct timeval
{
Long tv_sec; / * seconds * /
Long tv_usec; / * microseconds * /
}
Obtained by int gettimeofday (struct timeval * tv, struct timezone * tz);.
The struct timezone structure is defined as:
Struct timezone
{
How many minutes is the time difference between int tz_minuteswest; / * and Greewich * /
Int tz_dsttime; / * Daylight Saving time status * /
}
(4) struct timespec has two members, one is second and the other is nanosecond, so the highest accuracy is nanosecond.
Struct timespec
{
Time_t tv_sec; / * seconds * /
Long tv_nsec; / * nanoseconds * /
}
It is generally obtained by the function long clock_gettime (clockid_t which_clock, struct timespec * tp);.
Get the time of a specific clock, which is returned through the tp structure. At present, six kinds of clocks are defined, namely
The current time of the CLOCK_REALTIME system, calculated from 1. 1, 1970
The startup time of the CLOCK_MONOTONIC system cannot be set.
CLOCK_PROCESS_CPUTIME_ID process run time
CLOCK_THREAD_CPUTIME_ID thread run time
A high-precision version of CLOCK_REALTIME_HR CLOCK_REALTIME
A high-precision version of CLOCK_MONOTONIC_HR CLOCK_MONOTONIC
Get the time accuracy of a specific clock:
Long clock_getres (clockid_t)
Set the time for a specific clock:
Long clock_settime (clockid_t, struct timespec*)
If the time specified in hibernating time is returned early due to signal interruption, the remaining time will be returned by left_time:
Long clock_nanosleep (clockid_t, int flag,timespec* time,timespec* left_time)
(5) clock_t type, returned by clock_t clock (); to get.
Represents the cpu time occupied by the process. Accurate to microseconds.
(6) struct tm is an intuitive representation of time:
Struct tm
{
Int tm_sec; / * seconds * /
Int tm_min; / * minutes * /
Int tm_hour; / * hours * /
Int tm_mday; / * day of the month * /
Int tm_mon; / * month * /
Int tm_year; / * year * /
Int tm_wday; / * day of the week * /
Int tm_yday; / * day in the year * /
Int tm_isdst; / * daylight saving time * /
}
2. Get the current time
Under all UNIX, there is a function of time ().
Time_t time (time_t * t)
This function returns the number of seconds calculated from epoch, and if t is non-null, it will fill the time value into t.
For some requirements that require a high degree of precision, Linux provides gettimeofday ().
Int gettimeofday (struct timeval * tv,struct timezone * tz)
Int settimeofday (const struct timeval * tv,const struct timezone * tz)
Struct tm format time function
Struct tm * gmtime (const time_t * t)
To Greenwich mean time. Sometimes called GMT or UTC.
Struct tm * localtime (const time_t * t)
Convert to local time. It can be modified by the TZ environment variables in a machine, different users represent different times.
Time_t mktime (struct tm * tp)
Convert tm to time_t format, using local time.
Tme_t timegm (strut tm * tp)
Convert tm to time_t format, using UTC time.
Double difftime (time_t T2 timekeeper T1)
Calculate the second difference.
Text time format function
Char * asctime (struct tm * tp)
Char * ctime (struct tm * tp)
Both functions convert the time format to the standard UNIX time format.
Mon May 3 08:23:35 1999
The ctime rate is expressed in local time, while the asctime is expressed in timezone information in the tm structure.
Size_t strftime (char * str,size_t max,char * fmt,struct tm * tp)
Strftime is a bit like sprintf in that its format is specified by fmt.
% a: the name of this day, abbreviation.
% A: the name of this day, full name.
% b: month name, abbreviation.
% B: month name, full name.
% c: same format as ctime/asctime.
% d: the name of the day of this month, starting from zero.
% H: the number of hours of the day, 24-hour system, from zero.
% I: the hour of the day, the 12-hour system, calculated from zero.
% j: the day of the year, calculated from zero.
% m: the month of the year, from zero.
% M: the minute of the hour, calculated from zero.
% p: AM or PM.
% S: the second ordinal of the minute, starting from zero.
% U: what is the date of the year, calculated from the first day?
% W: what is the number of the year, calculated from the first one.
% w: what day is it, counting from zero?
% x: local date.
% X: local time.
% y: double-digit year.
% Y: four-digit year.
% Z: abbreviation for the name of the time zone.
%:% symbol.
Char * strptime (char * fmt,struct tm * tp)
Like scanf, the string is interpreted into tm format.
% h: same as% b and% B.
% c: read% x and% X formats.
% C: read% C format.
% e: same as% d.
% D: read% m/%d/%y format.
% k: same as% H.
% l: same as% I.
% r: read the "% I:%M:%S% p" format.
% R: read the "% HRV% M" format.
% T: read the "% H:%M:%S" format.
% y: read a two-digit year.
% Y: read four-digit years.
Here is a small example of how to obtain the current time of the system:
Time_t now
Struct tm * timenow
Char strtemp [255]
Time & now)
Timenow = localtime (& now)
Printf ("recent time is:% s\ n", asctime (timenow))
3. Delay
The delay can take the following functions:
Unsigned int sleep (unsigned int seconds)
Sleep () causes the current program to "hibernate" for seconds seconds unless it receives an "irresistible" signal.
If sleep () doesn't get enough sleep, it will return the amount of time it still needs to sleep, otherwise it will generally return zero.
Void usleep (unsigned long usec)
Usleep is similar to sleep (), except that the unit of seconds is 10E-6 seconds.
Int select (0real null timeval * tv)
You can take advantage of select's ability to implement sleep (), which will not wait for any event to occur.
Int nanosleep (struct timespec * req,struct timespec * rem)
Nanosleep will sleep at the time specified by req. If rem is non-null and you don't have enough sleep, you will put the time to make up your sleep on rem.
4. Timer
4.1 、 alarm
If precision is not required, alarm () and signal () will suffice
Unsigned int alarm (unsigned int seconds)
Specially designed for SIGALRM signal, after the specified time seconds seconds, the SIGALRM signal, also known as alarm clock time, will be sent to the process itself. After the process calls alarm, any previous alarm () calls will be invalid. If the parameter seconds is zero, no alarm clock time will be included in the process. If the alarm clock time has been set in the process before calling alarm (), the remaining time of the previous alarm clock time is returned, otherwise 0 is returned.
Example:
# include
# include
# include
Void sigalrm_fn (int sig)
{
/ * Do something * /
Printf ("alarm!\ n")
Alarm (2)
Return
}
Int main (void)
{
Signal (SIGALRM, sigalrm_fn)
Alarm (2)
/ * Do someting * /
While (1) pause ()
}
4.2 、 setitimer
Int setitimer (int which, const struct itimerval * value, struct itimerval * ovalue))
Setitimer () is more powerful than alarm and supports three types of timers:
ITIMER_REAL: in terms of the real time of the system, it sends out a SIGALRM signal.
ITIMER_VIRTUAL: based on the actual execution time of the trip, it sends out a SIGVTALRM signal.
ITIMER_PROF: calculated in terms of the actual execution of the journey and the time spent in the core, it sends a SIGPROF signal.
The first parameter which of Setitimer () specifies the timer type (one of the above three); the second parameter is an instance of the structure itimerval; and the third parameter can be left unprocessed.
The Setitimer () call returns 0 successfully, otherwise-1.
Here is a simple example of a setitimer call, in which a SIGALRM is emitted every second and a SIGVTALRM signal is issued every half a second:
# include
# include
# include
# include
# include
# include
Int sec
Void sigroutine (int signo) {
Switch (signo) {
Case SIGALRM:
Printf ("Catch a signal-- SIGALRM\ n")
Signal (SIGALRM, sigroutine)
Break
Case SIGVTALRM:
Printf ("Catch a signal-- SIGVTALRM\ n")
Signal (SIGVTALRM, sigroutine)
Break
}
Return
}
Int main ()
{
Struct itimerval value, ovalue, value2
Sec = 5
Printf ("process id is d", getpid ())
Signal (SIGALRM, sigroutine)
Signal (SIGVTALRM, sigroutine)
Value.it_value.tv_sec = 1
Value.it_value.tv_usec = 0
Value.it_interval.tv_sec = 1
Value.it_interval.tv_usec = 0
Setitimer (ITIMER_REAL, & value, & ovalue)
Value2.it_value.tv_sec = 0
Value2.it_value.tv_usec = 500000
Value2.it_interval.tv_sec = 0
Value2.it_interval.tv_usec = 500000
Setitimer (ITIMER_VIRTUAL, & value2, & ovalue)
For (;;)
}
The screen copy of the example is as follows:
Localhost:~$. / timer_test
Process id is 579
Catch a signal-SIGVTALRM
Catch a signal-SIGALRM
Catch a signal-SIGVTALRM
Catch a signal-SIGVTALRM
Catch a signal-SIGALRM
Catch a signal-GVTALRM
Note: the Linux signaling mechanism is basically inherited from the Unix system. The signal mechanism in the early Unix system is relatively simple and primitive, and then some problems are exposed in practice. therefore, those signals based on the early mechanism are called "unreliable signals", and the signals whose signal value is less than SIGRTMIN (Red hat 7.2, SIGRTMIN=32,SIGRTMAX=63) are unreliable signals. This is the source of "unreliable signal". Its main problem is that every time the process processes the signal, it sets the response to the signal as the default action. In some cases, it will result in incorrect handling of the signal; therefore, if the user does not want to do so, the user will have to call signal () again at the end of the signal handling function to reinstall the signal.
This is the end of the content of "what are the time-related functions and structures under linux". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.