Mik*_*ike 7 c linux terminal multithreading pthreads
似乎有很多关于SO的问题很接近,但并不是我正在寻找的.我正在尝试查看是否有办法打开一个新的终端窗口(Linux),使用我的主程序中的线程/子进程,并让该线程/子进程拥有新窗口.
只是对完整目标的概述:我将要有一个主程序,我将启动并将通过输入stdin,如果我选择输入"启动帮助器"它将产生一个新的终端窗口,它本身可以与user(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.
}
Run Code Online (Sandbox Code Playgroud)
对此的要求是松散的,它不必是可移植的,这只是一个让我的生活更轻松的Linux工具.它必须是C代码,所以没有shell脚本,除非该脚本可以从C运行.任何帮助甚至其他想法都表示赞赏.
编辑:
我知道在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");
Run Code Online (Sandbox Code Playgroud)
手柄正确打开,但终端中没有显示任何内容.