<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>Fprintf on ZRJ | 学习笔记</title>
        <link>https://blog.zrj.me/tags/fprintf/</link>
        <description>Recent content in Fprintf on ZRJ | 学习笔记</description>
        <generator>Hugo -- gohugo.io</generator>
        <language>zh-CN</language>
        <lastBuildDate>Thu, 11 Sep 2014 09:42:46 +0800</lastBuildDate><atom:link href="https://blog.zrj.me/tags/fprintf/index.xml" rel="self" type="application/rss+xml" /><item>
        <title>fprintf 是线程安全的吗</title>
        <link>https://blog.zrj.me/posts/2014-09-11-fprintf-%E6%98%AF%E7%BA%BF%E7%A8%8B%E5%AE%89%E5%85%A8%E7%9A%84%E5%90%97/</link>
        <pubDate>Thu, 11 Sep 2014 09:42:46 +0800</pubDate>
        
        <guid>https://blog.zrj.me/posts/2014-09-11-fprintf-%E6%98%AF%E7%BA%BF%E7%A8%8B%E5%AE%89%E5%85%A8%E7%9A%84%E5%90%97/</guid>
        <description>&lt;p&gt;以前一直担心在多线程环境下，多个线程同时调用 fprintf 往同一个 fp 写东西会不会出现交错的情况，昨天微博上问了问人，有提到 fprintf_unlocked，也说到 fprintf 是线程安全的，后来又查了一下，看到这里，&lt;a class=&#34;link&#34; href=&#34;http://stackoverflow.com/questions/11664434/how-fprintf-behavior-when-multi-threaded-and-multi-processed&#34;  target=&#34;_blank&#34; rel=&#34;noopener&#34;
    &gt;http://stackoverflow.com/questions/11664434/how-fprintf-behavior-when-multi-threaded-and-multi-processed&lt;/a&gt; ，其中也有提到，fprintf 在多线程下是内部持有锁的&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;If you&amp;rsquo;re using a single &lt;code&gt;FILE&lt;/code&gt; object to perform output on an open file, then whole &lt;code&gt;fprintf&lt;/code&gt; calls on that &lt;code&gt;FILE&lt;/code&gt; will be atomic, i.e. lock is held on the &lt;code&gt;FILE&lt;/code&gt; for the duration of the &lt;code&gt;fprintf&lt;/code&gt; call. Since a &lt;code&gt;FILE&lt;/code&gt; is local to a single process&amp;rsquo;s address space, this setup is only possible in multi-threaded applications; it does not apply to multi-process setups where several different processes are accessing separate &lt;code&gt;FILE&lt;/code&gt; objects referring to the same underlying open file. Even though you&amp;rsquo;re using &lt;code&gt;fprintf&lt;/code&gt; here, each process has its own &lt;code&gt;FILE&lt;/code&gt; it can lock and unlock without the others seeing the changes, so writes can end up interleaved. There are several ways to prevent this from happening:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Allocate a synchronization object (e.g. a process-shared semaphore or mutex) in shared memory and make each process obtain the lock before writing to the file (so only one process can write at a time); OR&lt;/li&gt;
&lt;li&gt;Use filesystem-level advisory locking, e.g. &lt;code&gt;fcntl&lt;/code&gt; locks or the (non-POSIX) BSD &lt;code&gt;flock&lt;/code&gt; interface; OR&lt;/li&gt;
&lt;li&gt;Instead of writing directly to the log file, write to a pipe that another process will feed into the log file. Writes to a pipe are guaranteed (by POSIX) to be atomic as long as they are smaller than &lt;code&gt;PIPE_BUF&lt;/code&gt;bytes long. You cannot use &lt;code&gt;fprintf&lt;/code&gt; in this case (since it might perform multiple underlying write operations), but you could use &lt;code&gt;snprintf&lt;/code&gt; to a &lt;code&gt;PIPE_BUF&lt;/code&gt;-sized buffer followed by &lt;code&gt;write&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;
</description>
        </item>
        
    </channel>
</rss>
