Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C fprintf随机产生奇怪的结果_C_Logging_Printf - Fatal编程技术网

C fprintf随机产生奇怪的结果

C fprintf随机产生奇怪的结果,c,logging,printf,C,Logging,Printf,我有一段将内存缓存记录到文件的代码: void cache_dump(const int signal) { rewind(cache_file); // this is ok, because the cache will never shrink unsigned int n; for (n = 0; n < cache_size; n++){ inet_ntop(AF_INET, &cache[n].device, device_b

我有一段将内存缓存记录到文件的代码:

void cache_dump(const int signal) {
    rewind(cache_file); // this is ok, because the cache will never shrink

    unsigned int n;

    for (n = 0; n < cache_size; n++){
        inet_ntop(AF_INET, &cache[n].device, device_buffer, INET_ADDRSTRLEN);
        inet_ntop(AF_INET, &cache[n].remote.sin_addr, remote_buffer, INET_ADDRSTRLEN);

        fprintf(cache_file, "%-15s %-15s %-5u %-lu\n", device_buffer, remote_buffer, ntohs(cache[n].remote.sin_port), cache[n].last_seen);
    }
}
其他信息

  • device\u buffer
    remote\u buffer
    char[INET6\u ADDRSTRLEN]
    对于ipv4地址来说足够大
  • 代码不是多线程的
  • 其他进程以只读模式访问文件-此代码是唯一具有写入权限的代码
奇怪的结果

结果文件将随机损坏,如下所示:

10.13.14.247   10.12.19.240   31337 1472552078
10.13.14.248   10.12.11.244   31337 1472552079
10.13.14.249   10.12.11.237   313310.108.16.151   10.230.16.102   31337 1469798419
10.13.28.1     10.12.26.148   31337 1472551983
10.13.28.58    10.12.127.241  31337 1472552008
因此,一行会突然中断(没有新行)并与下一行合并。这种情况很少发生,但仍然存在。这似乎与高系统负载有关,但在查看
htop
后,这是一个猜测

有人遇到过这种奇怪的行为吗

更新

缓存文件的打开方式如下:

cache_fd = open(logpath, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IRGRP | S_IROTH); // open for writing, empty, create if does not exist, set 444 rights

if (cache_fd == -1)
    die("Failed to open cache file for writing!");

cache_file = fdopen(cache_fd, "w");

if (cache_file == NULL)
    die("Failed to open cache file descriptor for writing!");

我看到名为
signal
的变量。是否也有信号处理程序打印到文件?缓存文件是否缓冲?未缓冲?你能分享文件打开代码吗?@JonathonReinhart这连接到一个信号处理程序,用于每个请求使用,但目前它并不是这样使用的——它是从邮件循环周期性地执行的。您有时可能有两个writer进程实例同时运行吗?您提供的代码本身并不能解释您描述的行为。如果(1)在系统范围内,除了一次执行您提供的代码的单个线程外,没有任何内容修改文件,并且(2)除非通过您提供的代码,否则编写过程不会操纵文件的打开文件描述,那么您描述的结果将描述底层系统中的错误。然而,我认为更可能的是,(1)或(2)实际上不成立。我看到了名为
signal
的变量。是否也有信号处理程序打印到文件?缓存文件是否缓冲?未缓冲?你能分享文件打开代码吗?@JonathonReinhart这连接到一个信号处理程序,用于每个请求使用,但目前它并不是这样使用的——它是从邮件循环周期性地执行的。您有时可能有两个writer进程实例同时运行吗?您提供的代码本身并不能解释您描述的行为。如果(1)在系统范围内,除了一次执行您提供的代码的单个线程外,没有任何内容修改文件,并且(2)除非通过您提供的代码,否则编写过程不会操纵文件的打开文件描述,那么您描述的结果将描述底层系统中的错误。然而,我认为更有可能的是,(1)或(2)实际上并不成立。
cache_fd = open(logpath, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IRGRP | S_IROTH); // open for writing, empty, create if does not exist, set 444 rights

if (cache_fd == -1)
    die("Failed to open cache file for writing!");

cache_file = fdopen(cache_fd, "w");

if (cache_file == NULL)
    die("Failed to open cache file descriptor for writing!");