Linux accurate time sequence (sleep, usleep, nanosleep)

Source: Internet
Author: User
Tags usleep
ArticleDirectory
    • Latency using I/O Ports
    • Latency using a combination of languages
Linux accurate time sequence (sleep, usleep, nanosleep)

11:36:48| Category:Linux| Tag:None |Font Size Subscription

First of all, I will say that it is not guaranteed that the process you run in user-mode can accurately control the time sequence because Linux is a multi-job environment. the progress you are performing will be paused for about 10 ms to several seconds at any time for various reasons (when the system load is very high ). however, for most applications that use I/O Ports, the latency is actually nothing. to shorten the delay time, you must use the function nice to set your progress in execution to a high priority (referNice (2)Or use the real-time scheduling (see the following ).

If you want to obtain a time sequence that is more accurate than the process in the user-mode, there are some ways for you) to support the 'real' schedule. in Linux 2.x, real-time scheduling is supported by software at the core. For more information, seeSched_setscheduler (2)Instructions for use. There is a special core support hardware real-time schedule; For details, please refer to the Web http://luz.cs.nmt.edu /~ RTLinux/

Sleeping ): Sleep () And Usleep ()

Now, let's start a simple time series call. The best way to delay a few seconds is probably to use the function.Sleep (). To delay at least dozens of milliseconds (10 MS seems to be the shortest delay ),Usleep ()It should be usable. these functions allow the CPU to be used for other processes (''take a rest ''), so there is no waste of CPU time. for details, seeSleep (3)AndUsleep (3).

If the CPU usage is granted, the time delay is about 50 milliseconds (depending on the speed of the processor and machine, and the system load), too much CPU time is wasted, because the Linux scheduler (sched) usually takes at least 10-30 milliseconds before sending control to your process. therefore, for short latency, use the FunctionUsleep (3)The latency result is usually greater than the value specified in the parameter, which is about 10 ms.

Nanosleep ()

In a series of Linux 2.0.x core releases, there is a new system call ),Nanosleep ()(SeeNanosleep (2)It allows you to rest or delay a short period of time (several microseconds or more ).

If the delay time <= 2 MS, if (and only if) the trip you are executing (process) sets the real-time schedule of the software (that is, using the function TT/sched_setscheduler () /), call FunctionNanosleep ()It does not use a busy loop to delay the time; it will be like a function.Usleep ()Let the CPU usage rest.

This busy loop uses the FunctionUdelay ()(A driver usually uses the core internal function) to achieve this, and uses the bogomips value (bogomips can accurately measure the speed of this busy loop) to calculate the length of the loop latency. for details about how to perform the operation, see/Usr/include/ASM/delay. h).

Latency using I/O Ports

Another method of delay in several microseconds is to use the I/O port. it is to input or output any byte data from the port address 0x80 (refer to the previous section). The waiting time should be almost 1 microsecond, depending on the type and speed of your processor. if you want to delay several microseconds, you can do this operation several times. output Data to this port address on any standard machine should not be defective (and some core device drivers are also using it ).{In | out} [BW] _ p ()This method is used to produce a time delay (refer to the fileASM/IO. h).

In fact, an I/O PORT command with the IP address □circumference 0-0x3ff takes almost 1 microsecond, so if you want to do this, for example, directly use the parallel port, just add a fewINB ()Read byte data from this port address.

Latency using a combination of languages

If you know the processor type and clock speed of the machine where the execution program is located, You can execute some combined language commands to get a short delay (but remember, you are in the process) will be paused at any time, so sometimes the delay will be longer than the actual time ). as shown in the following table, the speed of the internal processor determines the number of clock cycles to use. For example, a 50 MHz processor (486dx-50 or 486dx2-50 ), A clock cycle takes 1/50000000 seconds (= 200 Nai seconds ).

Instruction i386 Number of clock periods i486 Number of clock periods
NOP 3 1
Xchg % ax, % ax 3 3
Or % ax, % ax 2 1
MoV % ax, % ax 2 1
Add % ax, 0 2 1

(Sorry, I don't know about pentiums. Maybe it's close to IMG. I cannot find instructions that only take one clock cycle on i386. if you can use a command that takes a clock cycle, or you can use a new processor of the pipeline technology to shorten the time .)

Commands in the preceding tableNOPAndXchgThere should be no bad results. The command may change the content of the Flag temporarily, but it does not matter because GCC will process the. Command.NOPIs a good choice.

To use these commands in your program, you must useASM ("Instruction ")The command syntax is like the usage in the above table; if you wantASM ()Multiple commands can be used in the description to separate them using semicolons. For example,ASM ("NOP; NOP ")FourNOPCommand, which delays four clock cycles in the i486 or Pentium processor (or the i386 delays 12 clock cycles ).

GCC willASM ()It is translated into single-line combined language code, so there is no call function load.

In intel X86 architecture, there cannot be a shorter latency than a clock cycle.

Use a function on the pentiums Processor Rdtsc

For the pentiums processor, you can use the following C-language code to obtain the number of clock cycles that have elapsed since the last reboot:

Extern _ inline _ unsigned long int rdtsc ()
{
Unsigned long int X;
_ ASM _ volatile (". byte 0x0f, 0x31": "= A" (x ));
Return X;
}

You can refer to this value to delay the number of clock cycles you want.

If you want the time to be accurate to one second, use the FunctionTime ()Maybe it's the simplest method. If you want time to be more accurate, the functionGettimeofday ()It can be precise to microseconds (but will be affected by CPU schedules as described earlier). To the pentiums processor, the above code snippet can be precise to a clock cycle.

If you want a process to be notified after a period of time, you have to use the FunctionSetitimer ()OrAlarm ()For more information, see the description file.

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.