gpt4 book ai didi

c - 如何为每个子进程分配一个新的终端窗口

转载 作者:太空狗 更新时间:2023-10-29 12:12:45 24 4
gpt4 key购买 nike

我想 fork 多个进程并为每个子进程分配它自己的终端窗口,以便可以轻松演示 IPC。 fork 进行得很好,如果我在同一个终端上运行子进程,它运行良好。然而,为了让每个子进程都有自己的终端窗口,我这样做了

execl("/usr/bin/xterm", "xterm", "-e", "yourprogram", NULL);

程序在新窗口中运行,但其 PID 与 fork 进程的父进程显示的不同。我做错了什么?

谢谢

edit1 - 这是我的主要功能(父进程)。我 fork 了 4 个子进程。我希望每个子进程都有自己的终端窗口。然而,子进程刚刚退出,一个具有不同 PID 的新进程继续在新终端中运行。

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/shm.h>

int main()
{

pid_t pid[4];
int i = 0;
int status;

//Fork four new processes
for(i=0; i<4; i++)
{
pid[i] = fork();

if(pid[i] == 0 && i == 0)
{
execl("/usr/bin/xterm", "xterm", "./child1", NULL);
exit(1);
}
else if(pid[i] == 0 && i == 1)
{
execl("/usr/bin/xterm", "xterm", "./child2", NULL);
exit(1);
}
else if(pid[i] == 0 && i == 2)
{
execl("/usr/bin/xterm", "xterm", "./child3", NULL);
exit(1);
}
else if(pid[i] == 0 && i == 3)
{
execl("/usr/bin/xterm", "xterm", "./child4", NULL);
exit(1);
}
else
{
//Parent process
printf("The main function has forked a process with pid: %d\n", pid[i]);
}
}

for(i=0;i<4;i++)
{
status = waitpid(pid[i], NULL, 0);
if(status == pid[i])
printf("%d: Process Terminated Successfully\n", pid[i]);
else
{
perror("waitpid");
exit(1);
}
}

return 1;
}

edit2 - 添加了 ps -u 输出:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
dell-pc 3024 0.1 0.0 26872 5480 pts/0 Ss 16:54 0:00 bash
dell-pc 3038 0.0 0.0 4200 632 pts/0 S+ 16:54 0:00 ./main
dell-pc 3039 22.5 0.1 109240 11116 pts/0 S+ 16:54 0:01 xterm ./child1
dell-pc 3040 26.1 0.1 109240 11268 pts/0 R+ 16:54 0:02 xterm ./child2
dell-pc 3041 28.7 0.1 109240 11180 pts/0 S+ 16:54 0:02 xterm ./child3
dell-pc 3042 27.0 0.1 109240 11288 pts/0 S+ 16:54 0:02 xterm ./child4
dell-pc 3044 4.1 0.0 4200 648 pts/24 Ss+ 16:55 0:00 child3
dell-pc 3046 3.7 0.0 4200 680 pts/26 Ss+ 16:55 0:00 child4
dell-pc 3048 3.8 0.0 4200 792 pts/25 Ss+ 16:55 0:00 child2
dell-pc 3050 3.3 0.0 4200 660 pts/14 Ss+ 16:55 0:00 child1
dell-pc 3060 2.0 0.0 26816 5412 pts/27 Ss 16:55 0:00 bash
dell-pc 3072 0.0 0.0 22648 2688 pts/27 R+ 16:55 0:00 ps -u

edit3: 添加了 main 的输出:

The main function has forked a process with pid: 3491
The main function has forked a process with pid: 3492
The main function has forked a process with pid: 3493
The main function has forked a process with pid: 3494
3491: Process Terminated Successfully
3492: Process Terminated Successfully
3493: Process Terminated Successfully
3494: Process Terminated Successfully

最佳答案

我做了一个像你的程序(命名为 stackoverflow),在 xterm 中执行 vi,当它运行时我打开第三个 xterm 来运行 ps -u .输出是:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
osboxes 1713 0.0 0.2 6588 4756 pts/0 Ss 18:56 0:00 bash
osboxes 1780 0.0 0.2 6508 4484 pts/1 Ss 19:12 0:00 bash
osboxes 1836 88.4 0.0 2020 532 pts/0 R+ 19:21 0:29 ./stackoverflow
osboxes 1837 0.1 0.4 12844 8952 pts/0 S+ 19:21 0:00 /usr/bin/xterm -e vi stackoverflow.txt
osboxes 1839 0.0 0.1 6072 3536 pts/2 Ss+ 19:21 0:00 vi stackoverflow.txt
osboxes 1840 0.0 0.1 4772 2452 pts/1 R+ 19:22 0:00 ps -u

程序的输出是:

PID=1836
child PID=1837

因此 child 仍在运行xterm命令。它创建了另一个运行 vi 的子进程(pid 1839)。

关于c - 如何为每个子进程分配一个新的终端窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36552645/

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