多个进程可以追加到使用的fopen没有任何并发​​问题的文件? [英] Can multiple processes append to a file using fopen without any concurrency problems?

查看:318
本文介绍了多个进程可以追加到使用的fopen没有任何并发​​问题的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个过程,追加方式打开文件。在这种情况下,它是一个日志文件。示例code:

I have a process opening a file in append mode. In this case it is a log file. Sample code:

int main(int argc, char **argv) {
    FILE *f;
    f = fopen("log.txt", "a");
    fprintf(f, "log entry line");
    fclose(f);
}

两个问题:


  1. 如果我有多个进程追加到同一文件中,将每​​个日志行出现明显或对它们进行交错的进程上下文切换?

  2. 请问,如果大量进程需要访问的文件,因此导致并发问题这个写块?

我正在考虑无论是在最简单的化身这样做或使用zeromq在管道日志收集泵日志条目。

I am considering either doing this in its simplest incarnation or using zeromq to pump log entries over pipes to a log collector.

我没有考虑系统日志,但我真的不希望在软件的平台依赖性。

I did consider syslog but I don't really want any platform dependencies on the software.

默认的平台是Linux的这个BTW。

The default platform is Linux for this btw.

推荐答案

您一定会拥有平台的依赖性,因为Windows无法处理多个进程追加到同一文件中。

You'll certainly have platform dependencies since Windows can't handle multiple processes appending to the same file.

关于同步的问题,我觉得行缓冲输出/要/保存你的大部分时间,短日志行即99.99%以上的应根据我的短基于外壳的测试是完整的,但不是每一次。明确的语义是绝对preferable,因为你不能写这个黑客系统独立反正,我建议系统日志的方法。

Regarding synchronization problems, I think that line-buffered output /should/ save you most of the time, i.e. more than 99.99% of short log lines should be intact according to my short shell-based test, but not every time. Explicit semantics are definitely preferable, and since you won't be able to write this hack system-independently anyway, I'd recommend a syslog approach.

这篇关于多个进程可以追加到使用的fopen没有任何并发​​问题的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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