Sim*_*son 20 c linux operating-system
我想从我的c程序中启动一个进程,但我不想等待该程序完成.我可以使用system()启动该进程,但始终等待.有没有人知道一个"非阻塞"版本会在进程启动后立即返回?
[编辑 - 附加要求]当原始进程执行完毕后,子进程需要继续运行.
Dou*_* T. 19
一个选项是在系统调用中,执行以下操作:
system("ls -l &");
Run Code Online (Sandbox Code Playgroud)
&命令行末尾的参数会分叉您启动的任务.
Fre*_*ory 15
为什么不使用fork()
和exec()
,根本不打电话waitpid()
?
例如,您可以执行以下操作:
// ... your app code goes here ...
pid = fork();
if( pid < 0 )
// error out here!
if( !pid && execvp( /* process name, args, etc. */ )
// error in the child proc here!
// ...parent execution continues here...
Run Code Online (Sandbox Code Playgroud)