如何退出子进程

编程入门 行业动态 更新时间:2024-10-15 16:24:48
本文介绍了如何退出子进程 - _exit()与出口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

考虑这个code片断:

Consider this code snippet:

pid_t cpid = fork(); if (cpid == -1) { perror("fork"); exit(EXIT_FAILURE); } if (cpid == 0) { // in child execvp(argv[1], argv + 1); perror("execvp"); _exit(EXIT_FAILURE); } // in parent

我该如何退出,如果execvp返回子进程?我将使用的exit()或_exit()?

How shall I exit the child process if execvp returns? Shall I use exit() or _exit()?

推荐答案

您一定要使用 _Exit()。 退出()打电话给你添加了的atexit函数()并删除与 TMPFILE创建的文件()。由于父进程是真的想要这些东西做,当它存在,你应该叫一个 _Exit(),它没有做以上。

You should definitely use _Exit(). exit() calls the functions you added with atexit() and deletes files created with tmpfile(). Since the parent process is really the one that wants these things done when it exists, you should call _Exit(), which does none of these.

通知 _Exit()以大写E. _exit(2)很可能不是你想要怎样称​​呼直。 退出(3)和 _Exit(3)将调用此为您服务。如果你没有 _Exit(3),那么, _exit()是你想要的。

Notice _Exit() with a capital E. _exit(2) is probably not what you want to call directly. exit(3) and _Exit(3) will call this for you. If you don't have _Exit(3), then yes, _exit() is what you wanted.

更多推荐

如何退出子进程

本文发布于:2023-05-29 03:36:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/335862.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:进程

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!