C 中的 execlp() 执行后不给出提示

发布于 2024-12-25 21:01:11 字数 449 浏览 4 评论 0原文

我尝试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);
}

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

I try to fork() a child which will run an ls command.

#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);
}

This works fine but the cursor gets stuck after the execution of the child. I have to press the enter key to get back the terminal. Why is that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞陪衬 2025-01-01 21:01:11

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

Your main process exits before the child process is done. Wait for the child process to exit using wait() or waitpid().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文