简体   繁体   English

在进程之间同步消息队列

[英]Synchronizing message queue between processes

I'm trying to implement a program that has a producer and N (N >= 1) workers. 我正在尝试实现一个具有生产者和N(N> = 1)工人的程序。 They communicate using a message queue. 它们使用消息队列进行通信。 The idea is that the producer sends to the queue "tasks". 这个想法是生产者向队列发送“任务”。 The workers do a msgrcv() call to get a task and execute some code. 工作人员执行msgrcv()调用以获取任务并执行一些代码。 After the worker accomplish the task it sends to the queue the result of the computation. 在工作人员完成任务之后,它将计算结果发送到队列。 The producer will get this message and save the results. 制作人将收到此消息并保存结果。

I'm using POSIX message queues and the producer and the workers work concurrently. 我正在使用POSIX消息队列,生产者和工作者同时工作。

The problem behind this program is that exists a scenario that compromises the communication. 该程序背后的问题是存在危及通信的场景。 Each message has a size of ~5000 bytes. 每条消息的大小约为5000字节。 The maximum queue size is ~16000 bytes in UNIX Systems, which is the case. 在UNIX系统中,最大队列大小约为16000字节,在这种情况下。

The scenario is: There are 3 tasks in the queue (5000*3 = 15000 bytes). 方案是:队列中有3个任务(5000 * 3 = 15000字节)。 Some worker get one message from the queue (now the queue has 10000 bytes). 一些工作者从队列中获取一条消息(现在队列有10000字节)。 The worker starts executing the task and, due to the amount of bytes that a worker has to process in each task, the producer sends to the queue another message (the queue is now full). 工作人员开始执行任务,由于工作人员必须在每个任务中处理的字节数,生产者向队列发送另一条消息(队列现已满)。 Now after the task is complete the worker tries to send the result to the queue and becomes blocked (the queue is full). 现在任务完成后,工作人员尝试将结果发送到队列并被阻止(队列已满)。 The producer tries to send another task to the queue and becomes blocked too. 生产者尝试将另一个任务发送到队列并且也被阻止。

If I run this program with only one worker this scenario have a considerable probability to occur. 如果我只用一个工作程序运行这个程序,那么这种情况很有可能发生。

Does anyone have an idea to avoid this situation? 有没有人有想法避免这种情况?

If you can't change the queue size, number of queues to use, or use a different queuing API, then what about queuing less data? 如果您无法更改队列大小,要使用的队列数或使用不同的排队API,那么排队较少的数据呢?

You could place the actual data in shared memory objects or temporary files. 您可以将实际数据放在共享内存对象或临时文件中。 Then instead of putting the data in the message, you'd put a file name or shared memory object name and possibly an offset in the message instead. 然后,不是将数据放入消息中,而是将文件名或共享内存对象名称以及消息中的偏移量放入其中。 The producer process can then clean it up after receiving the results. 然后,生产者进程可以在收到结果后将其清理干净。

It doesn't necessarily have to be shared memory or temporary files, but the idea is to put the data somewhere other than in the message, and include in the message whatever information is needed for the other process to access it. 它不一定必须是共享内存或临时文件,但其目的是将数据放在消息之外的其他位置,并在消息中包含其他进程访问它所需的任何信息。

我要么为客户端使用第二个消息队列 - >服务器响应或限制(#sent - #received)是一个安全号码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM