gpt4 book ai didi

c - 为什么这段代码会导致死锁? (生产者消费者模型)

转载 作者:太空宇宙 更新时间:2023-11-04 08:56:15 26 4
gpt4 key购买 nike

我正在尝试设计 5 个生产者和单一消费者模型。我刚刚从Producer-consumer problem学会了如何设计模型, 维基百科。但是我很难避免死锁。我不明白为什么这段代码会导致死锁。

这是消费者。 (假设单进程执行这段代码,所有信号量和共享内存都已经初始化。)

int main()
{
Shared_Data = (shared_data*)Shared_Address; // this is a shared memory.
for(i = 0 ; i < 5; i++)
{
sprintf(debugbuf, "%ld, filled downsem, %dth loop\n", (long)getpid(), i);
write(STDOUT_FILENO, debugbuf, strlen(debugbuf));
sem_wait(filled); // try to access to the shared memory.
// if the shared memory is empty, consumer waits for it to be filled with pid and state.


read.isIdle = I_AM_IDLE;
read.WrittenByPid = Shared_Data->WrittenByPid;

sprintf(debugbuf, "%ld, empty upsem\n", (long)getpid());
write(STDOUT_FILENO, debugbuf, strlen(debugbuf));


sem_post(empty);
}
}

这里是制作人。 (假设有五个进程执行这段代码,并且所有的信号量和共享内存都已经初始化。)

int main()
{
sprintf(debugbuf, "%ld, empty downsem\n", (long)getpid());
write(STDOUT_FILENO, debugbuf, strlen(debugbuf));
// only one child process have access to here.
sem_wait(empty); // another child process doen't have access to here,
// untill parent process reads the shared memory.


sprintf(writebuf, "%s %ld process is forked.\n", Get_CurrentTime(date), (long)getpid());
write(STDOUT_FILENO, writebuf, strlen(writebuf));
Put_itsState_into_SharedMemory(getpid(), I_AM_IDLE, YES_Virgin);


sprintf(debugbuf, "%ld, filled upsem\n", (long)getpid());
write(STDOUT_FILENO, debugbuf, strlen(debugbuf));


sem_post(filled);
}

但我有以下结果。

32685, filled downsem, 0th loop
32687, empty downsem
32687, filled upsem
32685, empty upsem
32685, filled downsem, 1th loop
32690, empty downsem
32689, empty downsem
32691, empty downsem
32688, empty downsem
[stuck in for-loop]

任何帮助都会很棒。谢谢。

最佳答案

你应该经常检查,sem_wait 的返回值是什么。

如果您在 sem_wait 函数中并且有中断进来,那么 sem_wait 将返回一个不等于 0 的错误。然后你需要检查是否发生了中断,使用errno

由于您使用fork,您可以在一个子进程退出时获得SIGCHLD 中断。

关于c - 为什么这段代码会导致死锁? (生产者消费者模型),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16834663/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com