简体   繁体   English

使用php写入linux中的管道

[英]Writing to a pipe in linux using php

I have created a pipe in linux mkfifo /tmp/myFIFO now i have set the chmod to 777.. then i have ac application which reads the pipe and output what i wrote to the pipe. 我已经在linux mkfifo /tmp/myFIFO创建了一个管道,现在我已经将chmod设置为777 ..然后我有一个ac应用程序,它读取管道并输出我写入管道的内容。

When i do it this way in php and this works.. 当我这样在PHP中这样做,这工作..

$command = "echo 'helloworld' > myFIFO";
$process = proc_open($command, $descriptor, $pipes, $cwd, $env);

but when i do it with fwrite eg 但是当我用fwrite做的时候,例如

$out = fopen("/tmp/myFIFO","w");
fwrite($out,"hello");
fclose($out);

It does not work at all. 它根本不起作用。 I'm not getting any output from my pipe in linux?.. Why is that fwrite is not working?.. thanks 我没有从我的管道输出任何输出?..为什么那个fwrite不工作?...谢谢

It's working now thanks for all your help. 它现在正在工作,谢谢你的帮助。 I set the chmod to a+rw. 我将chmod设置为+ rw。

chmod a+rw /tmp/myFIFO

Now it's working fine now. 现在它工作正常。

Thanks for all your help. 感谢你的帮助。

FIFOs have the strange behavior that when you first open them for writing (either directly with the open(2) system call or via any wrapper such as fopen(3) ), the open call blocks until a reader also opens the same FIFO for reading. FIFO具有奇怪的行为,当您第一次open它们进行写入时(直接使用open(2)系统调用或通过任何包装器,如fopen(3) ), open调用阻塞,直到读取器也打开相同的FIFO进行读取。 When both ends have opened, the two calls are unblocked, and reading and writing can commence. 当两端都打开时,两个呼叫都被解锁,并且可以开始读写。

What you're probably seeing is that your program is never getting to the fwrite call (which should function normally) because the fopen call never returns, since no reader has opened the other end of the FIFO yet. 你可能看到的是你的程序永远不会进入fwrite调用(它应该正常运行),因为fopen调用永远不会返回,因为没有读者已经打开了FIFO的另一端。 If you have another program open up the FIFO for reading, it should work. 如果你有另一个程序打开FIFO进行读取,它应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM