#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
int main(void)
{
pid_t Checksum_pid = fork();
if (Checksum_pid < 0)
printf("Fork Failed\n");
else if (Checksum_pid == 0)
{
printf("\nInside Child Process before execl");
//execl("/bin/sleep", "/bin/sleep", "2", NULL);
execl("/bin/ls","ls",(char *)NULL) ;
//exit(EXIT_FAILURE);
printf("\nInside Child Process after execl\n");
exit(EXIT_FAILURE);
}
else
{
int childStatus;
printf("\nInside Parent Process");
sleep(5);
pid_t returnValue = waitpid(Checksum_pid, &childStatus, WNOHANG);
if (returnValue > 0)
{
if (WIFEXITED(childStatus))
printf("\nChild Exit Code: %d\n", WEXITSTATUS(childStatus));
else
printf("\nChild Exit Status: 0x%.4X\n", childStatus);
}
else if (returnValue == 0)
printf("\nChild process still running\n");
else
{
if (errno == ECHILD)
printf("\nError ECHILD!!\n");
else if (errno == EINTR)
printf("\nError EINTR!!\n");
else
printf("\nError EINVAL!!\n");
}
printf("\nParent: I am about to finish\n");
}
return 0;
}
출력 : 마지막으로 내가 0으로 시도인쇄 출력에 표시되지 않습니다
kth4kor-2:~/bosch/Test1$ ./a.out
Inside Parent Process
Child process still running
Parent: I am about to finish
[email protected]:~/bosch/Test1$ a.out ___waitpid_test1 waitpid_test1~ waitpid_test1.c waitpid_test1.c~ waitpid_test2.c waitpid_test2.c~
: I (5) 부모에 다음 출력을 잠을 제거하면 이제
kth4kor-2:~/bosch/Test1$ ./a.out a.out ___waitpid_test1 waitpid_test1~ waitpid_test1.c waitpid_test1.c~ waitpid_test2.c waitpid_test2.c~
Inside Parent Process Child Exit Code: 0
Parent: I am about to finish
WNOHANG 대신에 waitpid에 대한 세 번째 주장은 어린이의 지문과 함께 아래 출력을 주었다.
kth4kor-2:~/bosch/Test1$ ./a.out
a.out ___waitpid_test1 waitpid_test1~ waitpid_test1.c waitpid_test1.c~ waitpid_test2.c waitpid_test2.c~
Inside Parent Process
Child Exit Code: 0
Parent: I am about to finish
누군가 execl을 실행하고 실행하기 전에 프로세스의 동작을 이해할 수 있습니까?
@Joachim이 언급 한 해결책 이외에'setbuf (stdout, NULL); "을 사용하여 버퍼링을 비활성화하거나'stderr'에서 메시지를 출력 할 수 있습니다. –
printf ("\ n execl 이전의 하위 프로세스 내부 \ n"); 잘 작동하고 있습니다 ... – kapilddit