多线程程序和fork():替代或安全实现 [英] Multithreaded program and fork(): alternative or safe implementation

查看:266
本文介绍了多线程程序和fork():替代或安全实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在多线程Linux / C ++程序中,我想使用 SIGCHLD 使用 fork() >



在子进程中,我使用 open()创建两个新的文件描述符 sendfile() close(),然后子进程退出。



我计划使用 fork()来实现以下要求:



父进程中的线程应能够到


  1. 检测子进程的正常终止,在这种情况下应能够创建另一个对一系列文件执行 open()/ sendfile()/ close()

  2. 在特定事件的情况下杀死 sendfile() -child过程并检测有意终止以清除

对于需求1,我可以等待 sendfile()的结果。
需求2是为什么我想我需要首先使用 fork()



阅读以下帖子后





我认为我的解决方案可能不是一个好的解决方案。

$我的问题是:




  • 是否还有其他解决方案来实现要求2?

  • 或者我如何确保库调用 open(),close()和sendfile()将会正常?



更新:




  • 程序将在Busybox Linux / ARM上运行

  • 我假设我应该使用 sendfile()转移由于我阅读关于此主题的几个帖子。
    一个安全的方式来实现我的要求可以使用 fork() exec *() code> cp ,缺点是文件传输可能效率较低



> Update 2:




  • 每次只要因为我切换到 exec *() rsync 在特定事件子进程。但是程序需要在特定事件的情况下调用 rsync


解决方案

您可以使用线程,但强制终止线程通常会导致内存泄漏和其他问题。



我的linux体验有限,但我可能会尝试早期分叉程序,在它获得多线程之前。现在您有两个实例,可以安全地使用单线程实例来管理其他实例的启动和停止。


In a multithreaded Linux/C++-program, I want to use fork() with a signal handler for SIGCHLD.

In the child process I use open() to create two new file descriptors, sendfile() and close(), then the child exits.

I planned to use fork() to implement the following requirements:

The threads in the parent process shall be able to

  1. detect the normal termination of the child process, and in that case shall be able to create another fork() doing the open()/sendfile()/close() for a range of files
  2. kill the sendfile()-child process in case of a specific event and detect the intentional termination to clean up

For requirement 1 I could just wait for the result of sendfile(). Requirement 2 is why I think I need to use fork() in the first place.

After reading the following posts

I think that my solution might not be a good one.

My questions are:

  • Is there any other solution to implement requirement 2 ?
  • Or how can I make sure that the library calls open(), close() and sendfile() will be okay?

Update:

  • The program will run on a Busybox Linux / ARM
  • I've assumed that I should use sendfile() for having the most efficient file transfer due to several posts I've read regarding this topic. A safe way to implement my requirement could be using fork() and exec*() with cp, with the disadvantage that the file transfer might be less efficient

Update 2:

  • it's sufficient to fork() once in case of a specific event (instead of once per file) since I switched to exec*() with rsync in the child process. However the program needs invoke that rsync always in case of a specific event.

解决方案

You can use threads, but forcefully terminating threads typically leads to memory leaks and other problems.

My linux experience is somewhat limited, but I would probably try to fork the program early, before it gets multithreaded. Now that you have two instances, the single threaded instance can be safely used to manage the starting and stopping of additional instances.

这篇关于多线程程序和fork():替代或安全实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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