tzset()
分叉后调用似乎非常慢.如果我tzset()
在分叉之前首先调用父进程,我只会看到缓慢.我的TZ
环境变量未设置.我dtruss
是我的测试程序,它揭示了/etc/localtime
每个tzset()
调用的子进程读取,而父进程只读取一次.这个文件访问似乎是缓慢的来源,但我无法确定它每次在子进程中访问它的原因.
这是我的测试程序foo.c:
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
void check(char *msg);
int main(int argc, char **argv) {
check("before");
pid_t c = fork();
if (c == 0) {
check("fork");
exit(0);
}
wait(NULL);
check("after");
}
void check(char *msg) {
struct timeval tv;
gettimeofday(&tv, NULL);
time_t start = tv.tv_sec;
suseconds_t mstart = tv.tv_usec;
for (int i = 0; i < 10000; i++) {
tzset();
}
gettimeofday(&tv, NULL);
double delta = …
Run Code Online (Sandbox Code Playgroud)