在SO上似乎有很多与我所需不太相同但又很接近的问题。我正在尝试查看是否有一种方法可以从我的主程序中使用线程/子进程打开一个新的终端窗口(Linux),并使该线程/子进程拥有该新窗口。
总体目标概述:我将启动一个主程序,并通过 stdin
接收输入,如果我选择输入“启动助手”,它将生成一个新的终端窗口,该窗口本身可以与用户交互(stdin/stdout)。
因此,我想要做的是让主程序调用线程,让线程使用/拥有新的终端窗口,然后当线程消失并死亡时,关闭该窗口。
我知道这段代码不正确,但从概念上讲,我想要类似于这样的东西:
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.
}
这个要求很宽松,不需要可移植性,只是一个小型Linux工具,让我的生活更轻松。必须使用C代码,所以除非该脚本可以从C中运行,否则不要使用shell脚本。感谢任何帮助或其他想法。
编辑:
我知道在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");
手柄正确打开,但终端没有显示任何内容。
fork()
、execv()
、wait()
代码。线程是不必要的。 - Daveopen
和write
代替。还可以尝试使用gnome-terminal -e 'sleep 36000'
。 - zwol