Linux accurate time sequence (sleep, usleep, nanosleep)

Source: Internet
Author: User
Tags usleep
Document directory
  • Extend the time using the I/O port
  • Extend the time using the combined statement
Http://www.linux.org.tw/CLDP/MiniHOWTO/prog/IO-Port-Programming/IO-Port-Programming-4.html4. High-Precision chronological order 4.1 extended time

First of all, I will say that you do not guarantee the process of your trip in user-mode) the ability to precisely control the time sequence is because Linux is a multi-industry environment. the duration of your trip in the primary line will be suspended for various reasons for about 10 ms to several seconds (when the system load is very high ). however, for most of the applications using I/O Ports, this delay is actually not much. to reduce the delay, you need to use the function nice to set your itinerary (process) in your workshop to a master privilege.nice(2)Or use the real-time scheduling (see the following ).

If you want to optimize the time sequence of the trip (process) in user-mode, there are some ways for you to support the "instant" schedule in user-mode. the kernel of Linux 2.x includes instant schedule support in the manual mode. For more information about manual release, please refersched_setscheduler(2)Using the declarative document. There is a special key to support the hardware's out-of-the-box schedule; please refer to your website's http://luz.cs.nmt.edu for written information /~ RTLinux/

Sleeping ):sleep()Andusleep()

Now, let's start writing orders in chronological form. The best way to extend the number of seconds is to use the form.sleep()To delay at least dozens of milliseconds (10 MS seems to be the shortest delay ),usleep()It should be usable. these functions allow CPU usage to be granted to other processes (''let yourself rest '') that you want to perform, so there is no waste of CPU time. please submit for further examinationsleep(3)Andusleep(3)Description file.

If the CPU usage permission is granted, the time is extended by about 50 milliseconds (This determines the speed of the processor and the machine, and the system load ), it will waste too much CPU time, because the Linux scheduler (sched) (For X86 architecture alone) it usually takes at least 10-30 milliseconds before the process is returned. therefore, the function is used for short-term delay.usleep(3)The result of the delay is usually greater than the value you specified in the number, which is at least 10 ms.

nanosleep()

In the Linux 2.0.x series of core releases, there is a new system call ),nanosleep()(Please refernanosleep(2)He allows you to take a break or extend a short period of time (several microseconds or more ).

If the extended time <= 2 MS, if (and only if) the trip in your current row (process) set the instant schedule of the receiver (that is, call the form using the correspondence TT/sched_setscheduler ()/).nanosleep()The time is not extended by using a busy cycle; it is like a function.usleep()In this way, the CPU usage right is rested.

Function used in this busy cycleudelay()(The core internal function that is often used by an animation) to compile and use the bogomips value (bogomips can be used to determine the speed of such busy circles) it is the time duration of the cycle extension. please refer to the questions on how to initiate the manual examination./usr/include/asm/delay.h).

Extend the time using the I/O port

Another method to delay data in microseconds is to use the I/O port. is to input or export any byte information from port address 0x80 (please refer to the previous test) the waiting time should be about 1 microsecond, depending on the type and speed of your processor. if you want to extend the time to several microseconds, you can perform the operation multiple times. sending out information on any target machine should not result in poor port addresses (and some core device drivers are also using it) ).{in|out}[bw]_p()This method is used to produce time delays.asm/io.h).

In practice, an I/O PORT command using the port address must be 1 microsecond, so if you want to do so, for example, directly use and column ports, as long as there are severalinb()Write a response to the byte information from the local address.

Extend the time using the combined statement

If you know the processor type and time interval speed of the host where the travel style is located, you can merge some group statement commands to get a short delay (but remember, you are in the process) it will be stopped at some time, so the time for delay will be longer than the actual time ). as shown in the following table, the speed of the internal processor determines the time period data to be used. For example, a 50 MHz processor (486dx-50 or 486dx2-50 ), A time period takes 1/50000000 seconds (= 200 nets ).

Instruction i386 hour period (IMG)
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 the resources of pentiums, maybe it's close to IMG. I can't find the instruction that takes only one hour on the i386 resource. if you can renew the service, use the commands that take a certain period of time. Otherwise, you can use the new processor of render technology to renew the service for a short time .)

Commands in the preceding tablenopAndxchgThere should be no bad results. The instruction may change the content of the Flag memory at the end, but this does not matter because GCC will handle the instruction.nopIs a good choice.

To use these commands in your program, you must useasm("instruction")The explain method of the. Command is like the usage in the above table; if you wantasm()Multiple commands can be used in the statement to separate them. For example,asm("nop ; nop ; nop ; nop")Four rows will be mergednopCommand, which will be extended by four time periods in the iron or Pentium processor (or the i386 will be extended by 12 time periods ).

GCC willasm()It can be converted into a single line to combine the statement type, so there will be no call-type upload load.

In the intel X86 architecture, it is impossible to extend the period from one time period to another.

Use a function on the pentiums Processorrdtsc

For the pentiums processor, you can use the following C program to obtain the number of time periods that have elapsed since the last restart:

   extern __inline__ unsigned long long int rdtsc()
   {
     unsigned long long int x;
     __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
     return x;
   }

You can check this value to extend the time period you want.

4.2 hours

If you want time to be accurate to one second, use the Functiontime()Maybe it is the simplest method. If you want more accurate time, the functiongettimeofday()Maxcompute can be accurate to microseconds (but as described above, it will be affected by the CPU schedule ). as for the pentiums processor, you can use the previous program to confirm a time period.

If you want the process in your primary row to be notified after a period of time, you need to use the Functionsetitimer()Oralarm(). Please refer to the usage instructions document in the Examination Form.

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.