在C中暂停/等待 [英] Pausing/Waiting in C

查看:76
本文介绍了在C中暂停/等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在用C开发一个应用程序;基本上是一个链接列表,有一系列的事件系列。弹出,由命令分隔暂停读取

列表中的下一个事件。自从我上次做C以来已经过了一段时间,并且

这是第一个K& R版本!是否有命令暂停应用程序

a一段时间,因为我熟悉的所有命令都指定暂停

整数秒,以及我想要什么是
秒的分数,如果可能的话,最好是几毫秒


TIA


Paul


-

----

主页: http://www.paullee.com

困境: http://www.dr_paul_lee.btinternet.co.uk/zzq.shtml

Hi everyone,
I''m developing an application in C; basically a linked list, with a series
of "events" to be popped off, separated by a command to pause reading off
the next event in the list. It has been sometime since I last did C, and
that was the first K&R version! Is there a command to pause an app for
a period of time, as all the commands I am familiar with specify pauses
for integer numbers of seconds, and what I would like is fractions of a
second, preferably milliseconds if possible

TIA

Paul

--
----
Home: http://www.paullee.com
Woes: http://www.dr_paul_lee.btinternet.co.uk/zzq.shtml

推荐答案

Kwebway Konongo< pa ** @ pNaOuSlPlAeMe.comwrites:
Kwebway Konongo <pa**@pNaOuSlPlAeMe.comwrites:

我正在用C开发一个应用程序;基本上是一个链接列表,有一系列的事件系列。弹出,由命令分隔暂停读取

列表中的下一个事件。自从我上次做C以来已经过了一段时间,并且

这是第一个K& R版本!是否有命令暂停应用程序

a一段时间,因为我熟悉的所有命令都指定暂停

整数秒,以及我想要什么是
秒的分数,如果可能的话,最好是几毫秒
I''m developing an application in C; basically a linked list, with a series
of "events" to be popped off, separated by a command to pause reading off
the next event in the list. It has been sometime since I last did C, and
that was the first K&R version! Is there a command to pause an app for
a period of time, as all the commands I am familiar with specify pauses
for integer numbers of seconds, and what I would like is fractions of a
second, preferably milliseconds if possible



没有好的便携方法可以做到这一点。


(clock()函数返回你的程序消耗的CPU时间量
的指示。你可能想写一个循环

执行,直到clock()函数的结果达到某个

值。抵制这种诱惑。虽然它只使用标准的C />
特征,但它至少有两个主要的缺点:它测量CPU时间,

不是挂钟时间,而这种繁忙的循环会导致你的程序浪费CPU时间,可能会影响系统上的其他程序。 )


但是,大多数操作系统都会提供一个很好的方法。

在一个问题中新闻组,这是特定于你正在使用的任何操作系统,例如comp.unix.programmer或comp.os.ms-windows.programmer.win32的
- 但是

看看你是否能在新闻组的FAQ中找到答案。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *< http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。

There is no good portable way to do this.

(The clock() function returns an indication of the amount of CPU time
your program has consumed. You might be tempted to write a loop that
executes until the result of the clock() function reaches a certain
value. Resist this temptation. Though it uses only standard C
features, it has at least two major drawbacks: it measures CPU time,
not wall clock time, and this kind of busy loop causes your program to
waste CPU time, possibly affecting other programs on the system.)

However, most operating systems will provide a good way to do this.
Ask in a newsgroup that''s specific to whatever OS you''re using, such
as comp.unix.programmer or comp.os.ms-windows.programmer.win32 -- but
see if you can find an answer in the newsgroup''s FAQ first.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


Kwebway Konongo写道:
Kwebway Konongo wrote:

大家好,

我正在用C开发一个应用程序;基本上是一个链接列表,有一系列的事件系列。弹出,由命令分隔暂停读取

列表中的下一个事件。自从我上次做C以来已经过了一段时间,并且

这是第一个K& R版本!是否有命令暂停应用程序

a一段时间,因为我熟悉的所有命令都指定暂停

整数秒,以及我想要什么是
秒的分数,如果可能的话,最好是几毫秒


TIA


Paul
Hi everyone,
I''m developing an application in C; basically a linked list, with a series
of "events" to be popped off, separated by a command to pause reading off
the next event in the list. It has been sometime since I last did C, and
that was the first K&R version! Is there a command to pause an app for
a period of time, as all the commands I am familiar with specify pauses
for integer numbers of seconds, and what I would like is fractions of a
second, preferably milliseconds if possible

TIA

Paul



也许你可以使用类似sleep()的东西。

Maybe you can use something like sleep().


Kwebway Konongo写道:
Kwebway Konongo wrote:

大家好,

我正在用C开发一个应用程序;基本上是一个链接列表,有一系列的事件系列。弹出,由命令分隔暂停读取

列表中的下一个事件。自从我上次做C以来已经过了一段时间,并且

这是第一个K& R版本!是否有命令暂停应用程序

a一段时间,因为我熟悉的所有命令都指定暂停

整数秒,以及我想要什么是
秒的分数,如果可能的话,最好是几毫秒
Hi everyone,
I''m developing an application in C; basically a linked list, with a series
of "events" to be popped off, separated by a command to pause reading off
the next event in the list. It has been sometime since I last did C, and
that was the first K&R version! Is there a command to pause an app for
a period of time, as all the commands I am familiar with specify pauses
for integer numbers of seconds, and what I would like is fractions of a
second, preferably milliseconds if possible


>来自C-FAQ:
>From the C-FAQ:



19.37:如何实现延迟,或者用户的响应时间,

sub-

秒分辨率?


答:不幸的是,没有可移植的方式。 V7 Unix,以及派生的

系统,提供了一个非常有用的ftime()函数,其分辨率为

,但是它已经从
$ b $中消失了b System V和POSIX。您可能在

系统上寻找的其他例程包括clock(),delay(),gettimeofday(),msleep(),

nap(),napms(), nanosleep(),setitimer(),sleep(),times()和

usleep()。 (但是,一个名为wait()的函数至少在

Unix *中*不是你想要的。)select()和poll()调用(如果

可以投入使用以实现简单的

延迟。在MS-DOS机器上,可以重新编程

系统定时器和定时器中断。


其中只有clock()是ANSI的一部分标准。如果CLOCKS_PER_SEC

大于时间,则两次调用clock()之间的
差异会导致执行时间过长,甚至可能具有亚秒级分辨率。 1.但是,clock()给出了当前程序使用的经过处理器时间

,这在多任务系统上可能与实时有很大差异。


如果您正在尝试实施延迟并且所有可用的时间

是一个时间报告功能,您可以实施CPU密集型

busy-wait,但这只是单用户,单一$ b / b
任务机器的一个选项,因为它对任何其他

进程非常反社会。在多任务操作系统下,一定要使用一个让你的进程在一段时间内处于睡眠状态的呼叫,

,例如sleep()或select(),或者暂停()与

alarm()或setitimer()一起使用。


对于真正短暂的延迟,使用无操作循环很有吸引力

喜欢


long int i;

for(i = 0; i< 1000000; i ++)

;


但是如果可能的话,抵制这种诱惑!首先,

你的精心计算的延迟循环将在下个月更快的处理器出现时停止正常工作

。或许更糟糕的是,一个聪明的编译器可能会注意到循环没有做任何事情和

完全优化它。


参考文献:H& ; S Sec。 18.1页,第398-9页; PCS Sec。 12页.197-8,215-

6; POSIX秒4.5.2。

19.37: How can I implement a delay, or time a user''s response, with
sub-
second resolution?

A: Unfortunately, there is no portable way. V7 Unix, and derived
systems, provided a fairly useful ftime() function with
resolution up to a millisecond, but it has disappeared from
System V and POSIX. Other routines you might look for on your
system include clock(), delay(), gettimeofday(), msleep(),
nap(), napms(), nanosleep(), setitimer(), sleep(), times(), and
usleep(). (A function called wait(), however, is at least under
Unix *not* what you want.) The select() and poll() calls (if
available) can be pressed into service to implement simple
delays. On MS-DOS machines, it is possible to reprogram the
system timer and timer interrupts.

Of these, only clock() is part of the ANSI Standard. The
difference between two calls to clock() gives elapsed execution
time, and may even have subsecond resolution, if CLOCKS_PER_SEC
is greater than 1. However, clock() gives elapsed processor time
used by the current program, which on a multitasking system may
differ considerably from real time.

If you''re trying to implement a delay and all you have available
is a time-reporting function, you can implement a CPU-intensive
busy-wait, but this is only an option on a single-user, single-
tasking machine as it is terribly antisocial to any other
processes. Under a multitasking operating system, be sure to
use a call which puts your process to sleep for the duration,
such as sleep() or select(), or pause() in conjunction with
alarm() or setitimer().

For really brief delays, it''s tempting to use a do-nothing loop
like

long int i;
for(i = 0; i < 1000000; i++)
;

but resist this temptation if at all possible! For one thing,
your carefully-calculated delay loops will stop working properly
next month when a faster processor comes out. Perhaps worse, a
clever compiler may notice that the loop does nothing and
optimize it away completely.

References: H&S Sec. 18.1 pp. 398-9; PCS Sec. 12 pp. 197-8,215-
6; POSIX Sec. 4.5.2.


这篇关于在C中暂停/等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆