C 中的 execlp() 执行后不给出提示
我尝试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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的主进程在子进程完成之前退出。使用 wait() 或 waitpid() 等待子进程退出。
Your main process exits before the child process is done. Wait for the child process to exit using wait() or waitpid().