2011-12-22 66 views
0

我想出了這樣的事情..更改控制終端

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

+0

至於第一部分:你總是可以只放在客戶端(例如,'客戶端;讀取foo')之後暫停以保持終端打開。至於推出一個終端,並獲得其「pty」......我不知道。 – BRFennPocock 2011-12-22 19:59:42

回答

0

我想你應該使用其父斷開孩子3210。

From Wiki

系統調用setpgid()是用來設置進程組的過程的ID,從而無論是在加入過程到現有的處理組,或的會話中創建新的進程組過程成爲新創建的組的過程組組長。 POSIX禁止在具有該標識符的進程組仍然存在的情況下(即進程組的引導者已經退出,但該組中的其他進程仍然存在的情況下)重新使用進程ID。從而保證流程不會意外成爲流程組組長。

0

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"); 
}