是的glibc的实现fprintf中()的线程安全? [英] Is glibc's implementation of fprintf() thread-safe?

查看:138
本文介绍了是的glibc的实现fprintf中()的线程安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

fprintf中是线程安全的? glibc的手册似乎说是,但我的应用程序,它采用单调用fprintf中()似乎是从交织不同的进程部分写入写入一个文件。

编辑:澄清,问题的程序是 lighttpd的插件,服务器与多个工作线程运行

看文件,有些写的是混合在一起。

编辑2:看来我看到的可能是由于lighttpd的的工作线程实际上是独立的进程问题:<一href=\"http://redmine.lighttpd.net/wiki/lighttpd/Docs:MultiProcessor\">http://redmine.lighttpd.net/wiki/lighttpd/Docs:MultiProcessor


  

问题


  
  

通过运行在2个或更多的过程
  同一个插座,你将有一个更好
  并发性,但将有几
  你必须要知道的缺点
  的:


  
  

      
  • mod_accesslog可能造成破访问日志,作为同一个文件被打开两次,不是同步的。

  •   
  • mod_status的会有的 N 的独立的计数器,每个组
      流程。

  •   
  • mod_rrdtool会收到两次相同的时间戳失败。

  •   
  • mod_uploadprogress不会显示正确的状态。

  •   


解决方案

你混淆的两个概念 - 从多个线程编写,并从多个进程写

在其可能的一个过程,以确保下一个被允许访问输出缓冲区之前fprintf中的一个调用完成,但一旦你的应用程序的泵输出到你的操作系统,任人宰割的文件。如果没有某种形式的基于OS锁定机制,你不能保证一个完全不同的应用程序犯规写入日志文件。

Is fprintf thread-safe? The glibc manual seems to say it is, but my application, which writes to a file using single call to fprintf() seems to be intermingling partial writes from different processes.

edit: To clarify, the program in question is a lighttpd plugin, and the server is running with multiple worker threads.

Looking at the file, some of the writes are intermingled.

edit 2: It seems the problem I'm seeing might be due to lighttpd's "worker threads" actually being separate processes: http://redmine.lighttpd.net/wiki/lighttpd/Docs:MultiProcessor

Problems

By running 2 or more processes on the same socket you will have a better concurrency, but will have a few drawbacks that you have to be aware of:

  • mod_accesslog might create broken access logs, as the same file is opened twice and is NOT synchronized.
  • mod_status will have n separate counters, one set for each process.
  • mod_rrdtool will fail as it receives the same timestamp twice.
  • mod_uploadprogress will not show correct status.

解决方案

You're confusing two concepts - writing from multiple threads and writing from multiple processes.

Inside a process its possible to ensure that one invocation of fprintf is completed before the next is allowed access to the output buffer, but once your app pumps that output to a file you're at the mercy of the OS. Without some kind of OS based locking mechanism you cant ensure that an entirely different application doesnt write to your log file.

这篇关于是的glibc的实现fprintf中()的线程安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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