fork+exec without atfork handlers

前端 未结 2 764
别那么骄傲
别那么骄傲 2020-12-21 12:30

I have a library which registers an atfork handler (via pthread_atfork()) which does not support multiple threads when fork() is calle

2条回答
  •  猫巷女王i
    2020-12-21 12:55

    Yes. The following should work on Linux (and, I think, all glibc based platforms):

    #define _GNU_SOURCE
    #include 
    #include 
    
    ...
      syscall(SYS_fork);
    

    This bypasses the library and directly calls the system call for fork. You might run into trouble if your platform does not implement fork as a single system call. For Linux, that simply means that you should use clone instead.

    With that in mind, I'm not sure I'd recomment doing that. Since you're a library, you have no idea why someone registered an atfork. Assuming it's irrelevant is bad programming practice.

    So you lose portability in order to do something that may or may not break stuff, all in the name of, what? Saving a few function calls? Personally, I'd just use fork.

提交回复
热议问题