锐英源软件
第一信赖

精通

英语

开源

擅长

开发

培训

胸怀四海 

第一信赖

当前位置:锐英源 / 英语翻译 / Unix和Linux平台暂停程序执行函数
服务方向
人工智能数据处理
人工智能培训
kaldi数据准备
小语种语音识别
语音识别标注
语音识别系统
语音识别转文字
kaldi开发技术服务
软件开发
运动控制卡上位机
机械加工软件
软件开发培训
Java 安卓移动开发
VC++
C#软件
汇编和破解
驱动开发
联系方式
固话:0371-63888850
手机:138-0381-0136
Q Q:396806883
微信:ryysoft

锐英源精品开源心得,转载请注明:“锐英源www.wisestudy.cn,孙老师作品,电话13803810136。需要全文内容也请联系孙老师。

Unix和Linux平台暂停程序执行函数

Occasionally you want to suspend process execution for a short period of time. For example, the tail program with the -f flag waits until a file has grown longer, so it needs to relinquish the processor for a second or two between checks on the file status.

有时候你需要短时间内暂停程序执行函数。例如,结尾程序- fb标志等到文件变得更长,所以它需要放弃处理器一两秒之间来检查文件状态。

Typically, this is done with sleep. However, some applications need to specify the length of time more accurately than sleep allows, so a couple of alternatives have arisen: nap suspends execution for a number of milliseconds, and usleep suspends it for a number of microseconds.
通常情况下,可以使用sleep这样做。然而,一些应用程序需要指定比sleep允许的更准确地时间长度,所以出现几个备选方案:nap暂停执行若干毫秒,和usleep暂停数微秒。

nap

nap is a XENIX variant of sleep with finer resolution: nap是一个sleep的XENIX变量,具备更好的分辨率:

#include <time.h>
   long nap (long millisecs);

nap suspends process execution for at least millisecs milliseconds. In practice, the XENIX clock counts in intervals of 20 ms, so this is the maximum accuracy with which you can specify millisecs.

nap暂停程序执行函数至少要几毫秒。在实践中,XENIX时钟计数间隔20毫秒,这是您可以指定的最高精度的毫秒数。

setitimer

BSD systems and derivatives maintain three (possibly four) interval timers: BSD系统和派生维持三个间隔计时器(可能四个):

• Areal time timer, ITIMER_REAL, which keeps track of real elapsed time. 区域时间计时器,ITIMER_REAL跟踪实际运行时间。

• Avirtual timer, ITIMER_VIRTUAL, which keeps track of process execution time, in other words the amount of CPU time that the process has used. 虚拟计时器,ITIMER_VIRTUAL,该跟踪处理的执行时间,换言之,该过程使用了的CPU时间量。

• Aprofiler timer, ITIMER_PROF, which keeps track of both process execution time and time spent in the kernel on behalf of the process. As the name suggests, it is used to implement profiling tools. Aprofiler计时器,ITIMER_PROF跟踪程序执行时间和在内核中代表的时间过程。顾名思义,它是用来实现分析工具。

• Areal time profiler timer, ITIMER_REALPROF, used for profiling Solaris 2.X multithreaded processes. 实时探查计时器,ITIMER_REAL,用于分析的Solaris2.X多线程程序。

These timers are manipulated with the system calls getitimer and setitimer:这些时间与系统操作调用setitimer函数和setitimer函数:

#include <sys/time.h>
   struct timeval
   {
   long tv_sec; /* seconds */
   long tv_usec; /* and microseconds */
    };
   struct itimerval
   {
   struct timeval it_interval; /* timer interval */
   struct timeval it_value; /* current value */
   };
   int getitimer (int which, struct itimerval *value);
   int setitimer (int which, struct itimerval *value, struct itimerval *ovalue);

sleep

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

The library routine sleep suspends program execution for approximately seconds seconds. It is available on all UNIX platforms.

库函数sleep暂停程序执行函数大约几秒钟。它是适用于所有UNIX平台。

usleep

usleep is a variant of sleep that suspends program execution for a very short time: usleep是sleep的变形,很短的时间内暂停程序执行函数:

#include <unistd.h>
   void usleep (u_int microseconds);

usleep sleeps for at least microseconds microseconds. It is supplied on BSD and System V.4 systems as a library function that uses the setitimer system call.

usleep暂停至少几个微秒。它在BSD和系统V.4系统中,使用setitimer系统调用的库函数。

select and poll

If your system doesn’t supply any timing function with a resolution of less than one second, you might be able to fake it with the functions select or poll. select can wait for nothing if you ask it to, and since the timeout is specified as a struct timeval, you can specify times down to microsecond accuracy. You can use poll in the same way, except that you specifies its timeout value in milliseconds.

如果您的系统不提供任何分辨率不到一秒的时间函数,你可以伪造函数select和poll。如果你访问select可以等待,因为超时被指定为一个日期格式。你可以指定时间到微秒级精度。您可以使用同样的方式调查,除了你指定的超时值,以毫秒为单位。

For example,例如:

void usleep (int microseconds) { struct timeval timeout; timeout.tv_usec = microseconds % 1000000; timeout.tv_sec = microseconds / 1000000; select (0, NULL, NULL, NULL, &timeout); } or void usleep (int microseconds) { poll (0, NULL, microseconds / 1000); }

友情链接
版权所有 Copyright(c)2004-2021 锐英源软件
公司注册号:410105000449586 豫ICP备08007559号 最佳分辨率 1024*768
地址:郑州大学北校区院(文化路97号院)内