Linux timer and linux scheduled task

Source: Internet
Author: User

Linux timer and linux scheduled task

We often have the need to set the system to execute the corresponding action at a certain time, such as when the computer will automatically lock the screen, when it will automatically shut down, when the application will automatically run, and when it will automatically exit. These time-related functions must be implemented by the timer in the operating system.

In linux, the timer usage principle is very simple. You only need to set a timeout time and the corresponding execution function, and the system will execute the function set at the beginning during the timeout. The concept of timeout is a bit vague. It refers to the time you set. If the current time exceeds the time you set, it times out. For example, if you set the system to automatically shut down after five minutes, the system will remember the specific time after five minutes, that is, the current time plus five minutes. Five minutes later, the current time of the system is longer than the specific time set just now. As a result, the operation times out.

In linux, you can use the alarm function to set a timer. When the timer times out, a SIGALRM signal is generated. Therefore, to set the timeout behavior, you must set the corresponding function on the SIGALRM signal.

Include header file: # include <unistd. h>

Function prototype: unsigned int alarm (unsigned int seconds );

An example is as follows:

 

#include <stdio.h>#include <signal.h> #include <unistd.h>void SigFun(int signo){    printf("SigFun is running\n");}int main(){    if(signal(SIGALRM, SigFun) == SIG_ERR){        perror("signal\n");
     return -1; } alarm(5); pause();}

 

In linux, a process can have only one timer. Therefore, when a process needs to set multiple scheduled behaviors, corresponding measures must be taken to enable a single timer to implement multiple timer operations. There are two main methods: Time chain and time heap.

A time chain connects all scheduled behaviors in the form of a linked list, and maintains a fixed time-out timer in the process. When the timer times out, check whether all the actions on the linked list have timed out. If any, run the actions and delete them from the linked list. The biggest disadvantage of this method is that the whole linked list needs to be traversed at a fixed time, resulting in large overhead.

The time heap is to organize all scheduled behaviors in the form of the smallest heap, and maintain a timer with the heap top as the timeout time in the process. When the timer times out, check whether the heap top action times out. If the timer times out, run the action, delete it from the heap top, and continue the check. If the heap top action does not time out, the time-out time is used to continue to set the timer. The time heap does not need to check all scheduled behaviors at a fixed time. Instead, it only needs to run the corresponding time-out behaviors during the time-out period, which is more efficient than the time chain.

In linux, alarm functions are time-based in seconds. What should we do if we sometimes need to set behavior in smaller units of time, such as milliseconds? The setitimer function is provided in linux to provide a more precise timer.

Include header file: # include <sys/time. h>

Function prototype: int setitimer (int which, const struct itimerval * new_value, struct itimerval * old_value );

Parameters:

Int which has three options:

ITIMER_REAL: Decrements in real time, and deliversSIGALRM upon expiration.

ITIMER_VIRTUAL: Decrements only when the process is executing, anddeliversSIGVTALRM upon expiration.

ITIMER_PROF: Decrements both when the process executes and when the system is executing on behalf of the process. coupledwith ITIMER_VIRTUAL, this timer is usually used to profile the time spent by the application in user and kernel space. SIGPROF is delivered

The data structure of const struct itimerval * new_value is as follows:

Struct itimerval {struct timeval it_interval;/* Time-out each time after the first time-out */struct timeval it_value;/* time-out for the first time */}; struct timeval {long TV _sec; /* seconds */long TV _usec;/* microseconds */};

Struct itimerval * old_value is the original time. If you do not need to use it, you can use NULL

If the call is successful, 0 is returned. If the call fails,-1 is returned.

An example is as follows:

#include <stdio.h>#include <signal.h>#include <unistd.h>#include <sys/time.h>void SigFun(int signo){    printf("SigFun is running\n");    }int main(){    if(signal(SIGALRM, SigFun) == SIG_ERR){        perror("signal\n");        return -1;    }        struct itimerval tv;    tv.it_value.tv_usec = 500000;    tv.it_value.tv_sec = 0;    tv.it_interval.tv_usec = 300000;    tv.it_interval.tv_sec = 0;        if(setitimer(ITIMER_REAL, &tv, NULL) != 0){        perror("setitimer\n");        return -1;    }    while(true){        pause();    }}

 

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.