Objective
The signals in Linux provide the function of soft interrupts. Provides a way to handle asynchronous time, which can be reasonably arranged by signaling an asynchronous task.
Linux defines the following major system signals: Name Default action description SIGHUP terminate process End line hangs up
SIGINT terminating process Interrupt process
Sigquit establish core file termination process, and generate core file
Sigill Creating a core file illegal directive
SIGTRAP build core file tracking self-trapping
Sigbus establishing a core file bus error
SIGSEGV creating core file Segment Illegal error
SIGFPE creating a core file floating point exception
Sigiot creating a core file to perform I/O self-trapping
SIGKILL Terminate process Kill process
Sigpipe terminating a process writing data to a pipeline that does not have a read process
Sigalarm Terminating process timer to
SIGTERM terminating process software termination signal
SIGSTOP stop signal stop process non-terminal
SIGTSTP stop signal to stop the process terminal
Sigcont ignore signal continue execution of a stopped process
Sigurg ignoring signal I/O emergency signal
SIGIO ignoring the signal descriptor can be I/O
SIGCHLD Ignore signal Notify parent process when child process stops or exits
Sigttou Stop process background process write terminal
Sigttin Stop process background process read Terminal
SIGXGPU terminating process CPU time-out
SIGXFSZ termination process file length too long
Sigwinch ignoring signal window size changes
SIGPROF terminating process statistics distribution graph with timer to time
SIGUSR1 terminating a process user-defined signal 1
SIGUSR2 terminating a process user-defined signal 2
SIGVTALRM Terminate process virtual timer to apply:
Below we apply sigalarm to set the action performed once per second:
struct Itimerval onesec; Signal (SIGALRM, demo_updatetime); OneSec.it_value.tv_sec = 1; oneSec.it_value.tv_usec = 0; OneSec.it_interval.tv_sec = 1; oneSec.it_interval.tv_usec = 0; Setitimer (Itimer_real, &onesec, NULL);
The Demo_updatetime () function is the function that is registered to execute once per second.
Switch (signo) {case SIGALRM: Time (&timeval); Localtime_r (&timeval, &tmcur); Update date only once one day if (tmold.tm_mday! = tmcur.tm_mday) { char temp[40]; Get day of week from the This is only. Ctime_r (&timeval, temp); sprintf (Osdstr, "%04d-%02d-%02d", tmcur.tm_year+1900, tmcur.tm_mon+1,tmcur.tm_mday); Strncat (osdstr,temp,3); }
To achieve a high precision timing function, you will use the Setitimer function.
int Setitimer (int which, const struct itimerval *value, struct itimerval *ovalue);The which is a timer type and the Setitimer supports 3 types of timers:
itimer_real: It takes the real time of the system to calculate, it sends out the SIGALRM signal.
itimer_virtual:-Calculates the time spent by the process in the user state, and it sends out the SIGVTALRM signal.
itimer_prof: It sends out the SIGPROF signal with the time that the process takes in both the user state and the kernel state. Setitimer () the first parameter which specifies the timer type (one of the three above); The second parameter is an instance of the structure Itimerval; The third parameter is not processed. the Setitimer () call returns 0 successfully, otherwise returns-1.
struct Itimerval {struct timeval it_interval;struct timeval it_value;}; struct Timeval {long tv_sec;long tv_usec;};
Need signal.h and Sys/time.h
IT_INTERVAL Specifies the interval time, it_value Specifies the initial timing time. If you specify only It_value, you implement one time, and if you specify it_interval at the same time, the system re-initializes the It_value to It_interval, implements the recurrence, and clears the timer if both are cleared. TV_SEC provides second-level accuracy, TV_USEC provides microsecond accuracy with a large value first, and note 1s = 1000000us. The ovalue is used to hold the previous value, and the permanent is null.
If you do not need this high precision, you can use the alarm () function:
Alarm Introduction
Alarm is also called the alarm function ,alarm () is used to set the signal SIGALRM to the current process after the number of seconds specified by the parameter seconds . If the parameter seconds is 0, the previously set alarm is canceled and the remaining time is returned. Note that a process can have only one alarm time, and if the alarm time has been set before calling alarm, any previous alarm time is replaced by the new value .
Required header File
#include <unistd.h>
Function prototypes
unsigned int alarm (unsigned int seconds)
function parameters
Seconds: Specify the number of seconds
function return value
Success: If the process has set an alarm time before calling this alarm (), the time remaining for the previous alarm time is returned, otherwise 0 is returned.
Error:-1