没有atfork处理程序的fork + exec [英] fork+exec without atfork handlers

查看:46
本文介绍了没有atfork处理程序的fork + exec的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个库,该库注册一个atfork处理程序(通过pthread_atfork()),该处理程序在调用fork()时不支持多个线程.就我而言,我不需要派生环境,因为我想要的只是在fork()之后立即调用exec().因此,我需要fork(),但没有任何atfork处理程序.那可能吗?我会错过任何重要的案例吗?

I have a library which registers an atfork handler (via pthread_atfork()) which does not support multiple threads when fork() is called. In my case, I don't need the forked environment to be usable because all I want is to call exec() right after the fork(). So, I want the fork() but without any atfork handlers. Is that possible? Do I miss any important edge cases?

对于背景信息,该库是OpenBlas,在此处中描述了该问题,并且此处.

For background info, the library is OpenBlas, the issue is described here and here.

推荐答案

您可以使用

You could use vfork() (NPTL implementation doesn't call fork handlers). Although POSIX has removed vfork from the standard, it's likely available on your implementation.

使用pthread_atfork(3)建立的分叉处理程序在以下情况下不被调用 使用NPTL线程库调用的多线程程序 vfork().在这种情况下,在使用以下程序的程序中调用了分叉处理程序 LinuxThreads线程库. (请参阅pthreads(7) Linux线程库的说明.)

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.手册页上说:

Or, posix_spawn(). This is similar to vfork. Man page says:

根据POSIX,它未指定在调用posix_spawn()时是否调用通过pthread_atfork(3)建立的fork处理程序.在glibc上,仅在使用fork(2)创建子项时才调用fork处理程序.

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_clone. SYS_clone是用于在Linux上创建线程和进程的系统调用号.因此syscall(SYS_clone, SIGCHLD, 0);应该可以工作,只要您立即执行.

Or, syscall and directly use SYS_clone. SYS_clone is the system call number used to create threads and processes on Linux. So syscall(SYS_clone, SIGCHLD, 0); should work, provided you would exec immediately.

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

syscall(SYS_fork); (as answered by Shachar) would likely work too. But note that SYS_fork not available on some platforms (e.g., aarch64, ia64). SYS_fork is considered as obsolete in Linux and it's only there for backward compatibility and Linux kernel uses SYS_clone for creating all "types" of processes.

(注意:这些选项主要限于glibc/Linux).

(Note: These options are mostly limited to glibc/Linux).

这篇关于没有atfork处理程序的fork + exec的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆