perl多任务问题
我已经完成了我之前使用 Perl 线程的多线程程序,它可以在我的系统上运行。问题是,在它需要运行的某些系统上,线程支持未编译到 perl 中,并且我无法安装其他软件包。因此,我需要使用线程以外的其他东西,并且我正在将代码移至使用 fork()。这适用于我的 Windows 系统启动子任务。
几个问题:
如何判断子进程何时退出?当线程计数低于某个值时,我创建了新线程,我需要跟踪正在运行的线程数量。对于进程,我如何知道一个进程何时退出,以便跟踪当时有多少个进程存在,当创建一个进程时递增计数器,当一个退出时递减计数器?
当父进程打开时,使用通过 OPEN 获得的句柄进行文件 I/O 在子进程中是否安全?我需要为每个子进程附加一个文件,这在 UNIX 上也安全吗?
除了 fork 和 thread 之外还有其他选择吗?我尝试使用 Parallel::ForkManager,但它没有安装在我的系统上(使用 Parallel::ForkManager;给出了错误),并且我绝对要求我的 perl 脚本在所有 unix/windows 系统上工作,而无需安装任何其他模块。< /p>
I have finished my earlier multithreaded program that uses perl threads and it works on my system. The problem is that on some systems that it needs to run on, thread support is not compiled into perl and I cannot install additional packages. I therefore need to use something other than threads, and I am moving my code to using fork(). This works on my windows system in starting the subtasks.
A few problems:
How to determine when the child process exits? I created new threads when the thread count was below a certain value, I need to keep track of how many threads are running. For processes, how do I know when one exits so I can keep track of how many exist at the time, incrementing a counter when one is created and decrementing when one exits?
Is file I/O using handles obtained with OPEN when opened by the parent process safe in the child process? I need to append to a file for each of the child processes, is this safe on unix as well.
Is there any alternative to fork and threads? I tried use Parallel::ForkManager, but that isn't installed on my system (use Parallel::ForkManager; gave an error) and I absolutely require that my perl script work on all unix/windows systems without installing any additional modules.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
典型用法:
perldoc -f fork
中的详细信息,waitpid
,等待
,kill
和perlipc
。perlipc
中有关为SIGCHLD
事件设置处理程序的内容应该特别有用,尽管 Windows 不支持这一点。在 Unix 和 Windows 上,跨分叉进程的 I/O 通常是安全的。文件描述符是共享的,因此对于类似的事情,
子进程和父进程都将成功写入同一个文件(但请注意输出缓冲)。
Typical usage:
Gory details in
perldoc -f fork
,waitpid
,wait
,kill
, andperlipc
. The stuff inperlipc
about setting up a handler forSIGCHLD
events should be particularly helpful, though that isn't supported on Windows.I/O across forked processes is generally safe on Unix and Windows. File descriptors are shared, so for something like this
both child and parent processes will successfully write to the same file (be aware of output buffering, though).
看一下
waitpid
。下面是一些需要完成九个任务(1 到 9)的代码。它将启动最多三名工作人员来完成这些任务。Take a look at
waitpid
. Here is some code that has nine tasks that need to be done (1 through 9). It will start up to three workers to do those tasks.