我有一个非常基本的代码运行,类似这样(我尝试过Python和C):
for (;;) {
fprintf(f, strftime());
fflush(f);
sleep(1);
}
我的系统中某处出了问题,因为日志中两条连续线之间的时差有时是十分之几秒!
fflush
会触发什么样的长时间运行过程?
PS:正在打开的文件位于本地驱动器上。
编辑:
strace -tt
日志的摘录:
12:12:42.664505 rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0
12:12:42.664571 rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0
12:12:42.664595 rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
12:12:42.664614 nanosleep({1, 0}, 0x7fff3c26fad0) = 0
12:12:43.664818 stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2309, ...}) = 0
12:12:43.664944 write(3, "2016-03-17 12:12:43\n", 20) = 20
12:12:43.665018 rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0
12:12:43.665102 rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0
12:12:43.665168 rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
12:12:43.665217 nanosleep({1, 0}, 0x7fff3c26fad0) = 0
12:12:49.789302 stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2309, ...}) = 0
12:12:49.789430 write(3, "2016-03-17 12:12:49\n", 20) = 20
12:12:49.789495 rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0
12:12:49.789549 rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0
12:12:49.789605 rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
12:12:49.789648 nanosleep({1, 0}, 0x7fff3c26fad0) = 0
12:12:50.946087 stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2309, ...}) = 0
12:12:50.946201 write(3, "2016-03-17 12:12:50\n", 20) = 20
12:12:50.946273 rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0
我很困惑,因为我从一个更复杂的例子中提取了这个非常简单的测试,没有等待,但是显然需要时间,尽管这里的问题似乎来自nanosleep
花了太长时间。