On 9/17/07, perllearner <[EMAIL PROTECTED]> wrote: > I am trying to get my mind around using fork() > > I have a text file that has solid, liquid, and gas on it all on new > lines, how I understand fork() is if I wanted to do a print each > statement on each line in the file, fork() could do all at once with > parent and child processes.
Not exactly. You could fork a second process, but (with a few exceptions) the two processes won't run "all at once", but concurrently. That is, your central processor may at any given moment be working for one of your processes, but typically not both. It's not twice as fast to use two processes, in general. The most common use of fork is in order to establish an environment for a child process which will be launched (usually) via exec. The child process runs concurrently with the parent. A program could fork off several child processes, each one taking on a particular task. The parent process could do other processing for a while, then use wait() or waitpid() to wait for the child processes to finish. The main reason to do this is when each separate process is expected to be idle much of the time; if many processes all have work for the CPU at the same time, some will normally have to wait. Some people have more than one CPU, and may be hopeful that forking several child processes will keep as many CPUs as busy as possible. It might work, depending upon the algorithm and the system. But it might not, and the overhead of multiple processes might not be overcome in any case. Additionally, using fork for concurrency adds a new set of potential bugs and race conditions. Not to mention fork bombs! As with all power tools, use fork with caution. Does that help you any? Cheers! --Tom Phoenix Stonehenge Perl Training -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/