信号量未能等待,原因 "Resource temporarily unavailable"
Semaphores failing to wait, with reason "Resource temporarily unavailable"
我试图让一个父进程等待多个子进程发出信号,然后在父进程继续之前,使用一组未命名的信号量(每个子进程一个)。但是,当使用 sem_wait() 时,父进程会无限期地等待,而 sem_trywait() return 会出现 "resource temporarily unavailable" 错误,并且在没有子进程发出信号的情况下继续。 sem_init() 和 sem_post() return 都不是错误。
代码的相关部分:
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;
}
我是不是设置不正确,或者只是误解了在这种情况下如何使用信号量?
编辑添加:sem_wait() 遇到错误,无论子进程在等待之前还是之后调用 sem_post()。
用sem_t* sems = new sem_t[numsems];
分配的信号量不在共享内存中。因此每个进程都有自己的副本,在子进程中发布不会影响父进程。
父副本保持锁定状态。 sem_trywait
失败并显示 EAGAIN
,这转化为 resource temporarily unavailable
解释。
我试图让一个父进程等待多个子进程发出信号,然后在父进程继续之前,使用一组未命名的信号量(每个子进程一个)。但是,当使用 sem_wait() 时,父进程会无限期地等待,而 sem_trywait() return 会出现 "resource temporarily unavailable" 错误,并且在没有子进程发出信号的情况下继续。 sem_init() 和 sem_post() return 都不是错误。
代码的相关部分:
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;
}
我是不是设置不正确,或者只是误解了在这种情况下如何使用信号量?
编辑添加:sem_wait() 遇到错误,无论子进程在等待之前还是之后调用 sem_post()。
用sem_t* sems = new sem_t[numsems];
分配的信号量不在共享内存中。因此每个进程都有自己的副本,在子进程中发布不会影响父进程。
父副本保持锁定状态。 sem_trywait
失败并显示 EAGAIN
,这转化为 resource temporarily unavailable
解释。