gpt4 book ai didi

c - 为什么子进程的最后一行不打印?

转载 作者:行者123 更新时间:2023-11-30 20:05:20 24 4
gpt4 key购买 nike

#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>


int main( ) {
pid_t pid;
int status = -1;

if ((pid = fork()) != 0) {
printf("Father process wait child PID=%d\n", pid);
wait(&status);
printf("Child finish with status: %d\n",WEXITSTATUS(status));
exit(0);
}
else {
printf("Child process running...\n");
execl("/bin/ls","ls", "-la", NULL);
printf("Child ending...\n");
}
}

编译此代码时,else 的最后一行不会打印,我不知道为什么。

最佳答案

http://linux.die.net/man/3/execl

The exec() family of functions replaces the current process image with a new process image. ....

Return Value

The exec() functions only return if an error has occurred. The return value is -1, and errno is set to indicate the error.

关于c - 为什么子进程的最后一行不打印?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32303450/

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