I. Three time Structures
Time_t: // seconds
Struct timeval {
Long TV _sec;/* seconds */
Long TV _usec;/* microseconds */
};
Struct timespec {
Time_t TV _sec;/* seconds */
Long TV _nsec;/* nanoseconds */
};
Ii. setitimer ()
In the current system, many programs no longer use alarm to call, but use setitimer to set the timer and getitimer to get the timer status,
The declaration formats of these two calls are as follows:
# Include <sys/time. h>
Int getitimer (int which, struct itimerval * curr_value );
Int setitimer (int which, const struct itimerval * new_value, struct itimerval * old_value );
Parameters:
- The first parameter which specifies the timer type
- The second parameter is an instance of the structure itimerval, in the form of the structure itimerval
- The third parameter cannot be processed.
Returned value: 0 is returned successfully.-1 is returned If no result is returned.
The system provides three timers for processes. Each of them has its own timing domain. When any one of them arrives, a corresponding signal is sent to the process, and make the timer start again. The three timers are specified by the which parameter as follows:
TIMER_REAL: Timing by actual time. When the time arrives, the SIGALRM signal is sent to the process.
ITIMER_VIRTUAL: Timing is performed only when the process is executed. When the timer arrives, the SIGVTALRM signal is sent to the process.
ITIMER_PROF: timing when the process is executed and when the system executes the action for the process. It is a pair with the ITIMER_VIR-TUAL, which is often used to count the time spent by the process in the user and kernel states. When the timer arrives, the SIGPROF signal is sent to the process.
The parameter value in the timer is used to specify the timer time. Its structure is as follows:
Struct itimerval {
Struct timeval it_interval;/* interval after the first time */
Struct timeval it_value;/* how long it takes for the first call */
};
The timeval structure in this structure is defined as follows:
Struct timeval {
Long TV _sec;/* seconds */
Long TV _usec;/* microseconds, 1 second = 1000000 microseconds */
};
In setitimer calls, if the parameter ovalue is not empty, the value set in the last call is retained. When the timer degrades it_value to 0, a signal is generated, and the it_value value is set to the it_interval value, and then the timing starts again. When it_value is set to 0, the timer stops, or when it expires and it_interval is set to 0. If the call is successful, 0 is returned. If an error is returned,-1 is returned, and the corresponding error code errno is set:
EFAULT: The parameter value or ovalue is an invalid pointer.
EINVAL: The which parameter is not one of ITIMER_REAL, ITIMER_VIRT, or ITIMER_PROF.
Example 1:
#include <unistd.h><sys/stat.h><sys/wait.h><sys/types.h><fcntl.h><stdlib.h><stdio.h><errno.h><.h><signal.h><sys/time.h> ERR_EXIT(m) \ ( handler( main( argc, * (signal(SIGALRM, handler) == timeval tv_interval = {, timeval tv_value = {, ==&
Result:
<Sys/stat. h> <sys/wait. h> <sys/types. h> <fcntl. h> <stdlib. h> <stdio. h> <errno. h> <. h> <signal. h> <sys/time. h> ERR_EXIT (m) \ (main (argc, * timeval TV _interval = {, timeval TV _value = {, ==& (I =; I <; I ++ & it, & oit); printf (, () oit. it_interval. TV _sec, () oit. it_interval. TV _usec, () oit. it_value. TV _sec ,(
Result:
The first method is used:
<Unistd. h> <stdio. h> <sys/time. h> sigroutine (=========== &
Result:
We can see that SIGVTALRM is not SIGALRM twice.