C 变更控制终端

C 变更控制终端,c,linux,ubuntu,process,terminal,C,Linux,Ubuntu,Process,Terminal,我想出了这样的主意 int main (unsigned argc, char **argv) { printf("***this is the original terminal window!!!***\n"); if(!fork()){//child system("gnome-terminal -e ./client"); } else{ printf("this is the parent, printi

我想出了这样的主意

int main (unsigned argc, char **argv)
{
    printf("***this is the original terminal window!!!***\n");
    if(!fork()){//child
        system("gnome-terminal -e ./client");
    }
    else{         
        printf("this is the parent, printing in the original terminal window\n");
    }
}
它将打开一个新的终端窗口,其中执行./client。唯一的问题是./client事件结束后,新的终端窗口会自动关闭。我如何在不做一些愚蠢的事情的情况下解决这个问题,比如在./client上使用(;)for(;)?此外,整个方法比最优解要小

我真正想做的是:

int main (unsigned argc, char **argv)
{
    printf("***this is a generator!!!***\n");
    if(!fork()){//child
        system("gnome-terminal or wathever"); //the solution must be here right??
        printf("this get's printed on the new window and whatever i do on the\
                child process get's done there too")
        //and the window won't close automatically
    }
    else{         
        printf("this is the parent, printing in the original terminal window\n");
    }
}
它将更加灵活,我只是不希望从另一个文件中执行
exec()


我使用的是Ubuntu 11.10,语言是C。

我认为应该使用
setpgid()
断开孩子与父母的连接

:


系统调用setpgid()用于设置流程的流程组ID,从而将流程加入现有流程组,或在流程会话中创建新流程组,流程将成为新创建组的流程组组长。POSIX禁止在仍存在具有该标识符的流程组的情况下(即,流程组的负责人已退出,但组中仍存在其他流程)重复使用流程ID。因此,它可以保证进程不会意外地成为进程组的领导者。

xterm
有一个
-S
选项,允许它从预先存在的进程继承从属终端,因此我认为您可以执行以下操作

main(){
    if(!fork()){
        int master, slave;
        char slvname[BUF_SIZ];
        openpty(&master, &slave, slvname, NULL, NULL);
        if(!fork()){
            execlp("xterm", "xterm", "-S", slvname, NULL);
        }
        write(master, "new term\n", 9); //or do you write to the slave?
    }
    printf("original term\n");
}

至于第一部分:您可以始终在客户端(例如,
client;read foo
)后暂停,以保持终端打开。至于启动终端并访问其
pty
…我不知道。