c - fork+exec 没有 atfork 处理程序

标签 c linux fork exec posix

我有一个注册 atfork 处理程序(通过 pthread_atfork())的库,当 fork() 为叫。就我而言,我不需要 fork 环境可用,因为我只想在 fork() 之后调用 exec() 。因此,我想要 fork() 但没有任何 atfork 处理程序。那可能吗?我是否错过了任何重要的边缘情况?

有关背景信息,库是 OpenBlas,问题已描述 herehere .

最佳答案

您可以使用vfork() (NPTL 实现不调用 fork 处理程序)。尽管 POSIX 已从标准中删除了 vfork,但它可能在您的实现中可用。

Fork handlers established using pthread_atfork(3) are not called when a multithreaded program employing the NPTL threading library calls vfork(). Fork handlers are called in this case in a program using the LinuxThreads threading library. (See pthreads(7) for a description of Linux threading libraries.)

或者,posix_spawn() 。这与vfork类似。手册页显示:

According to POSIX, it unspecified whether fork handlers established with pthread_atfork(3) are called when posix_spawn() is invoked. On glibc, fork handlers are called only if the child is created using fork(2).

或者,syscall并直接使用SYS_cloneSYS_clone 是用于在 Linux 上创建线程和进程的系统调用号。因此,如果您立即执行,syscall(SYS_clone, SIGCHLD, 0); 应该可以工作。

syscall(SYS_fork); (由 Shachar 回答)可能也会起作用。但请注意,SYS_fork 在某些平台上不可用(例如 aarch64、ia64)。 SYS_fork 在 Linux 中被认为是过时的,它只是为了向后兼容而存在,Linux 内核使用 SYS_clone 来创建所有“类型”的进程。

(注意:这些选项大多仅限于 glibc/Linux)。

关于c - fork+exec 没有 atfork 处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46810597/

相关文章:

c - 为什么我在将参数传递给不应接受任何传入参数的函数时没有收到警告?

mysql - 当csv文件在cell linux中有逗号时剪切

python - 退出并清理 python fork

c - 在 Linux 中 fork

c# - C# 中的 fork 概念

C int数组在传递时缩小

c++ - 有没有一种方法可以使用 C/C++ 程序检测网络路由器是否支持 DHCP?

c - 在单位球体上产生随机位移

linux - 用于 reaver 解锁 wps 锁定状态的 Bash 脚本

linux - 如何使用没有管道的三通