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 > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains the "use of linux timer", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "the use of linux timer" bar!
First, take a look at the three internal timers provided by the linux operating system for each process.
ITIMER_REAL: give a specified time interval, reduce this count according to the actual time, and send a SIGALRM signal when the time interval is 0.
ITIMER_VIRTUAL: given a time interval, the count is reduced when the process is executed, and the SIGVTALRM signal is sent when the interval is 0.
ITIMER_PROF: given a time interval, when the process executes or the system schedules for the process, reduce the count, time is up, send out SIGPROF signals, which, in conjunction with ITIMER_VIRTUAL, is often used to calculate system kernel time and user time.
The functions used are:
# include
Int getitimer (int which, struct itimerval * value)
Int setitimer (int which, struct itimerval*newvalue, struct itimerval* oldvalue)
Strcut timeval
{
Long tv_sec; / * seconds * /
Long tv_usec; / * microsecond * /
}
Struct itimerval
{
Struct timeval it_interval; / * time interval * /
Struct timeval it_value; / * current time count * /
}
It_interval is used to specify how often a task is executed, and it_value is used to save the current time before the task is executed. For example, if you specify an it_interval of 2 seconds (microseconds is 0), at the beginning we set the it_value time to 2 seconds (microseconds is 0). After one second, the it_value decreases to 1, and after 1 second, the it_value decreases again to 0, which sends a signal (telling the user that the time is up and the task can be executed). And the system automatically resets the time of it_value to the value of it_interval, that is, 2 seconds, and then re-counts.
To help you understand this problem, let's look at an example:
# include
# include
# include
# include
# include
Static char msg [] = "time is running out\ n"
Static int len
/ / output information to standard error, telling the user that the time is up
Void prompt_info (int signo)
{
Write (STDERR_FILENO, msg, len)
}
/ / establish a signal processing mechanism
Void init_sigaction (void)
{
Struct sigaction tact
/ * when the signal arrives, the task handling function to be executed is prompt_info*/.
Tact.sa_handler = prompt_info
Tact.sa_flags = 0
/ * initialize the signal set * /
Sigemptyset & tact.sa_mask)
/ * establish a signal processing mechanism * /
Sigaction (SIGALRM, & tact, NULL)
}
Void init_time ()
{
Struct itimerval value
/ * set the time interval for task execution to 2 seconds and 0 microseconds * /
Value.it_value.tv_sec = 2
Value.it_value.tv_usec = 0
/ * set the initial time count to 2 seconds and 0 microseconds * /
Value.it_interval = value.it_value
/ * set timer ITIMER_REAL*/
Setitimer (ITIMER_REAL, & value, NULL)
}
Int main ()
{
Len = strlen (msg)
Init_sigaction ()
Init_time ()
While (1)
Exit (0)
}
The program's ITMER_REAL timer sends a SIGALRM signal every 2 seconds. When the main function receives this signal, it calls the signal processing function prompt_info to output the string time is running out on the standard error.
The use of ITIMER_VIRTUAL and ITIMER_PROF is similar. When you set the timer in setitimer to ITIMER_VIRTUAL, you change the SIGALRM in sigaction to SIGVTALarm. In the same way, ITIMER_PROF corresponds to SIGPROF.
However, you may notice that when you use ITIMER_VIRTUAL and ITIMER_PROF, you take a stopwatch and you will find that the program outputs a string at intervals of more than 2 seconds, or even 5-6 seconds. As for why, think about it for yourself.
Let's take a look at how to implement timed execution of tasks with sleep and usleep.
# include
# include
# include
# include
Static char msg [] = "I received a msg.\ n"
Int len
Void show_msg (int signo)
{
Write (STDERR_FILENO, msg, len)
}
Int main ()
{
Struct sigaction act
Union sigval tsval
Act.sa_handler = show_msg
Act.sa_flags = 0
Sigemptyset & act.sa_mask)
Sigaction (50 & act, NULL)
Len = strlen (msg)
While (1)
{
Sleep (2); / * Sleep for 2 seconds * /
/ * when you send a signal to the main process, you actually send a signal to yourself * /
Sigqueue (getpid (), 50, tsval)
}
Return 0
}
See, this is much simpler than the above, and you measure it with a stopwatch, the time is very accurate, specify 2 seconds to output a string for you. Therefore, if you only do the general timing, it is time to perform a task, this method is the easiest.
Let's take a look at timing by calculating the time difference ourselves:
# include
# include
# include
# include
# include
Static char msg [] = "I received a msg.\ n"
Int len
Static time_t lasttime
Void show_msg (int signo)
{
Write (STDERR_FILENO, msg, len)
}
Int main ()
{
Struct sigaction act
Union sigval tsval
Act.sa_handler = show_msg
Act.sa_flags = 0
Sigemptyset & act.sa_mask)
Sigaction (50 & act, NULL)
Len = strlen (msg)
Time & lasttime)
While (1)
{
Time_t nowtime
/ * get the current time * /
Time & nowtime)
/ * compare with the last time, if it is greater than or equal to 2 seconds, send a signal immediately * /
If (nowtime-lasttime > = 2)
{
/ * when you send a signal to the main process, you actually send a signal to yourself * /
Sigqueue (getpid (), 50, tsval)
Lasttime = nowtime
}
}
Return 0
}
The difference between this and the above is that you calculate the time difference manually. If you want to calculate the time difference more accurately, you can replace the time function with gettimeofday, which can be subtly accurate.
Several timing methods introduced above have their own advantages, and they are also different in timing efficiency, method and time accuracy. which method to use depends on the needs of your program.
Thank you for your reading, the above is the content of "the use of linux timer", after the study of this article, I believe you have a deeper understanding of the use of linux timer, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.