Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unix 具有信号量的多个生产者-消费者:解决方案的构想_Unix_Synchronization_Producer Consumer - Fatal编程技术网

Unix 具有信号量的多个生产者-消费者:解决方案的构想

Unix 具有信号量的多个生产者-消费者:解决方案的构想,unix,synchronization,producer-consumer,Unix,Synchronization,Producer Consumer,我试图用信号量解决多生产者/消费者问题。 我解决了一个特定情况下多个生产者/消费者的问题-当缓冲区的大小为1时。但是,当缓冲区的大小大于1并且仍然有许多生产者和消费者时,我很难找出应该对代码做哪些修改来解决问题 缓冲区大小=1时的工作解决方案 当缓冲区的大小为1时,下面是我的工作解决方案的伪代码。(代码使用了一个名为libIPC的库,它不是很流行,因此我提出了伪代码) 共享内存是: typedef struct { int buffer;//the buffer size is 1,so it

我试图用信号量解决多生产者/消费者问题。 我解决了一个特定情况下多个生产者/消费者的问题-当
缓冲区的大小为1时。但是,当缓冲区的大小大于1并且仍然有许多生产者和消费者时,我很难找出应该对代码做哪些修改来解决问题

缓冲区大小=1时的工作解决方案 当
缓冲区的大小为1时,下面是我的工作解决方案的伪代码。(代码使用了一个名为
libIPC
的库,它不是很流行,因此我提出了伪代码)

共享内存是:

typedef struct {
 int buffer;//the buffer size is 1,so it is only one integer
 int nbconsumers;  
//shared counter between all processes indicating the number of consumers that read the message
} t_segpart;

t_segpart *sp; 
我使用3个信号量,如下所述:

sem_t* sem_produce ;
// semaphore blocking the producers when the buffer is full
sem_t* sem_consumer[NR] ;
// semaphore blocking the consumers when there is no message in the buffer
sem_t* mutex ;
// Mutual exclusion semaphore for protected access to nbconsumers
制作人
代码:

while(1)
{
        P(sem_producer);//producer demands a permission to write in the buffer
        Produce the message in the buffer
        for each consummer i
        V(sem_consumer[i]);//Give access to consumers so that the can read the buffer
 }
消费者
代码是:

while(1)
    {
        ind = index_consumer(id);
        P(sem_consumer[ind]);//the receiver wants a permission to read the message 
        P(mutex);
        read the buffer
        sp->nbconsumer++;//upgrade nbconsummer
        if(all consumers have read the message)
        {
                V(sem_produce);//Give access to producers so that they can produce a new message in the buffer
                sp->nbconsumer = 0;// nobady has read yet the new buffer
        }
        V(mutex);
}
当缓冲区大小为1时,此解决方案适用于这种特殊情况。 您能告诉我如何修改它,使其适用于更大的缓冲区大小吗?我应该引入什么信号量?我是否应该对共享内存进行任何更改?例如,我认为
缓冲区
将不再是简单的
int变量
,而是一个int
数组。
当缓冲区大小大于1时,您能给出一个伪代码吗?

当您在更多的机器上运行时,您将怎么做?你不应该让一个中央调度服务器来处理同步问题吗?我不太理解你的问题。我正在做一个简化版的问题,消费者和生产者都是用fork制作的简单流程。我永远不会在更多的机器上运行。我试图在一台电脑上解决这个问题。忘了我的问题吧。我在想“卡夫卡”,一个使用生产者和消费者的分布式消息传递系统。有人能告诉我解决这个问题的总体思路吗?