【问题标题】:Opening a device file using erlang使用erlang打开设备文件
【发布时间】:2011-05-29 21:05:12
【问题描述】:

有没有办法在erlang中打开终端设备文件?

我在 Solaris 上,我正在尝试以下操作::

Erlang (BEAM) 仿真器版本 5.6 [来源] [64 位] [异步线程:0] [内核轮询:假] /xlcabpuser1/xlc/abp/arunmu/Dolphin/ebin Eshell V5.6(使用 ^G 中止) 1> 文件:打开(“/dev/pts/2”,[写入])。 {错误,eisdir} 2> 文件:打开(“/dev/null”,[写入])。 {好的,} 3>

从上面可以看出erlang文件驱动打开空文件没有问题但是没有打开终端设备文件!!

无法得出结论,因为文件驱动程序能够打开空文件。

还有其他方法可以打开终端设备文件吗?

谢谢

【问题讨论】:

  • 一种解决方法可能会有所帮助:您可以为自己编写一个包装器,例如在作为端口启动的 C 或 python 中。
  • @ZeissS:是的,可以。但我在想为什么不这样呢?我可以在 perl 中做到这一点。

标签: file file-io serial-port erlang


【解决方案1】:

更新:我能够使用端口解决下面描述的限制。例如,这是一个将“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/nullfile(3) 文档指出:

     eisdir :
       The named file is not a regular file. It  may  be  a  directory,  a
       fifo, or a device.

所以它实际上是记录在案的。不过,我不确定为什么存在这种限制——也许它与性能有关,因为设备驱动程序可能会比普通文件通常阻塞的时间更长。

【讨论】:

猜你喜欢
  • 2021-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多