gpt4 book ai didi

c - 使用 system() 打开 2 个终端并选择默认输出

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

我有一个像这样的简单程序

int main(void) {

system("gnome-terminal");
puts("terminal 1");
system("gnome-terminal");
puts("terminal 2");

return EXIT_SUCCESS;
}

在运行时:只打开第一个终端,只有当我关闭它时,程序才会继续,在控制台打印并打开第二个。

如何打开它们?(当第一个打开时不停止我的程序的执行)

如何在我的程序中有选择地在两个终端中打印?(puts("something"); 在第二个终端)

谢谢

最佳答案

这是不可能的,因为 system 会阻塞直到执行的程序结束,一种可能的解决方案是使用 fork()

#include <stdio.h>
#include <unistd.h>

int main(void)
{
int i;

for (i = 0 ; i < 2 ; ++i)
{
if (fork() == 0)
{
printf("terminal %d\n", 1 + i);
system("gnome-terminal");
}
}
return EXIT_SUCCESS;
}

如果你想与执行的程序通信,阅读popen() .您可能也对 execv() 感兴趣和家人。

关于c - 使用 system() 打开 2 个终端并选择默认输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27911328/

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