Tag: 生产者 消费者

当缓冲区小于生产者的输入时,生产者/消费者似乎陷入僵局

我创建了一个循环缓冲区,其中有多个客户端写入(最后我希望它们将不同大小的消息写入)到缓冲区中。 服务器将它们读出来。 它基于消费者/生产者问题中的代码: #include #include #include #include #include #define BUFFER_SIZE 10 struct cBuf{ char *buf; int size; int start; int end; pthread_mutex_t mutex; pthread_cond_t buffer_full; pthread_cond_t buffer_empty; }; struct cBuf cb; void buf_Init(struct cBuf *cb, int size) { int i; cb->size = size + 1; cb->start = 0; cb->end = 0; cb->buf = (char *)calloc(cb->size, […]

OpenMP Producer-Consumer意外结果

我正在研究一个简单的生产者 – 消费者问题,在C中使用OpenMP。 我的程序创建了4个线程,其中两个是消费者,两个是生产者。 每个制作人将一个角色放在一个缓冲区中,消费者只需打印该角色。 我的目标是同步生产者/消费者,以便每个生产者将生成字母表中的下一个按顺序字符,并且每个消费者将打印放置在缓冲区中的下一个按顺序字符。 这是我的代码: #include #include #include #define SIZE 5 #define NUMITER 26 char buffer[SIZE]; int nextin = 0; int nextout = 0; int count = 0; int empty = 1; int full = 0; int i,j; void put(char item) { buffer[nextin] = item; nextin = (nextin + 1) % SIZE; count++; […]

这个生产者 – 消费者实施中是否存在竞争条件?

在操作系统概念的第3.4.1节(Silberschatz,第9版)中,作者介绍了生产者 – 消费者问题,并给出了使用循环缓冲区的以下实现(第125,126页)。 //Shared variables #define BUFFER SIZE 10 struct Item; Item buffer[BUFFER SIZE]; int in = 0, out = 0; //buffer is empty when in == out //buffer is full when (in + 1) % BUFFER SIZE == out //Producer while (true) { Item next_produced = /*produce item here*/; while (((in + 1) % […]

c ++中的消费者/生产者

这是一个经典的c / p问题,其中一些线程产生数据而另一些线​​程读取数据。 生产者和消费者都共享一个const大小的缓冲区。 如果缓冲区为空,则消费者必须等待,如果它已满,则生产者必须等待。 我正在使用信号量来跟踪完整或空闲的队列。 生产者将减少信号量,增加价值和增加填充的信号量信号量。 所以我试图实现一个从生成器函数中获取一些数字的程序,然后打印出数字的平均值。 通过将其视为生产者 – 消费者问题,我试图节省一些时间来执行程序。 generateNumber函数导致进程有一些延迟,所以我想创建一些生成数字的线程,并将它们放入队列中。 然后运行main函数的“主线程”必须从队列中读取并找到总和然后平均。 所以这就是我到目前为止所拥有的: #include #include #include #include “Thread.h” #include int generateNumber() { int delayms = rand() / (float) RAND_MAX * 400.f + 200; int result = rand() / (float) RAND_MAX * 20; struct timespec ts; ts.tv_sec = 0; ts.tv_nsec = delayms * 1000000; nanosleep(&ts, […]