그래서, 내가하려고하는 것은 자식 프로세스를 생성하고 execl을 실행하고 ls를 실행하는 것이다. 그런 다음 ls가 완료되면 다른 하위 프로세스를 작성하고 파일에 cat 명령을 실행하게하십시오. 두 번째 대기 (pid)는 완료되기 전에 두 번째 하위 프로세스가 완료되기를 기다리는 것 같지 않습니다. 여기에 내 코드와 출력부모 프로세스가 두 번째 대기하지 않는 이유는 무엇입니까? C/Unix
#include <fcntl.h>
int main(){
int pid;
printf("In parent process, creating child now...\n");
pid = fork();
if (pid==0){
printf("Now in child process...\n");
execl("/bin/ls","ls","-l",(char*)0);
}
wait(pid);
printf("ls child process complete\n");
printf("in parent process\n");
printf("Creating another child process\n");
pid=fork();
if(pid==0){
execl("/bin/cat","cat","f1",(char*)0);
}
wait(pid);
return 0;
}
것은 여기 내 출력
In parent process, creating child now...
Now in child process...
"Contents of ls"
ls child process complete
in parent process
creating another child process
[[email protected] ~]$ "Contents of file" *cursor*
부모 프로세스가 두 번째 아이가 완료되기 전에 완료하는 것입니다. 한 번에 한 명의 자녀 만 존재합니다. "파일 내용"은 [username @ host ~] $ 프롬프트 앞에 나타납니다. 나는 내가 기다리거나 잘못 된 약물 할당을 잘못하고 있다고 생각한다. 미리 감사드립니다!
예를 재현 할 수 없습니다. 적절한 헤더를 추가 한 후 코드가 잘 실행되는 것 같습니다. – merlin2011
때로는 실제로 실제로 제대로 인쇄되지만 대부분의 경우 인쇄되지 않습니다. – JohnnyBoy