How to handle execvp(…) errors after fork()?

后端 未结 6 1415
孤街浪徒
孤街浪徒 2020-12-08 08:45

I do the regular thing:

  • fork()
  • execvp(cmd, ) in child

If execvp fails because no cmd is found, how can I notice this error in parent pr

6条回答
  •  醉酒成梦
    2020-12-08 09:26

    1) Use _exit() not exit() - see http://opengroup.org/onlinepubs/007908775/xsh/vfork.html - NB: applies to fork() as well as vfork().

    2) The problem with doing more complicated IPC than the exit status, is that you have a shared memory map, and it's possible to get some nasty state if you do anything too complicated - e.g. in multithreaded code, one of the killed threads (in the child) could have been holding a lock.

提交回复
热议问题