在2.4内核的多线程应用程序中等待失败

时间:2020-12-06 15:35:22

I have an application that is multithreaded - one thread is responsible for collecting the dead children with wait(), anther thread spawns them with fork upon request.

我有一个多线程的应用程序 - 一个线程负责使用wait()收集死孩子,另一个线程根据请求使用fork生成它们。

I found out that on one platform with 2.4 kernel and LinuxThread wait always fails with ECHILD. I've found out that problem might be in non-POSIX compliant implementation of LinuxThreads on 2.4 kernel and the following discussion suggests that there is no way how this could be solved.

我发现在一个具有2.4内核和LinuxThread的平台上,等待总是因ECHILD而失败。我发现问题可能出在2.4内核上LinuxThreads的非POSIX兼容实现中,以下讨论表明没有办法解决这个问题。

Still I'd like to be sure that nobody knows about any solution. Even patch for the kernel would be acceptable.

我仍然希望确定没有人知道任何解决方案。即使是内核补丁也是可以接受的。

When I think about the application design I don't think it could be possible to do both fork() and wait() in a single thread (or only with enormous effort)

当我考虑应用程序设计时,我认为不可能在单个线程中执行fork()和wait()(或者只需付出巨大的努力)

2 个解决方案

#1


It seems to me that this (obviously bogus) behavior is features of LinuxThreads implementation.

在我看来,这(显然是虚假的)行为是LinuxThreads实现的特性。

There really seems to be only two ways out - either switch to NPTL (requires kernel 2.6) or avoid such multi-threaded fork/wait model (this was my solution to the problem and tough it made the architecture a bit more complicated and complex it still was manageable to do in a single day)

实际上似乎只有两种方法 - 要么切换到NPTL(需要内核2.6),要么避免使用这种多线程的fork / wait模型(这是我解决问题的方法,而且它使架构变得更加复杂和复杂仍然可以在一天内完成

Following example is the bare bone example of the bogus situation that fails on LinuxThreads.

以下示例是LinuxThreads上失败的虚假情况的裸骨示例。

#include <pthread.h>
#include <sys/wait.h>
#include <unistd.h>
#include <errno.h>

void * wait_for_child(void *arg)
{
    int s;
    pid_t ret;
    ret = wait(&s);
    if (ret == -1 && errno == ECHILD) perror("Bogus LinuxThreads encountered");
    return NULL;    
}

int main(int argc, char ** argv)
{
    pid_t pid = fork();
    if (pid == -1) return 1;

    // child waits and then dies
    if (pid == 0)
    {
        sleep(3);
        return 0;
    }

    pthread_t wt;
    pthread_create(&wt, NULL, wait_for_child, NULL);
    pthread_join(wt, NULL);
    return 0;
}

#2


If you're starting to think about kernel patches, then it's time to think about upgrades. 2.4 is very long in the tooth.

如果你开始考虑内核补丁,那么就该考虑升级了。 2.4牙齿很长。

#1


It seems to me that this (obviously bogus) behavior is features of LinuxThreads implementation.

在我看来,这(显然是虚假的)行为是LinuxThreads实现的特性。

There really seems to be only two ways out - either switch to NPTL (requires kernel 2.6) or avoid such multi-threaded fork/wait model (this was my solution to the problem and tough it made the architecture a bit more complicated and complex it still was manageable to do in a single day)

实际上似乎只有两种方法 - 要么切换到NPTL(需要内核2.6),要么避免使用这种多线程的fork / wait模型(这是我解决问题的方法,而且它使架构变得更加复杂和复杂仍然可以在一天内完成

Following example is the bare bone example of the bogus situation that fails on LinuxThreads.

以下示例是LinuxThreads上失败的虚假情况的裸骨示例。

#include <pthread.h>
#include <sys/wait.h>
#include <unistd.h>
#include <errno.h>

void * wait_for_child(void *arg)
{
    int s;
    pid_t ret;
    ret = wait(&s);
    if (ret == -1 && errno == ECHILD) perror("Bogus LinuxThreads encountered");
    return NULL;    
}

int main(int argc, char ** argv)
{
    pid_t pid = fork();
    if (pid == -1) return 1;

    // child waits and then dies
    if (pid == 0)
    {
        sleep(3);
        return 0;
    }

    pthread_t wt;
    pthread_create(&wt, NULL, wait_for_child, NULL);
    pthread_join(wt, NULL);
    return 0;
}

#2


If you're starting to think about kernel patches, then it's time to think about upgrades. 2.4 is very long in the tooth.

如果你开始考虑内核补丁,那么就该考虑升级了。 2.4牙齿很长。