更新:我能够使用端口解决下面描述的限制。例如,这是一个将“hello world”打印到/dev/stdout的示例程序:
-module(test).
-export([main/1]).
main(X) ->
P = open_port({spawn, "/bin/cat >/dev/stdout"}, [out]),
P ! {self(), {command, "hello world"}}.
这有点不方便,因为端口不像常规文件,但至少这是完成工作的一种方式。
在efile_openfile()(erts/emulator/drivers/unix/unix_efile.c)中有以下代码:
if (stat(name, &statbuf) >= 0 && !ISREG(statbuf)) {
#if !defined(VXWORKS) && !defined(OSE)
/*
* For UNIX only, here is some ugly code to allow
* /dev/null to be opened as a file.
*
* Assumption: The i-node number for /dev/null cannot be zero.
*/
static ino_t dev_null_ino = 0;
if (dev_null_ino == 0) {
struct stat nullstatbuf;
if (stat("/dev/null", &nullstatbuf) >= 0) {
dev_null_ino = nullstatbuf.st_ino;
}
}
if (!(dev_null_ino && statbuf.st_ino == dev_null_ino)) {
#endif
errno = EISDIR;
return check_error(-1, errInfo);
#if !defined(VXWORKS) && !defined(OSE)
}
#endif
}
如果文件不是常规文件(这是ISREG(statbuf) 检查),此代码(令人困惑)返回EISDIR 错误,除非文件具体是/dev/null。 file(3) 文档指出:
eisdir :
The named file is not a regular file. It may be a directory, a
fifo, or a device.
所以它实际上是记录在案的。不过,我不确定为什么存在这种限制——也许它与性能有关,因为设备驱动程序可能会比普通文件通常阻塞的时间更长。