调用 system() 后程序不执行任何操作 [英] program not executing anything after a call to system()
问题描述
我正在通过系统(命令)调用调用命令.但是在这个 system() 调用之后没有其他代码被执行.
为什么会这样?我认为,system() 会为命令"执行创建一个子进程,而我的程序(命令"-子进程的父进程)将在此之后继续执行代码.
我没有正确理解 system() 吗?
代码:
printf("before
");system("tail -f 文件名");/* 长寿 - 永不返回 */printf("在
之后");
在这里,我在打印后没有看到.
system(3)
函数使您的进程等待子进程的完成.
编辑 0:
你必须使用经典的一对 fork(2)
和 execve(2)
用于您想要做什么.您还可以检查您的 C 库是否提供 POSIX spawn(3)
.>
编辑 1:
查看waitpid(2)
让父母在身边.
I am calling a command via system(command) call. But no other code is executed after this system() call.
Why is so? I thought, system() would create a child process for "command" execution and my program (parent of "command"-child) will continue executing code after that.
Am I not understanding system() correctly?
code:
printf("before
");
system("tail -f filename"); /* long lived - never returns */
printf("after
");
Here, I do not see after getting printed ever.
The system(3)
function causes your process to wait for the completion of the child.
Edit 0:
You have to use the classic pair of fork(2)
and execve(2)
for what you want to do. You might also check whether your C library provides POSIX spawn(3)
.
Edit 1:
Look into waitpid(2)
to keep the parent around.
这篇关于调用 system() 后程序不执行任何操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!