简体   繁体   English

不完全是fifo

[英]not exactly fifo

I'm busting my head but cannot find a solution for this. 我的脑袋破了,但找不到解决方案。 Please consider this scenario: 请考虑以下情形:

I have writers that want to write to non-blocking "queue" to another machine on the local network and have a reader that will read data (blocking mode if there are no writers) and do some job A and then return after a long hour and take next data. 我有一些写入器,它们希望将非阻塞“队列”写入本地网络上的另一台计算机,并且有一个读取器将读取数据(如果没有写入器,则为阻塞模式),并做一些工作A,然后经过很长时间后返回并获取下一个数据。

So the scenario is like that: 所以场景是这样的:

  • Writer writes 作家写
  • Writer writes 作家写
  • Writer writes 作家写
  • Writer writes 作家写
  • Writer writes 作家写
  • Reader reads and does job 读者阅读并工作

    In the same time while reader is busy: 在阅读器忙的同时:

  • Writer writes 作家写
  • Writer writes 作家写
  • Writer writes 作家写
  • Writer writes 作家写
  • Writer writes 作家写
  • etc ... 等...

I thought I could do this with a tcp daemon as a reader, but that would mean that it would run simultaneously with fork(s) and I want the reader to process one at a time because it will do a cpu hungry job. 我以为我可以使用tcp守护程序作为读取器来执行此操作,但这意味着它将与fork同时运行,并且我希望读取器一次处理一个,因为它将完成cpu所需的工作。

I thought about having a tcp server get the requests and then signal to a FIFO and have another daemon read from the FIFO but it has the same limitations. 我考虑过让tcp服务器获取请求,然后向FIFO发出信号,并从FIFO中读取另一个守护程序,但是它具有相同的局限性。

I mean the FIFO must read when the writer writes, and I want the writer to write many times faster than the reader. 我的意思是写程序写时FIFO必须读取,并且我希望写程序比读程序快许多倍。

A db solution would be ok, but a) it is not very fast and b) there is no locking for the reader..I wouldn't want to implement it with sleep(x) it seems not a good programming technique. db解决方案可以,但是a)速度不是很快,b)读者没有锁定。我不想用sleep(x)实现它,这似乎不是一种好的编程技术。

Any solutions? 有什么办法吗?

This sounds as if you've got a producer-consumer problem . 听起来好像您遇到了生产者-消费者问题 Take a look at the various implementations in the Wikipedia article and see if one of them meets your needs. 查看Wikipedia文章中的各种实现,看看其中一种是否满足您的需求。

Multithreading seems like the path to follow. 多线程似乎是要遵循的道路。 Create a thread that starts either the reading or the writing part and use the other thread on the other task. 创建一个启动阅读或写作部分的线程,并在另一个任务上使用另一个线程。 You will need to provide some thread safe communication among threads if you need to pass data from reader to writer. 如果需要将数据从读取器传递到写入器,则需要在线程之间提供一些线程安全的通信。 You may also consider using more than one thread for the writing, depending on the context of the application. 您还可以考虑使用多个线程进行编写,具体取决于应用程序的上下文。

With plain C on a POSIX system, pthreads is the way to go. 使用POSIX系统上的纯C语言,pthreads是必经之路。

one option is to have a server (writer) and a client node. 一种选择是拥有服务器(写入器)和客户端节点。 this outlines the the general approach: 这概述了一般方法:

Server produces jobs and push them into a local queue: 服务器产生作业并将其推送到本地队列中:

// server thread
while(true)
{
     job = generate();
     jobs_queue.push(job); // push job to a local queue on the server
}

when a client connects to the server, a thread on the server reads everything in the queue and push it to the client. 当客户端连接到服务器时,服务器上的线程将读取队列中的所有内容并将其推送到客户端。 the queue is required to hold jobs while there is no connected client. 没有连接的客户端时,队列需要保留作业。 may be irrelevant in your case. 可能与您的情况无关。

// server acceptor
while(true)
{
     c = wait_for_connection();
     while(connected(c))
     {
          while(queue.size() >  0)
              c.write(queue.pop()); // send a job to the client

          // block till queue is not empty. can be achieved with sleep or by using a mutex.
     }
}

client node will sit on a tcp socket, reading jobs and putting them into a local queue (on the client). 客户端节点将坐在tcp套接字上,读取作业并将其放入本地队列(在客户端上)。 there is a client thread that work like this: 有一个客户端线程像这样工作:

// client thread that poll from server
while(true)
{
    job = readNextJob(); // tcp, blocks if there is nothing to read
    queue.push(job);
}

// client thread that spawn jobs from queue
while(true)
{
    job = queue.pop(); // blocks if queue empty
    job.execute();
    job.waitForCompletion();
}

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

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