Linux signal Practice (5)--Time and timer

Source: Internet
Author: User
Tags signal handler usleep
three different kinds of sleep accuracy

1.sleep

#include <unistd.h>unsigned int sleep (unsigned int seconds);

RETURN VALUE

Zero If the requested time has a elapsed, or the number of seconds left to sleep,

If the call is interrupted by a signal handler.

Example int sleeptime = 5;do{    sleeptime = Sleep (sleeptime);} while (Sleeptime > 0);

2.usleep (in microseconds)

int Usleep (useconds_t usec);

The type useconds_t is a unsigned integer type capable of holding integers in the range [0,1000000].

Programs'll is more portable if they never mention this type explicitly.

3.nanosleep (in nanoseconds)

#include <time.h>int nanosleep (const struct TIMESPEC *req, struct timespec *rem);

REQ Specifies the time of sleep, REM returns the remaining sleep time

struct timespec{    time_t tv_sec;        /* Seconds: SEC */    long   tv_nsec;       /* nanoseconds: nanoseconds */};

Three time Structures
time_t
struct Timeval {long    tv_sec;         /* seconds */long    tv_usec;        /* microseconds microseconds */};
struct Timespec {time_t tv_sec;        /* seconds */long   tv_nsec;       /* nanoseconds */};

Setitimer
#include <sys/time.h>int setitimer (int which, const struct itimerval *value, struct itimerval *ovalue));

Setitimer () is powerful than alarm and supports 3 types of timers

Parameters

The first parameter which specifies the type of timer

The second parameter is the requested time

The third parameter is the value that the timer originally associated

struct itimerval{    struct timeval it_interval;/* Next value: The interval between the time the signal is generated */    struct timeval it_value;    /* Current value: The first time the signal is generated */};struct timeval{    time_t      tv_sec;         /* Seconds: SEC */    suseconds_t tv_usec;        /* Microseconds: microseconds */};

which value

itimer_real: After a specified amount of time, the kernel sends a SIGALRM signal to the process (with more)

itimer_virtual : After the program executes the specified time in the user space, the kernel sends a SIGVTALRM signal to the process

itimer_prof : When the process executes in kernel space, the time count is reduced, usually shared with itimer_virtual, and the kernel sends a SIGPROF signal to the process after the specified time has elapsed between user space and kernel space.

/** example 1:1. Generates a signal after 5 seconds of starting the process 2. Then generate a signal every 1 seconds **/int main () {    if (signal (SIGALRM, signalaction) = = Sig_err)        err_exit ("Signal error");    struct Itimerval it;    struct Timeval it_interval = {1, 0};    struct Timeval It_value = {5, 0};    It.it_interval = It_interval;    It.it_value = It_value;    if (Setitimer (Itimer_real, &it, NULL) = =-1)        err_exit ("Setitimer error");    while (true)        pause ();}
/** Example 2: Get the Itimerval struct **/int main () {    struct itimerval it;    struct Timeval it_interval = {1, 0};    struct Timeval It_value = {5, 0};    It.it_interval = It_interval;    It.it_value = It_value;    if (Setitimer (Itimer_real, &it, NULL) = =-1)        err_exit ("Setitimer error");    for (int i = 0; i < 100000; ++i)        ;    struct Itimerval oldit;//    if (Setitimer (Itimer_real, &it, &oldit) = =-1)//        err_exit ("Setitimer error ");    Get the remaining time without re-setting the clock    if (Getitimer (itimer_real, &oldit) = =-1)        err_exit ("Getitimer error");    cout << oldIt.it_interval.tv_sec << ' << oldIt.it_interval.tv_usec         << ' << oldit.i T_value.tv_sec << ' << oldIt.it_value.tv_usec << Endl;}

Attached: seconds-microseconds-conversion of nanoseconds

s (seconds), MS (MS), μs (microseconds), NS (nanoseconds), Where: 1s=1000ms,1 ms=1000μs,1μs=1000ns

Linux signal Practice (5)--Time and timer

Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

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.