不完全是先进先出

发布于 2024-08-06 03:29:55 字数 692 浏览 8 评论 0原文

我正在绞尽脑汁,但找不到解决方案。请考虑这种情况:

我有编写器想要写入非阻塞“队列”到本地网络上的另一台机器,并且有一个读取器将读取数据(如果没有编写器则为阻塞模式)并执行一些作业 A,然后一个小时后返回并获取下一个数据。

所以场景是这样的:

  • Writer 写入
  • Writer 写入
  • Writer 写入
  • Writer 写入
  • Writer 写入
  • Reader 读取并执行工作

    在读者忙碌的同时:

  • 作家写作
  • 作家写作
  • 作家写作
  • 作家写作
  • 作家写作
  • 等等...

我想我可以做到这一点使用 tcp 守护进程作为读取器,但这意味着它将与 fork 同时运行,并且我希望读取器一次处理一个,因为它将执行一项占用 cpu 的工作。

我考虑过让 tcp 服务器获取请求,然后向 FIFO 发出信号,并让另一个守护进程从 FIFO 读取数据,但它具有相同的限制。

我的意思是当写入器写入时 FIFO 必须读取,并且我希望写入器的写入速度比读取器快很多倍。

数据库解决方案就可以了,但是a)它不是很快,b)读者没有锁定。我不想用 sleep(x) 来实现它,这似乎不是一个好的编程技术。

有什么解决办法吗?

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.

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.

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.

I mean the FIFO must read when the writer writes, and I want the writer to write many times faster than the reader.

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.

Any solutions?

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

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

发布评论

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

评论(3

草莓味的萝莉 2024-08-13 03:29:55

这听起来好像您遇到了生产者-消费者问题。查看维基百科文章中的各种实现,看看其中之一是否满足您的需求。

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.

哥,最终变帅啦 2024-08-13 03:29:55

多线程似乎是一条可行的道路。创建一个线程来启动读取或写入部分,并在其他任务上使用另一个线程。如果需要将数据从读取器传递到写入器,则需要在线程之间提供一些线程安全通信。您还可以考虑使用多个线程进行写入,具体取决于应用程序的上下文。

对于 POSIX 系统上的普通 C,pthreads 是最佳选择。

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.

红ご颜醉 2024-08-13 03:29:55

一种选择是拥有一个服务器(编写器)和一个客户端节点。
这概述了一般方法:

服务器生成作业并将其推送到本地队列中:

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

当客户端连接到服务器时,服务器上的线程读取队列中的所有内容并将其推送到客户端。当没有连接的客户端时,队列需要保存作业。可能与您的情况无关。

// 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.
     }
}

客户端节点将位于 TCP 套接字上,读取作业并将其放入本地队列(在客户端上)。
有一个客户端线程的工作方式如下:

// 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();
}

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