睡眠()延迟输出,直到结束 [英] sleep() delays output until end

查看:112
本文介绍了睡眠()延迟输出,直到结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-string\">Why除非换行符是格式字符串的printf确实没有呼叫后冲洗? (C语言)

我使用C中的睡眠()函数,和我遇到了一个问题:我不知道,这是这个问题,所以我煮了整个code到这一点:

I'm using the sleep() function in C, and am running into a problem: I wasn't sure that this was the problem so I boiled the entire code down to this:

int main() {

  printf("1");
  sleep(3);
  printf("2");

  return 0;
}

我想这应该是产生1 ..等待3秒。2.相反,程序等待3秒钟,然后打印12.是否有什么办法可以使用​​睡眠功能,使我得到的第一个输出?

What I thought this should produce is 1 .. wait for 3 seconds .. 2. Instead the program waits for 3 seconds and then prints 12. Is there any way to use the sleep function so that I get the first output?

感谢

推荐答案

这不是实际上是延迟输出的休眠功能,它的标准输出流的缓冲性质。输出 2 几乎肯定的延迟,直到你的程序退出主却迟迟没有这么小你没有注意到它。

It's not actually the sleep function which is delaying the output, it's the buffering nature of the standard output stream. The output of 2 is almost certainly also delayed until your program exits main but the delay there is so small you're not noticing it.

,如果它可以被检测到指交互设备(否则它是完全缓冲)标准输出行缓冲

Standard output is line buffered if it can be detected to refer to an interactive device (otherwise it's fully buffered).

如果您 fflush(标准输出)要立即看到每个输出的呼叫,这将解决这个问题了。

If you fflush (stdout) after every output call that you want to see immediately, that will solve the problem.

另外,你可以在标准输出操作前使用 setvbuf用来,将其设置为无缓冲,你不会不必担心将所有的 fflush 行到code:

Alternatively, you can use setvbuf before operating on stdout, to set it to unbuffered and you won't have to worry about adding all those fflush lines to your code:

setvbuf (stdout, NULL, _IONBF, BUFSIZ);

请记住,如果你发送输出到文件,可能会影响性能相当多。也请记住,这是支持实现定义的,而不是由标准的保证。

Just keep in mind that may affect performance quite a bit if you're sending the output to a file. Also keep in mind that support for this is implementation-defined, not guaranteed by the standard.

ISO C99部分 7.19.3 / 3 是相关位:

ISO C99 section 7.19.3/3 is the relevant bit:

当一个流的无缓冲的,字符旨在尽快从源或在目的地显示。否则字符可以累积并传送到或从主机环境为一个块。

When a stream is unbuffered, characters are intended to appear from the source or at the destination as soon as possible. Otherwise characters may be accumulated and transmitted to or from the host environment as a block.

当一个流的全缓冲的,字符旨在被发送到或者从主机环境时的缓冲器被填充的块。

When a stream is fully buffered, characters are intended to be transmitted to or from the host environment as a block when a buffer is filled.

当一个流的行缓冲的,字符旨在被发送到或者从主机环境遇到一个新行字符时的块

When a stream is line buffered, characters are intended to be transmitted to or from the host environment as a block when a new-line character is encountered.

此外,字符被打算作为宿主环境的块时的缓冲器填充,当请求在无缓冲流输入时,或者当要求对一个线缓冲流输入,需要从字符的发送要发送主机环境。

Furthermore, characters are intended to be transmitted as a block to the host environment when a buffer is filled, when input is requested on an unbuffered stream, or when input is requested on a line buffered stream that requires the transmission of characters from the host environment.

有关这些特性的支持是实现定义的,并且可以通过则setbuf setvbuf用来功能受到影响。

Support for these characteristics is implementation-defined, and may be affected via the setbuf and setvbuf functions.

这篇关于睡眠()延迟输出,直到结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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