如何处理多线程进程的 fork 错误?

发布于 2025-01-07 03:28:10 字数 346 浏览 2 评论 0原文

我正在开发一个多线程进程,它分叉来执行另一个进程。有时,如果执行文件不存在,fork 可能会出错。由于此进程在分叉之前有多个线程运行,因此我有几个问题:

  1. 线程是否复制到分叉进程。
  2. 使用多线程进程处理 fork 错误的最佳实践是什么。例如:

    /* 在多线程进程中 */
    
    
    pid = fork();
    
    如果(pid==0)
    {
    
        execlp(文件名,文件名,NULL);
    
        fprintf(stderr, "文件名不存在");
    
        /* 如果有多个线程正在运行我该怎么办
        从叉子上? */
    
        退出(-1);
    }
    

I am working on a multithreaded process that forks to execute another process. Sometimes, the fork may error if the execution file does not exist. Since this process has multiple threads running prior to fork I have a couple questions:

  1. Are threads copied over to the forked process.
  2. What is the best practice to handling an error from fork with a multithreaded process. For example:

    /* in a multithreaded process */
    
    
    pid = fork();
    
    if(pid == 0)
    {
    
        execlp(filename, filename, NULL);
    
        fprintf(stderr, "filename doesn't exist");
    
        /* what do i do here if there's multiple threads running
        from the fork? */
    
        exit(-1);
    }
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑凤梨 2025-01-14 03:28:10

好吧,如果可执行文件不存在,fork 不会出错。在这种情况下,exec 会出错。但是,对于您的实际问题,POSIX指出fork使用单个线程创建一个新进程,该线程是调用的线程的副本>分叉。有关详细信息,请参阅此处

进程应使用单个线程创建。如果多线程进程调用 fork(),新进程应包含调用线程及其整个地址空间的副本,可能包括互斥体和其他资源的状态。

因此,为了避免错误,子进程只能执行异步信号安全操作,直到调用 exec 函数之一为止。

所以你所拥有的是好的,如果有点稀疏:-)

单个线程将在子进程中运行,如果你不能执行另一个程序,记录一条消息并退出。

并且,在基本原理部分,它解释了为什么这样做:

POSIX 程序员调用 fork() 有两个原因。原因之一是在同一个程序中创建一个新的控制线程(这最初只能在 POSIX 中通过创建一个新进程来实现);另一种是创建一个运行不同程序的新进程。在后一种情况下,对 fork() 的调用很快就会调用其中一个 exec 函数。

使 fork() 在多线程世界中工作的一般问题是如何处理所有线程。有两种选择。一种是将所有线程复制到新进程中。这会导致程序员或实现处理在系统调用上挂起的线程,或者可能即将执行不应在新进程中执行的系统调用的线程。另一种选择是仅复制调用 fork() 的线程。这造成了进程本地资源的状态通常保存在进程内存中的困难。如果未调用 fork() 的线程持有资源,则该资源永远不会在子进程中释放,因为其任务是释放该资源的线程在子进程中不存在。

当程序员编写多线程程序时,首先描述的 fork() 的使用(即在同一程序中创建新线程)是由 pthread_create() 函数提供的。因此,fork() 函数仅用于运行新程序,并且在调用 fork() 和调用 exec 函数之间调用需要某些资源的函数的效果是未定义的。

Well, the fork doesn't error if the executable file doesn't exist. The exec errors in that case. But, to your actual question, POSIX states that fork creates a new process with a single thread, a copy of the thread that called fork. See here for details:

A process shall be created with a single thread. If a multi-threaded process calls fork(), the new process shall contain a replica of the calling thread and its entire address space, possibly including the states of mutexes and other resources.

Consequently, to avoid errors, the child process may only execute async-signal-safe operations until such time as one of the exec functions is called.

So what you have is okay, if a little sparse :-)

A single thread will be running in the child and, if you cannot exec another program, log a message and exit.

And, in the rationale section, it explains why it was done that way:

There are two reasons why POSIX programmers call fork(). One reason is to create a new thread of control within the same program (which was originally only possible in POSIX by creating a new process); the other is to create a new process running a different program. In the latter case, the call to fork() is soon followed by a call to one of the exec functions.

The general problem with making fork() work in a multi-threaded world is what to do with all of the threads. There are two alternatives. One is to copy all of the threads into the new process. This causes the programmer or implementation to deal with threads that are suspended on system calls or that might be about to execute system calls that should not be executed in the new process. The other alternative is to copy only the thread that calls fork(). This creates the difficulty that the state of process-local resources is usually held in process memory. If a thread that is not calling fork() holds a resource, that resource is never released in the child process because the thread whose job it is to release the resource does not exist in the child process.

When a programmer is writing a multi-threaded program, the first described use of fork(), creating new threads in the same program, is provided by the pthread_create() function. The fork() function is thus used only to run new programs, and the effects of calling functions that require certain resources between the call to fork() and the call to an exec function are undefined.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文