【问题标题】:Why doesn't the last line of the child process print?为什么子进程的最后一行不打印?
【发布时间】:2015-08-31 03:06:00
【问题描述】:
#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 的最后一行没有打印出来,我不知道为什么。

【问题讨论】:

  • 您是否真的阅读了execl 的手册页?它会告诉你所有你需要知道的——它不会成功返回。

标签: c fork exit wexitstatus


【解决方案1】:

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

exec() 系列函数用新的进程映像替换当前进程映像。 ....

返回值

exec() 函数仅在发生错误时返回。回报 值为-1,设置errno表示错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-21
    • 1970-01-01
    • 2023-02-26
    • 2013-09-28
    • 1970-01-01
    相关资源
    最近更新 更多