Parallel::ForkManager demystified
The aim of this post is to check if Parallel::ForkManager always runs the maximum number of processes simultaneously. Suppose one of the processes finish early but the rest continue, will another process be created immediately so that the number of processes becomes equal to the maximum number of processes?
Let us have a look at a script that forks 10 processes:
Output
Each line of the output is of the format:
[time in seconds since the epoch] - [Started|Finished]: [Process ID]
Consider the sleep intervals (in seconds) again: 1, 1, 5, 5, 4, 5, 6, 7, 8 and 9. This means that our first and second processes should sleep for 1 second each before finishing. Similarly, the third and fourth processes should sleep for 5 seconds each, the fifth should sleep for 4 seconds and so on.
The first four lines of the output state that four processes with process IDs 4229, 4230, 4231 and 4232 are created.
1330656252 - Started: 4229
1330656252 - Started: 4230
1330656252 - Started: 4231
1330656252 - Started: 4232
The next four lines:
1330656253 - Finished: 4229
1330656253 - Finished: 4230
1330656253 - Started: 4233
1330656253 - Started: 4234
The first and second processes (4229 and 4230) finished after 1 second (the sleep interval assigned for them). This can be confirmed by doing the simple arithmetic: 1330656253 - 1330656252 = 1. As soon as these processes are finished, two other processes are forked (4233 and 4234) so that the number of parallel processes is always 4.
