44

我有两个程序,Writer 和 Reader。

我有一个从 Writer 到 Reader 的 FIFO,所以当我在 Writer 中向 stdin 写入内容时,它会从 Reader 打印到 stdout。

我尝试在打开两个阅读器的情况下执行此操作,并且仅从两个阅读器程序中的一个将输出输出到标准输出。每次我运行它时,Unix 选择从哪个 Reader 程序打印 stdout 似乎是任意的,但是一旦它选择了其中一个程序,stdout 的每个输出都会从同一个 Reader 程序打印。

有谁知道为什么会这样?

如果我有两个 WRITER 程序,它们都可以写入同一个管道。

4

5 回答 5

33

FIF O 中的 O表示“出局”。一旦您的数据“出局”,它就消失了。:-) 因此,如果另一个进程出现并且其他人已经发出读取,那么数据就不会出现两次。

要完成您的建议,您应该查看 Unix 域套接字。手册页在这里。您可以编写一个可以写入客户端进程的服务器,绑定到文件系统路径。另请参阅socket(), bind(), listen(), accept(), connect(), 所有这些您都想与PF_UNIX,AF_UNIX和一起使用struct sockaddr_un

于 2009-10-28T00:50:21.333 回答
11

Linux tee() 可能适合您的需求。
这里

注意:此功能是 Linux 特定的。

于 2009-10-28T06:23:39.367 回答
3

我不认为你观察到的行为不仅仅是巧合。考虑这个跟踪,它使用“sed”作为两个读取器,一个循环作为写入器:

Osiris JL: mkdir fifo
Osiris JL: cd fifo
Osiris JL: mkfifo fifo
Osiris JL: sed 's/^/1: /' < fifo &
[1] 4235
Osiris JL: sed 's/^/2: /' < fifo &
[2] 4237
Osiris JL: while read line ; do echo $line; done > fifo < /etc/passwd
1: ##
1: # User Database
1: #
1: # Note that this file is consulted directly only when the system is running
1: # in single-user mode. At other times this information is provided by
1: # Open Directory.
1: #
1: # This file will not be consulted for authentication unless the BSD local node
1: # is enabled via /Applications/Utilities/Directory Utility.app
1: #
1: # See the DirectoryService(8) man page for additional information about
1: # Open Directory.
1: ##
1: nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false
1: root:*:0:0:System Administrator:/var/root:/bin/sh
1: daemon:*:1:1:System Services:/var/root:/usr/bin/false
1: _uucp:*:4:4:Unix to Unix Copy Protocol:/var/spool/uucp:/usr/sbin/uucico
1: _lp:*:26:26:Printing Services:/var/spool/cups:/usr/bin/false
2: _postfix:*:27:27:Postfix Mail Server:/var/spool/postfix:/usr/bin/false
2: _mcxalr:*:54:54:MCX AppLaunch:/var/empty:/usr/bin/false
2: _pcastagent:*:55:55:Podcast Producer Agent:/var/pcast/agent:/usr/bin/false
2: _pcastserver:*:56:56:Podcast Producer Server:/var/pcast/server:/usr/bin/false
2: _serialnumberd:*:58:58:Serial Number Daemon:/var/empty:/usr/bin/false
2: _devdocs:*:59:59:Developer Documentation:/var/empty:/usr/bin/false
2: _sandbox:*:60:60:Seatbelt:/var/empty:/usr/bin/false
2: _mdnsresponder:*:65:65:mDNSResponder:/var/empty:/usr/bin/false
2: _ard:*:67:67:Apple Remote Desktop:/var/empty:/usr/bin/false
2: _www:*:70:70:World Wide Web Server:/Library/WebServer:/usr/bin/false
2: _eppc:*:71:71:Apple Events User:/var/empty:/usr/bin/false
2: _cvs:*:72:72:CVS Server:/var/empty:/usr/bin/false
2: _svn:*:73:73:SVN Server:/var/empty:/usr/bin/false
2: _mysql:*:74:74:MySQL Server:/var/empty:/usr/bin/false
2: _sshd:*:75:75:sshd Privilege separation:/var/empty:/usr/bin/false
2: _qtss:*:76:76:QuickTime Streaming Server:/var/empty:/usr/bin/false
2: _cyrus:*:77:6:Cyrus Administrator:/var/imap:/usr/bin/false
2: _mailman:*:78:78:Mailman List Server:/var/empty:/usr/bin/false
2: _appserver:*:79:79:Application Server:/var/empty:/usr/bin/false
2: _clamav:*:82:82:ClamAV Daemon:/var/virusmails:/usr/bin/false
2: _amavisd:*:83:83:AMaViS Daemon:/var/virusmails:/usr/bin/false
2: _jabber:*:84:84:Jabber XMPP Server:/var/empty:/usr/bin/false
2: _xgridcontroller:*:85:85:Xgrid Controller:/var/xgrid/controller:/usr/bin/false
2: _xgridagent:*:86:86:Xgrid Agent:/var/xgrid/agent:/usr/bin/false
2: _appowner:*:87:87:Application Owner:/var/empty:/usr/bin/false
2: _windowserver:*:88:88:WindowServer:/var/empty:/usr/bin/false
2: _spotlight:*:89:89:Spotlight:/var/empty:/usr/bin/false
2: _tokend:*:91:91:Token Daemon:/var/empty:/usr/bin/false
2: _securityagent:*:92:92:SecurityAgent:/var/empty:/usr/bin/false
2: _calendar:*:93:93:Calendar:/var/empty:/usr/bin/false
2: _teamsserver:*:94:94:TeamsServer:/var/teamsserver:/usr/bin/false
2: _update_sharing:*:95:-2:Update Sharing:/var/empty:/usr/bin/false
2: _installer:*:96:-2:Installer:/var/empty:/usr/bin/false
2: _atsserver:*:97:97:ATS Server:/var/empty:/usr/bin/false
2: _unknown:*:99:99:Unknown User:/var/empty:/usr/bin/false
Osiris JL:  jobs
[1]-  Running                 sed 's/^/1: /' < fifo &
[2]+  Done                    sed 's/^/2: /' < fifo
Osiris JL: echo > fifo
1: 
Osiris JL: jobs
[1]+  Done                    sed 's/^/1: /' < fifo
Osiris JL: 

如您所见,两位读者都必须阅读一些数据。在任何时候安排哪个阅读器取决于操作系统的心血来潮。请注意,我小心地使用了 echo 来打印文件的每一行;那些是原子读取的原子写入。

如果我使用 Perl 脚本,例如,在读取和回显一行后有延迟,那么我很可能会看到(通常)阅读器 1 的两行对应阅读器 2 的每 1 行的更确定的行为。

perl -n -e 'while(<>){ print "1: $_"; sleep 1; }' < fifo &
perl -n -e 'while(<>){ print "2: $_"; sleep 2; }' < fifo &

在 MacOS X 10.5.8 (Leopard) 上进行的实验——但可能与大多数地方相似。

于 2009-10-28T02:00:52.970 回答
2

我想补充一下上面的解释,即写入(和可能的读取,尽管我无法从手册页确认这一点)到管道的原子大小达到一定大小(Linux 上为 4KiB)。所以假设我们从一个空管道开始,写入器将 <=4KiB 数据写入管道。这是我认为会发生的事情:

a) writer 一次性写入所有数据。虽然发生这种情况,但没有其他进程有机会读取(或写入)管道。

b) 其中一位读者被安排进行 I/O。

c) 选定的阅读器一次性从管道中读取所有数据,并在稍后将它们打印到其标准输出。

我认为当您只看到一位读者的输出时,这可以解释。尝试以较小的块写入,并且可能在每次写入后睡觉。

当然,其他人已经回答了为什么每个数据只能由进程读取。

于 2013-01-16T11:50:52.677 回答
0

The sockets solution works, but becomes complicated if the server crashes. To allow any process to be the server, I use record locks at the end of a temporary file that contains location/length/data changes to the given file. I use a temporary named pipe to communicate append requests to whichever process has the write lock at the end of the temporary file.

于 2019-06-07T17:05:43.513 回答