Perl-与fork / exec进程通信 [英] Perl - Communicating with a fork/exec'ed process

查看:76
本文介绍了Perl-与fork / exec进程通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个服务器,该服务器将由 fork / exec 初始化,并由四个经理(他们自己处理服务器进程),然后接受来自客户端的连接, fork / exec 将奴隶与客户进行通信。在他们的一生中,奴隶将与管理者建立联系并向他们发送工作请求。

I'm designing a server that will initialize by fork / exec'ing four "managers" (themselves server processes) and will then accept connections from clients, fork / exec'ing "slaves" to communicate with the clients. During their lifetimes, the slaves will establish connections with the managers and send them work requests.

我的问题是有关启动管理者的问题。每个人可能要花一些时间进行初始化(几分钟),我不希望主服务器在初始化之前继续接受客户端。最好的方法是什么?我应该探索让经理在准备就绪时通知主服务器吗?我是否应该让管理者与主服务器建立套接字连接(可能与客户端连接的端口不同),并在准备好后发送消息?还是其他?

My question is about starting up the managers. Each one may take some time to initialize (minutes) and I don't want the master server to proceed to accept clients until they've initialized. What would be the best way to do this? Should I explore having the managers signal the master server when they're ready? Should I have the managers make a socket connection to the master - probably on a different port than the one clients connect to - and send a message when ready? Or something else?

推荐答案

我很想创建一个管道,然后再派出四名经理。管理器准备就绪后,可以将其PID写入管道并关闭。主服务器可以延迟打开其侦听端口,直到至少一个管理器指示它准备就绪为止。如果在所有管理人员都报告准备好就职之前从管道中获得EOF,则它知道至少一名管理人员无法启动,可以采取适当的措施(日志错误并退出?)。写入管道的消息通常被原子处理。也就是说,如果消息足够短,则一个进程写的内容将不会与另一进程写的内容交错。

I'd be very tempted to create one pipe before forking off the four managers. When a manager is ready, it can write its PID on the pipe and close it. The master server can delay opening its listening port until at least one of the managers has indicated that it is ready. If it gets EOF from the pipe before all the managers have reported ready for active duty, then it knows at least one of the managers failed to start and can take appropriate action (log errors and exit?). Messages written to a pipe are normally treated atomically; that is, if the message is short enough, what one process writes will not be interleaved with what another process writes.

对于某些变体,每个管理器可以有一个管道;那么您必须决定如何轮询或选择哪个管道等待消息。您可以在指示经理准备就绪后决定不关闭管道。他们可以保持其打开状态,并在准备就绪时编写适当的 PID准备就绪,并稍后再发送其他状态消息( PID退出, PID太忙, PID喝咖啡休息等)。

For some variations, you could have one pipe per manager; then you have to decide how you're going to poll or select which of the pipes has a message waiting. You could decide that managers do not close the pipe after indicating that they're ready; they could keep it open, and write an appropriate 'PID ready' when ready, and other status messages ('PID exiting', 'PID too busy', 'PID taking coffee break', ...) later on.

还有很多其他可以使用的IPC机制,每种机制都有其自身的优缺点。在很大程度上取决于管理者需要与主控者进行通信的内容(以及主控者是否需要与特定管理者进行通信)。可以肯定使用套接字。消息队列也可以。如果您的通信需求很简单,则可以使用信号灯集。清单还在继续。

There are a lot of other IPC mechanisms that could be used, each with its own set of advantages and disadvantages. A lot depends on what the managers need to communicate to the master (and whether the master needs to communicate with specific managers). Sockets could be used for sure; so could message queues. If your communication needs are simple, a semaphore set might work. And the list goes on.

这篇关于Perl-与fork / exec进程通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆