【问题标题】:execlp() in C doesn't give prompt back after executionC 中的 execlp() 执行后不返回提示
【发布时间】:2012-01-09 15:04:44
【问题描述】:

我尝试 fork() 一个将运行ls 命令的孩子。

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

int main() {

    if (fork()==0){ //child
        execlp("ls", "ls", "-l", (char*)0);
        exit(1);
    }

    fflush(stderr); //doesn't fix my problem
    fflush(stdout); //doesn't fix my problem
    exit(0);
}

这工作正常,但光标在孩子执行后卡住了。我必须按回车键才能取回终端。这是为什么呢?

【问题讨论】:

    标签: terminal exec fork prompt


    【解决方案1】:

    您的主进程在子进程完成之前退出。使用 wait() 或 waitpid() 等待子进程退出。

    【讨论】:

    • 如果父母和孩子退出,我如何获得孩子的状态?我需要使用waitpid吗?我可以在何时何地使用它?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 2011-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多