Are there repercussions to having many processes write to a single reader on a named pipe in posix?

前端 未结 1 857
礼貌的吻别
礼貌的吻别 2021-01-05 02:17

I am writing a program for POSIX (OSX) where I will have many processes sending messages to one listener, who is essentially a logging thread. All of the processes are runn

相关标签:
1条回答
  • 2021-01-05 02:55

    The FIFO write should be atomic, as long as it's under the page size. So there shouldn't be an issue with 100 bytes messages. On linux the max size used to be 4K, I believe it is larger now. I've used this technique on a few systems for message passing, since the writes end up atomic.

    You can end up with an issue, if you are using a series of writes, since output buffering could cause a sync issue. So make sure the whole message is written at one time. eg. build a string, then print, don't print multiple pieces at once.

    s="This is a message"
    echo $s
    

    NOT

    echo "This "
    echo "is "
    echo " a message"
    
    0 讨论(0)
提交回复
热议问题