C:设置和伪终端使用xterm的开放 [英] C: setup pseudoterminal and open with xterm

查看:186
本文介绍了C:设置和伪终端使用xterm的开放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

code的以下简化件通过在后台线程中执行。线程运行,直到他被告知(由用户输入)退出。

The following simplified piece of code is executed by a thread in the background. The thread runs until he is told to exit (by user input).

在code下面我已删除了一些错误检查更好的可读性。即使有错误检查code运作良好,主机和从机被创建和/或打开。

In the code below I have removed some error checking for better readability. Even with error checking the code works well and both the master and the slave are created and/or opened.

...
int master, slave;
char *slavename;
char *cc;

master = posix_openpt(O_RDWR);

grantpt(master);
unlockpt(master);
slavename = ptsname(master);
slave = open(slavename, O_RDWR);

printf("master: %d\n",master);
printf("slavename: %s\n",slavename);

在我的机器上输出以下内容:

On my machine the output is the following:

master: 3
slavename: /dev/pts/4

所以,我认为,打开一个xterm使用命令的xterm -S4 / 3 (4 = PT-奴隶,3 = PT-主)我的程序运行时,应打开一个新的xterm窗口的创建伪终端。但xterm的刚刚起步没有给错误或任何进一步的信息运行,但根本不打开一个窗口。上有什么建议?

So I thought that opening an xterm with the command xterm -S4/3 (4 = pt-slave, 3 = pt-master) while my program is running should open a new xterm window for the created pseudoterminal. But xterm just starts running without giving an error or any further informations but does not open a window at all. Any suggestions on that?

现在与Wumpus Q. Wumbley的帮助xterm的正常启动,但我不能任何输出重定向到它。我想:

Now with Wumpus Q. Wumbley's help xterm starts normally, but I can't redirect any output to it. I tried:

dup2(slave, 1);
dup2(slave, 2);

printf("Some test message\n");

的fopen 打开奴隶,然后用 fprinf 。两人都没有工作。

and opening the slave with fopen and then using fprinf. Both didn't work.

推荐答案

在xterm进程需要访问该文件描述符莫名其妙。此功能的设计用途是可能推出的xterm作为创建pty中一个子进程。还有其他的方法,虽然。你可以使用SCM_RIGHTS文件描述符传递(pretty复杂)或者,如果你有一个Linux样式的/ proc 文件系统试试这个:

The xterm process needs to get access to the file descriptor somehow. The intended usage of this feature is probably to launch xterm as a child process of the one that created the pty. There are other ways, though. You could use SCM_RIGHTS file descriptor passing (pretty complicated) or, if you have a Linux-style /proc filesystem try this:

xterm -S4/3 3<>/proc/$PID_OF_YOUR_OTHER_PROGRAM/fd/3


标准输入,&GT; 为标准输出,<$ C $ &LT:你以前可能看到过壳重定向操作符C> 2 - ; 为标准错误(文件描述符2)。正在运行结束也许你也看到了其他文件描述符输入或输出像的东西第3版; inputfile中4是H. OUTPUTFILE 。井 3';&gt;此处操作符是另一个。它打开读/写模式文件描述符3。和 / proc /进程/ FD / NUM 是访问另一个进程打开的文件的便捷方式。

' You've probably seen shell redirection operators before: < for stdin, > for stdout, 2> for stderr (file descriptor 2). Maybe you've also seen other file descriptors being opend for input or output with things like 3<inputfile 4>outputfile. Well the 3<> operator here is another one. It opens file descriptor 3 in read/write mode. And /proc/PID/fd/NUM is a convenient way to access files opened by another process.

我不知道这个问题的其余部分。我还没有尝试过使用的xterm的这种模式。

I don't know about the rest of the question. I haven't tried to use this mode of xterm before.

OK,与的/ proc 的伎俩是一个坏主意。这相当于的/ dev / ptmx设置新开,创建一个新的无关PTY。

OK, the trick with /proc was a bad idea. It's equivalent to a fresh open of /dev/ptmx, creating a new unrelated pty.

您将不得不作出的xterm您的pty创造计划的孩子。

You're going to have to make the xterm a child of your pty-creating program.

下面是我用来探索功能测试程序。这是草率的,但它揭示了一些有趣的事情。一个有趣的事情是,成功的xterm初始化后,将它的窗口ID pty主。这是你需要去处理。它表现为在TTY线路输入的实际用户输入开始之前。

Here's the test program I used to explore the feature. It's sloppy but it revealed some interesting things. One interesting thing is that xterm writes its window ID to the pty master after successful initialization. This is something you'll need to deal with. It appears as a line of input on the tty before the actual user input begins.

另一个有趣的事情是,xterm的(在Debian版本至少)崩溃,如果您使用 -S的/ dev /尽管这是专门的PTS / 2/3的在手册页提到允许的格式。

Another interesting thing is that xterm (the version in Debian at least) crashes if you use -S/dev/pts/2/3 in spite of that being specifically mentioned in the man page as an allowed format.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

int main(void)
{
  int master;
  char *slavename, window[64], buf[64];
  FILE *slave;

  master = posix_openpt(O_RDWR);

  grantpt(master);
  unlockpt(master);
  slavename = ptsname(master);

  printf("master: %d\n", master);
  printf("slavename: %s\n", slavename);

  snprintf(buf, sizeof buf, "-S%s/%d", strrchr(slavename,'/')+1, master);
  if(!fork()) {
    execlp("xterm", "xterm", buf, (char *)0);
    _exit(1);
  }
  slave = fopen(slavename, "r+");
  fgets(window, sizeof window, slave);
  printf("window: %s\n", window);

  fputs("say something: ", slave);
  fgets(buf, sizeof buf, slave);
  fprintf(slave, "you said %s\nexiting in 3 seconds...\n", buf);
  sleep(3);
  return 0;
}

这篇关于C:设置和伪终端使用xterm的开放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆