I was trying to get a program to start independently from my perl script, and was having a little difficulty today until I found this page which breaks it down pretty simple, however I am going to break it down a little farther for people like me who are pretty duh huh without explicit instructions.
This is from the page above:
if($pid = fork){
# Parent
command;
}elsif($pid == 0){
# Child
command;
# The child must end with an exit!!
exit;
}else{
# Error
die "Fork did not work\n";
}
So in itself it's pretty self explanatory, however you could have some problems if you don't explicitly declare $pid. But besides that you can literally just plug your commands in where it says command;. So maybe I was making it out harder than it was supposed to be, but anyway haves fun. Oh and here's a stupid simple example:
use strict;
my $pid;
if($pid = fork){
# Parent
system("calc");
}elsif($pid == 0){
# Child
system("notepad");
# The child must end with an exit!!
exit;
}else{
# Error
die "Fork did not work\n";
}
system("notepad");
Ok so what's this do. Well it starts calc as part of the parent program, then it starts notepad, once you close the program it will continue on to start a new notepad...so what's the difference between starting it this way and doing it this way:
system("calc");
system("notepad");
system("notepad");
Well without spawning a child process using for you will simply open calc, and once it is closed notepad is opened. Then when you close notepad, another notepad will open.
Tuesday, September 9, 2008
Simple stupid forking in perl. Or starting a process separate from your perl script or app tutorial.
Labels:
child process,
fork. fork(),
fork(),
perl,
processes,
spawn process,
system(),
tutorial
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment