複数のプロセスをフォークし、各子プロセスに独自の端末ウィンドウを割り当ててIPCを簡単に実演できるようにしたい。 フォークは正常に動作し、同じターミナルで子プロセスを実行するとうまく動作します。しかし、それぞれの子プロセスを作るために は、自端末のウィンドウを持って、私は各子プロセスに新しい端末ウィンドウを割り当てる方法
execl("/usr/bin/xterm", "xterm", "-e", "yourprogram", NULL);
を行うプログラムが新しいウィンドウで実行されますが、そのPIDは、プロセスをフォーク親が示すものとは異なります。私は間違って何をしていますか?
おかげ
EDIT1 - これが私の主な機能(親プロセス)です。私は4つの子プロセスをforkします。それぞれの子プロセスが独自の端末ウィンドウを持つようにしたい。しかし、子プロセスが終了するだけで、別の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:メインの追加出力:
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
ここに回答がある可能性があります: http://stackoverflow.com/questions/11040334/linuxhow-to-fork-not-exec-a-new-process-in-a-new-terminal-ウィンドウ –
その解決策を見ましたが、Cコードは使用しません。私はCを使用する必要があります – neoprogrammer
あなたは何も間違っていると推測します。端末は、別のサブプロセスで必要なコマンドを実行します。 – jdarthenay