1.1. Sleep function
There are two sleep functions under Linux, and the prototypes are:
#include <unistd.h>
unsigned int sleep (unsigned int seconds);
void Usleep (unsigned long usec);
The function sleep allows the process to seconds seconds, and the function usleep let the process sleep usec microseconds.
Sleep sleep functions are internally processed using a signaling mechanism, and the functions used are:
#include <unistd.h>
unsigned int alarm (unsigned int seconds);
Inform yourself of the process, to automatically generate a sigalrm signal after seconds seconds
int pause (void); Suspends the process itself until a signal occurs, returning from pause
Example: Simulated sleep 3 seconds:
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
void Signhandler (int isignno)
{
printf ("signal:%d\n", Isignno);
}
int main ()
{
Signal (Sigalrm,signhandler);
Alarm (3); Wait 3 seconds to automatically generate SIGALRM signal
printf ("before pause () \ n");
Pause (); Suspends the process until a signal occurs before exiting the pending state
printf ("after pause () \ n");
return 0;
}
Note: Because sleep is implemented internally with alarm, it is best not to mix sleep and alarm in the program, so as not to cause confusion.
1.2. Clock processing
Linux maintains 3 timers for each process, namely real timers, virtual timers, and utility timers.
L Real timers Calculate the actual time the program is running;
The virtual timer calculates the time it takes for the program to run in the user state (the time it takes to lose the actual time (system calls and program sleep);
The utility timer calculates the sum of the time consumed by the program in the user state and in the kernel state.
For example: There is a program running, in the user state run for 5 seconds, in the kernel state to run for 6 seconds, also sleep for 7 seconds, the real calculator calculates the result is 18 seconds, the virtual timer calculates 5 seconds, the utility timer calculates 11 seconds.
When the good one timer is set for the process with the specified initial and repeating intervals, the timer sends a clock signal to the process on a timed basis. The clock signals sent by the 3 timers are: Sigalrm,sigvtalrm and sigprof, respectively.
Functions and data structures used:
#include <sys/time.h>
1. int Getitimer (int which, struct itimerval *value); Get the settings for a timer
parameter which specifies which timer, optional itimer_real (real timer), itimer_virtual (virtual timer, itimer_prof (utility timer))
Parameter value is an outgoing parameter of a struct that is used to outgoing the timer's initial and repeating intervals
Return value: If successful, returns 0, otherwise-1
2. int Setitimer (int which, const struct itimerval *value, struct itimerval *ovalue); Setting timers
parameter which specifies which timer, optional itimer_real (real timer), itimer_vitual (virtual timer, itimer_prof (utility timer))
Parameter value is an incoming parameter of a struct, specifying the initial and repeating intervals for the timer
The parameter ovalue is a struct-body outgoing parameter that is used to pass out the previous timer time setting.
Return value: If successful, returns 0, otherwise-1
struct Itimerval {
struct Timeval it_interval; /* Next value *///repeat Interval
struct Timeval it_value; /* Current Value *///initial interval
};
struct Timeval {
Long tv_sec; /* seconds *//time of the second part
Long tv_usec; /* microseconds *//microsecond portion of Time
};
Example: Enable clock processing for real timers (get the current system time and update once a second)
#include <signal.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
void Sighandler (int isignum)
{
time_t tt;
Time (&TT);
struct TM *ptm = gmtime (&TT);
printf ("%04d-%02d-%02d%02d:%02d:%02d\n", (1900+ptm->tm_year), (1+ptm->tm_mon), Ptm->tm_mday, (8+pTm-> Tm_hour), Ptm->tm_min, ptm->tm_sec);
}
void Inittime (int tv_sec, int tv_usec)
{
Signal (SIGALRM, Sighandler);
struct Itimerval TM;
Tm.it_value.tv_sec = tv_sec;
Tm.it_value.tv_usec = tv_usec;
Tm.it_interval.tv_sec = tv_sec;
Tm.it_interval.tv_usec = tv_usec;
if (Setitimer (Itimer_real, &tm, NULL) = =-1)
{
Perror ("Setitimer error");
Exit (-1);
}
}
int main ()
{
Inittime (1, 0);
while (1)
;
return 0;
}
Timers and Signals