나는 다양한 프로세스를 생성하고있다. 지금까지 너무 좋아. 모든 아이들이 끝날 때까지 부모님을 기다리고 있습니다. 나는 많은 옵션들 (아래에 나열된 것과 같은)을 가지고 놀았지만, 부모 중 하나가 기다린다. 그러나 Enter 키를 눌러서 쉘로 돌아 가야한다. (어떤 자식은 부모 뒤에있는 것을 의미 하는가?) 부모는 절대 반환하지 않는다. 껍질. 어떤 아이디어? 더 많은 도움을 얻을 수있는 곳을 가리키는 포인터? 감사합니다여러 프로세스를 포킹하고 상위 프로세스를 포기하도록 (C에서)
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#define READ_END 0
#define WRITE_END 1
int main (int argc, char **argv)
{
pid_t pid;
int fd[2];
int fd2[2];
pipe(fd);
pipe(fd2);
for (int i=0; i<3; i++) {
pid=fork();
if (pid==0 && i==0) {
//never uses fd2, so close both descriptors
close(fd2[READ_END]);
close(fd2[WRITE_END]);
printf("i'm the child used for ls \n");
close(fd[READ_END]); /*close read end since I don't need it */
dup2(fd[WRITE_END], STDOUT_FILENO);
close(fd[WRITE_END]);
execlp("ls", "ls", "-hal", NULL);
break; /*exit for loop to end child's code */
}
else if (pid==0 && i==1) {
printf("i'm in the second child, which will be used to run grep\n");
close(fd[WRITE_END]);
dup2(fd[READ_END], STDIN_FILENO);
close(fd[READ_END]);
close(fd2[READ_END]);
dup2(fd2[WRITE_END], STDOUT_FILENO);
close(fd2[WRITE_END]);
execlp("grep", "grep","p",NULL);
break;
}
else if (pid==0 && i==2) {
//never uses fd so close both descriptors
close(fd[READ_END]);
close(fd[WRITE_END]);
printf("i'm in the original process which will be replaced with wc\n");
close(fd2[WRITE_END]);
dup2(fd2[READ_END], STDIN_FILENO);
close(fd2[READ_END]);
printf("going to exec wc\n");
execlp("wc","wc","-w",NULL);
break;
}
else {
//do parenty things
}
}
wait(NULL);
while (1){
wait(NULL);
if(errno== ECHILD) {
printf("all children ended\n");
break;
}
}
close(fd[READ_END]);
close(fd[WRITE_END]);
close(fd2[READ_END]);
close(fd2[WRITE_END]);
return 0;
}
포맷이 잘못되어 코드를 수정 해 주실 수 있습니까? – t0mm13b
모두 고정 :) thx – theprole