简体   繁体   English

信号量未能等待,原因是“资源暂时不可用”

[英]Semaphores failing to wait, with reason “Resource temporarily unavailable”

I'm trying to have a parent process wait on multiple child processes signalling, before the parent continues, using an array of unnamed semaphores (one per child process). 我正在尝试让父进程在父进程继续之前等待多个子进程信号,使用一组未命名的信号量(每个子进程一个)。 However, when using sem_wait(), the parent process waits indefinitely, while sem_trywait() returns a "resource temporarily unavailable" error, and continues without the child processes having signalled. 但是,当使用sem_wait()时,父进程无限期地等待,而sem_trywait()返回“资源暂时不可用”错误,并且在子进程没有发出信号的情况下继续。 Neither sem_init() nor sem_post() return an error. sem_init()和sem_post()都不会返回错误。

Relevent portion of the code: 相关部分代码:

int numsems = concurrent_instrs.size();
std::cout << "Num sems: " << numsems << "\n";
// create semaphores
sem_t* sems = new sem_t[numsems];
for (int i = 0; i < numsems; i++)
{
    if (sem_init(&sems[i], 1, 0) < 0)
    {
        perror("sem initialization failed");
        exit(1);
    }
}

int child_num = 0;

// perform all calculations in block concurrently
for(unsigned int i = 0; i < concurrent_instrs.size() && !isChild; i++)
{
    int pid = fork();
    if (pid == -1)
    {
        perror("Error forking:");
        exit(1);
    }
    if (pid == 0)
    {
        isChild = true;
        instr = concurrent_instrs[i];
    }
    else
    {
        child_num++;
    }
}
if (isChild)
{
    std::cout << "Child process " << child_num << " calculating: " << instr << "\n";
    perform_calculation(instr, input_vars, internal_vars, shm_input, shm_internal);
    std::cout << "Child process " << child_num << " finished calculating\n";

    if (sem_post(&sems[child_num]) < 0)
    {
        perror("Child signal failed");
    }

    std::cout << "Child "<< child_num << " signalled\n";

    // detach from shared memory
    if (shmdt(shm_input) < 0)
    {
        perror("child shm_input detach failed");
    }
    if (shmdt(shm_internal) < 0)
    {
        perror("child shm_internal detach failed");
    }
    exit(0);
}
else
{
    // parent waits for all children to finish
    for (int i = 0; i < numsems; i++)
    {
        std::cout << "Waiting on subprocess " << i << " of " << numsems << "\n";
        if (sem_trywait(&sems[i]) < 0)
            perror("Parent wait failed");
        else
            std::cout << "Parent wait " << i << " working\n";
    }
    std::cout << "Finished waiting\n";

    // destroy semaphores
    for (int i = 0; i < numsems; i++)
    {
        if(sem_destroy(&sems[i]) < 0)
        {
            perror("Sem destroy failed");
            exit(2);
        }
        else
        {
            std::cout << "Sem " << i << " destroyed\n";
        }
    }

    delete[] sems;
}

Am I setting something up incorrectly, or just misunderstanding how to use semaphores in this situation? 我是否设置错误,或者只是误解了如何在这种情况下使用信号量?

Edit to add: sem_wait() encounters the error regardless of whether the child processes call sem_post() before or after the wait. 编辑添加:sem_wait()遇到错误,无论子进程在等待之前或之后调用sem_post()。

The semaphores allocated with sem_t* sems = new sem_t[numsems]; sem_t* sems = new sem_t[numsems];分配的信号量sem_t* sems = new sem_t[numsems]; are not in the shared memory. 不在共享内存中。 Therefore each process has its own copy, and posting in the child doesn't affect the parent. 因此,每个进程都有自己的副本,并且在子进程中发布不会影响父进程。

The parent's copy remains locked. 父母的副本保持锁定状态。 sem_trywait fails with EAGAIN , which translates to resource temporarily unavailable explanation. sem_trywaitEAGAIN而失败,这转换为resource temporarily unavailable解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM