gpt4 book ai didi

c - 使用线程打开 2 个控制台时程序卡住

转载 作者:太空宇宙 更新时间:2023-11-04 04:46:31 25 4
gpt4 key购买 nike

我正在尝试用 C 编写一个程序,我必须打开另一个控制台,上面有其他程序,当我这样做时,第一个程序会卡住,直到我关闭第二个程序。我一直在寻找没有结果的答案。

这是我知道失败的代码。

system("setterm -term linux -back white -fore black -clear");
char line[LEN];

system("cd ~/Desktop/ && gcc -o Rover Rover.c -lpthread");
FILE *cmd = popen("pidof -s Rover", "r");

fgets(line, LEN, cmd);
pid_t pid = strtoul(line, NULL, 10);
pclose(cmd);

printf("check");
if(pid == 0){
system("xterm -e ./Rover");
sleep(1);

cmd = popen("pidof -s Rover", "r");
fgets(line, LEN, cmd);
pid = strtoul(line, NULL, 10);fflush(stdout);
//pclose(cmd2);
system("\n");
fflush(stdout);
}

最佳答案

问题出在

system("xterm -e ./Rover");

此调用使终端调用 xterm 然后等待它。

要验证它,只需从您的终端运行 xterm,您会注意到您自己的终端将卡住。

我能想到的一个解决方案是创建一个子进程并从那里调用 xterm。

pid_t test = fork();
if(!test){
system("xterm -e ./Rover");
} else {
//Rest of your task.

}

关于c - 使用线程打开 2 个控制台时程序卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20321897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com