百科狗-知识改变命运!
--

posix_mkfifo() - posix函数(可移植操作系统接口)

乐乐12个月前 (11-21)阅读数 22#技术干货
文章标签所有者

posix_mkfifo()

posix_mkfifo() - posix函数(可移植操作系统接口)

(PHP 4, PHP 5, PHP 7)

Create a fifo special file(a named pipe)

说明

posix_mkfifo(string $pathname,int $mode): bool

posix_mkfifo() creates a specialFIFOfile which exists in the file system and acts as a bidirectional communication endpoint for processes.

参数

$pathname

Path to theFIFOfile.

$mode

The second parameter$modehas to be given in octal notation(e.g. 0644). The permission of the newly createdFIFOalso depends on the setting of the current umask(). The permissions of the created file are(mode &~umask).

返回值

成功时返回TRUE,或者在失败时返回FALSE

注释

Note:当启用安全模式时,PHP 会检查被操作的文件或目录是否与被执行的脚本有相同的 UID(所有者)。

For non-blocking, fopen'd read access to a "half-connected" pipe (created with /usr/bin/mkfifo, posix_mkfifo, etc.), I just go ahead and do:

The "r+" allows fopen to return immediately regardless of external writer channel. You then have to use your own conventions to track $fh as a pseudo-read-only resource, since fwrite would technically be permitted as well. I've successfully used this approach on Linux with PHP 4.3.10 and PHP 5.2.4 with both half-connected (no writer yet) and pre-connected (writer already waiting) pipes, polling with stream_select as usual.
Couldn't you just open up a writer yourself right after you open up a reader? Just to be sure that you won't have any blocking issues...
Here is a possible solution to what - tech at kwur dot com- mentioned:
 I faced the problem where i had a process (a server) that needed to take care of socket connection, and in the meanwhile get some data from the database. I didn't wanted to make the clients wait for the query execution time, so i decided to make a separate process that executes the query on the DB, and the two would communicate over a pipe. Of course i didn't wanted the server blocking if no data was available. So what i come up with is to use stream_select() , and to overcome the mentioned problem, i would fork the process, open up the pipe for writing in the child, this way the parent won't block when it opens the pipe.
here is some code 
A way to have a non-blocking pipe reader is to check first if the pipe exists. If so, then read from the pipe, otherwise do other stuff. This will work assuming that the writer creates the pipe, writes on it, and after that deletes the pipe.
This is a blocking writer:

And this is the non-blocking reader: 
Note (quoted from `man 7 pipe` on debian linux):
"On some systems (but not Linux), pipes are bidirectional: data can be transmitted in both directions between the pipe ends. According to POSIX.1-2001, pipes only need to be unidirectional. Portable applications should avoid reliance on bidirectional pipe semantics."
Linux pipes are NOT bidirectional.
Also, it appears to me that the use of fifo (named) pipes in php is pretty pointless as there appears to be NO way of determining whether opening (let alone reading) from it will block. stream_select SHOULD be able to accomplish this, unfortunatly you cannot get to this point because even trying to OPEN a pipe for read will block until there is a writer.
I even tried to use popen("cat $name_of_pipe", 'r'), and even it blocked until it was opened for write by another process.
Object Oriented FIFO Communication process: 
This is still not a solution: if I listen to commands on a pipe and output status on a separate pipe, PHP will block on both opens because something else has not already connected to this pipe. Because I can't do a low-level fcntl() to to set O_NONBLOCK or something like it, this always locks up and is really stupid. The only way I can get it to work is to spawn seperate subshells with system() and have them cat, or echo respectively and then the pipes work properly...usually? Its alot of trouble that we can't set the blocking on the open!!

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)