类 PTY

创建和管理伪终端(PTYs)。另请参见 en.wikipedia.org/wiki/Pseudo_terminal

PTY 允许您使用 ::open 分配新的终端,或使用 ::spawn 使用特定命令生成新的终端。

示例

在此示例中,我们将更改 factor 命令中的缓冲类型,假设 factor 使用 stdio 进行 stdout 缓冲。

如果使用 IO.pipe 而不是 PTY.open,则此代码将死锁,因为 factor 的 stdout 是完全缓冲的。

# start by requiring the standard library PTY
require 'pty'

master, slave = PTY.open
read, write = IO.pipe
pid = spawn("factor", :in=>read, :out=>slave)
read.close     # we dont need the read
slave.close    # or the slave

# pipe "42" to the factor command
write.puts "42"
# output the response from factor
p master.gets #=> "42: 2 3 7\n"

# pipe "144" to factor and print out the response
write.puts "144"
p master.gets #=> "144: 2 2 2 2 3 3\n"
write.close # close the pipe

# The result of read operation when pty slave is closed is platform
# dependent.
ret = begin
        master.gets     # FreeBSD returns nil.
      rescue Errno::EIO # GNU/Linux raises EIO.
        nil
      end
p ret #=> nil

许可证

© 版权所有 1998 Akinori Ito。

本软件可以为此目的全部或部分免费重新分发,前提是本软件的任何副本以及本软件及其衍生应用程序中均包含完整的版权声明。

本软件按“原样”提供,不作任何形式的保证,无论是明示的还是暗示的,包括但不限于对适用性,适销性或使用本软件获得的结果的保证。