生产者消费者问题的 FIFO 队列不工作

发布于 2025-01-17 03:49:18 字数 155 浏览 3 评论 0原文

我目前正在尝试实现 FIFO 来解决生产者消费者问题。然而,当我运行代码时,似乎第一个项目没有从缓冲区中删除,因为输出显示每个消费者都在消耗第一个项目,而不是其他项目(附加输出的屏幕截图)。

我实现了一个 LIFO 队列并得到了预期的结果,这让我相信问题出在我的 FIFO 实现上。

I currently am trying to implement FIFO for the producer consumer problem. However when I run the code it seems that the first item is not being removed from the buffer as the output shows that every consumer is consuming the first item and never the others (Screenshot of output attached).

I implemented a LIFO queue and got the expected result which is what leads me to believe that the issue is with my FIFO implementation.

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

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

发布评论

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

评论(1

我的鱼塘能养鲲 2025-01-24 03:49:18

出队时出现简单错误。想象一下您想要获取队列中的第一个条目(buffer_rd == 0)。但是你增加 buffer_rd 和读取 theat 条目,

buffer_t dequeuebuffer() {
    if (buffer_rd == buffer_wr) {
        printf("Buffer underflow\n");
    } else {
        buffer_rd = (buffer_rd + 1) % SIZE; <<<<<====
         return buffer[buffer_rd];   <<<<<<======
    }
    return 0;
}

你需要反转这 2 个(就像在插入中一样)

buffer_t dequeuebuffer() {
    if (buffer_rd == buffer_wr) {
        printf("Buffer underflow\n");
    } else {
        int ret =  buffer[buffer_rd];
        buffer_rd = (buffer_rd + 1) % SIZE;

    return ret;
    }


    return 0;
} 

Simple error in dequeue. Imagine you want to get the first entry in the queue ( buffer_rd == 0). But you increment buffer_rd and the read theat entry,

buffer_t dequeuebuffer() {
    if (buffer_rd == buffer_wr) {
        printf("Buffer underflow\n");
    } else {
        buffer_rd = (buffer_rd + 1) % SIZE; <<<<<====
         return buffer[buffer_rd];   <<<<<<======
    }
    return 0;
}

you need to reverse those 2 (like in insert)

buffer_t dequeuebuffer() {
    if (buffer_rd == buffer_wr) {
        printf("Buffer underflow\n");
    } else {
        int ret =  buffer[buffer_rd];
        buffer_rd = (buffer_rd + 1) % SIZE;

    return ret;
    }


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