简体   繁体   English

Perl-与fork / exec'd进程进行通信

[英]Perl - Communicating with a fork/exec'ed process

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. 我正在设计一个服务器,该服务器将通过fork / exec'ing四个“ manager”(自己的服务器进程)进行初始化,然后接受来自客户端的连接,而fork / exec'ing “ slave”与客户端进行通信。 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? 或者是其他东西?

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. 管理器准备就绪后,可以将其PID写入管道并关闭。 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?). 如果在所有管理人员都报告准备好就职前从管道中获得EOF,则它知道至少一名管理人员无法启动,并且可以采取适当的措施(日志错误并退出?)。 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. 也就是说,如果消息足够短,则一个进程写的内容将不会与另一进程写的内容交错。

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. 他们可以保持打开状态,并在准备就绪时编写适当的“ PID准备就绪”,以及稍后的其他状态消息(“ PID退出”,“ PID太忙”,“ PID喝咖啡休息”等)。

There are a lot of other IPC mechanisms that could be used, each with its own set of advantages and disadvantages. 可以使用许多其他IPC机制,每种机制都有其自己的优点和缺点。 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. 而这样的例子不胜枚举。

What you are thinking of are FIFO pipes. 您在想的是FIFO管道。 mknod is traditionally used to create them. 传统上, mknod用于创建它们。 The pipes have 2 file descriptors, one for reading, one for writing.... they can block on that if necessary... 管道有2个文件描述符,一个用于读取,一个用于写入...。如果有必要,它们可以阻止...

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

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