是否可以在C中打开一个带有线程的新Linux终端?

时间:2021-04-23 15:08:53

There seem to be a lot of question on SO that are close, but not quite what I'm looking for. I'm trying to see if there's a way to open a new terminal window (Linux), with a thread/child process from my main program, and have that thread/child process own the new window.

似乎有很多关于SO的问题很接近,但并不是我正在寻找的。我试图看看是否有办法打开一个新的终端窗口(Linux),从我的主程序中使用线程/子进程,并让该线程/子进程拥有新窗口。

Just an overview of the full objective: I'm going to have a main program that I will launch and will take input via stdin, if I select input to "start a helper" it will spawn a new terminal window which can itself interact with the user (stdin/stdout).

只是对完整目标的概述:我将有一个我将启动的主程序,并将通过stdin获取输入,如果我选择输入“启动帮助器”它将产生一个新的终端窗口,它本身可以与之交互用户(stdin / stdout)。

So what I want to do is have the main program call the thread, have the thread use/own the new terminal window, then have that window close when the thread goes away and dies.

所以我想做的是让主程序调用线程,让线程使用/拥有新的终端窗口,然后当线程消失并死亡时关闭该窗口。

I know this code doesn't work right, but conceptually, I'd like something like this:

我知道这段代码不能正常工作,但从概念上讲,我喜欢这样的东西:

void * Runit()
{
    system("gnome-terminal"); //Would like to get a handle to this window
    while(1)
      printf("I'm the thread!!!\n"); //Would like to get this printed to the new window
}

int main()
{
    pthread_t child;
    pthread_create(&child, NULL, Runit, NULL);
    sleep(10);
    return 0; //Would like the child & its window to go away now.
}

The requirements on this are loose, it does not have to be portable, this is just a little Linux tool to make my life easier. It does have to be C code, so no shell scripting unless that script can be run from C. Any help or even other ideas is appreciated.

对此的要求是松散的,它不必是可移植的,这只是一个让我的生活更轻松的Linux工具。它必须是C代码,所以没有shell脚本,除非该脚本可以从C运行。任何帮助甚至其他想法都表示赞赏。

EDIT:

I'm aware that in linux terminals have file handles /dev/pts/x and I have tried code like:

我知道在linux终端上有文件句柄/ dev / pts / x,我尝试过类似的代码:

system("gnome-terminal");
sleep(2); //let the file handle show up in /dev/pts
fp = fopen("/dev/pts/<new file handler number>");
fprintf(fp, "echo hi");

The handle opens correctly, but nothing is displayed in the terminal.

手柄打开正确,但终端中没有显示任何内容。

3 个解决方案

#1


2  

Both gnome-terminal and xterm allow you to run an arbitrary command once the terminal opens.

gnome-terminal和xterm都允许您在终端打开后运行任意命令。

I would recommend, therefore, that you write a helper program that knows how to communicate with your main program (via sockets, named pipes, or some other IPC mechanism). Your thread spawns the terminal program, passing it your helper program, which will run inside the terminal and communicate with the thread via the aforementioned IPC channel.

因此,我建议您编写一个帮助程序,该程序知道如何与主程序通信(通过套接字,命名管道或其他一些IPC机制)。你的线程产生终端程序,传递你的帮助程序,它将在终端内运行并通过上述IPC通道与线程通信。

#2


1  

The library libvte, specifically the vte_pty_* functions, may be capable of doing what you want.

库libvte,特别是vte_pty_ *函数,可能能够做你想要的。

The way you have phrased the question suggests that you do not understand how windows and terminal I/O work under Linux (or, actually, Unix in general), so I strongly recommend you read up on these things. Start with W. Richard Stevens' book Advanced Programming in the Unix Environment.

你提出这个问题的方式表明你不了解Windows和终端I / O在Linux下(或者实际上是Unix)的工作原理,所以我强烈建议你阅读这些内容。从W. Richard Stevens的“Unix环境中的高级编程”一书开始。

#3


0  

As I understand it, the application must be able to print into the terminal - but do you want it also to read from it? Should the terminal be controlled by the user?

据我了解,应用程序必须能够打印到终端 - 但你是否也希望它从它读取?终端应该由用户控制吗?

I'd manipulate with terminal's file descriptors, you should check from what file does the terminal obtain user input (it's probably NOT stdin, but some /dev/pts?), and to what file does the terminal write it's output, and you could capture it, and you could also write into it, causing the content to be displayed in the terminal. But beware: 1) The terminal itself will probably lauch shell (bash) - if you write to /dev/pts, it will be displayed in the terminal, but will not be passed to the bash process in the terminal - you can't remotely make commands in such temrinal, 2) I think that /dev/pts/x file will be created when the terminal starts, so you can't fork+dup+exec and have the terminal output captured 3) The process that will be ran in the terminal will print some output too - it will be not read from /dev/pts

我用终端的文件描述符进行操作,你应该检查终端获取用户输入的文件(它可能不是stdin,但有些/ dev / pts?),以及终端写入输出的文件,你可以捕获它,你也可以写入它,导致内容显示在终端中。但要注意:1)终端本身可能是lauch shell(bash) - 如果你写入/ dev / pts,它将显示在终端中,但不会传递给终端中的bash进程 - 你不能在这样的temrinal中远程创建命令,2)我认为/ dev / pts / x文件将在终端启动时创建,因此你不能fork + dup + exec并且终端输出被捕获3)这个过程将是在终端中运行也会打印一些输出 - 它不会从/ dev / pts读取

Maybe you need to write application in C which will just be ran in the terminal, and that application will communicate with your application which owns the terminal?

也许您需要在C中编写应用程序,它只能在终端中运行,并且该应用程序将与拥有该终端的应用程序通信?

#1


2  

Both gnome-terminal and xterm allow you to run an arbitrary command once the terminal opens.

gnome-terminal和xterm都允许您在终端打开后运行任意命令。

I would recommend, therefore, that you write a helper program that knows how to communicate with your main program (via sockets, named pipes, or some other IPC mechanism). Your thread spawns the terminal program, passing it your helper program, which will run inside the terminal and communicate with the thread via the aforementioned IPC channel.

因此,我建议您编写一个帮助程序,该程序知道如何与主程序通信(通过套接字,命名管道或其他一些IPC机制)。你的线程产生终端程序,传递你的帮助程序,它将在终端内运行并通过上述IPC通道与线程通信。

#2


1  

The library libvte, specifically the vte_pty_* functions, may be capable of doing what you want.

库libvte,特别是vte_pty_ *函数,可能能够做你想要的。

The way you have phrased the question suggests that you do not understand how windows and terminal I/O work under Linux (or, actually, Unix in general), so I strongly recommend you read up on these things. Start with W. Richard Stevens' book Advanced Programming in the Unix Environment.

你提出这个问题的方式表明你不了解Windows和终端I / O在Linux下(或者实际上是Unix)的工作原理,所以我强烈建议你阅读这些内容。从W. Richard Stevens的“Unix环境中的高级编程”一书开始。

#3


0  

As I understand it, the application must be able to print into the terminal - but do you want it also to read from it? Should the terminal be controlled by the user?

据我了解,应用程序必须能够打印到终端 - 但你是否也希望它从它读取?终端应该由用户控制吗?

I'd manipulate with terminal's file descriptors, you should check from what file does the terminal obtain user input (it's probably NOT stdin, but some /dev/pts?), and to what file does the terminal write it's output, and you could capture it, and you could also write into it, causing the content to be displayed in the terminal. But beware: 1) The terminal itself will probably lauch shell (bash) - if you write to /dev/pts, it will be displayed in the terminal, but will not be passed to the bash process in the terminal - you can't remotely make commands in such temrinal, 2) I think that /dev/pts/x file will be created when the terminal starts, so you can't fork+dup+exec and have the terminal output captured 3) The process that will be ran in the terminal will print some output too - it will be not read from /dev/pts

我用终端的文件描述符进行操作,你应该检查终端获取用户输入的文件(它可能不是stdin,但有些/ dev / pts?),以及终端写入输出的文件,你可以捕获它,你也可以写入它,导致内容显示在终端中。但要注意:1)终端本身可能是lauch shell(bash) - 如果你写入/ dev / pts,它将显示在终端中,但不会传递给终端中的bash进程 - 你不能在这样的temrinal中远程创建命令,2)我认为/ dev / pts / x文件将在终端启动时创建,因此你不能fork + dup + exec并且终端输出被捕获3)这个过程将是在终端中运行也会打印一些输出 - 它不会从/ dev / pts读取

Maybe you need to write application in C which will just be ran in the terminal, and that application will communicate with your application which owns the terminal?

也许您需要在C中编写应用程序,它只能在终端中运行,并且该应用程序将与拥有该终端的应用程序通信?