ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Without getting into specifics, I don't want the perl script that runs this to wait for a return code from "mycommand" before it continues with the rest of its code. I did some reading and saw something about %SIG{CHLD} and 'IGNORE' but I'm not sure if I am on the right track with that. Any ideas?
You definitely want to use fork and exec for this. One idiom would be
Code:
my $pid = fork;
if ($pid) {
# the parent process, $pid contains the child pid
print "$$: I'm the parent, child is $pid\n";
# continue with other stuff
} elsif (defined $pid) {
# the child process, $pid is zero
print "$$: I'm the child\n";
# run the program you want to replace this process
exec "command";
} else {
die "Fork error: $!\n";
}
Note that $$ is the special variable that contains the current process pid. Remember to use wait or waitpid to reap your dead children, or you'll have zombies until the parent exits (morbid terminology . Also, if you want to control the input and output of the child process, look at IPC::Open2 or IPC::Open3. You can also open a socket or two before forking to do the IPC. Check out the Perl Cookbook for all this crap .
You definitely want to use fork and exec for this. One idiom would be
Code:
my $pid = fork;
if ($pid) {
# the parent process, $pid contains the child pid
print "$$: I'm the parent, child is $pid\n";
# continue with other stuff
} elsif (defined $pid) {
# the child process, $pid is zero
print "$$: I'm the child\n";
# run the program you want to replace this process
exec "command";
} else {
die "Fork error: $!\n";
}
Note that $$ is the special variable that contains the current process pid. Remember to use wait or waitpid to reap your dead children, or you'll have zombies until the parent exits (morbid terminology . Also, if you want to control the input and output of the child process, look at IPC::Open2 or IPC::Open3. You can also open a socket or two before forking to do the IPC. Check out the Perl Cookbook for all this crap .
Thank you for all the tips. Wouldn't I not want the wait? Exec calls just a normal binary which is coded to just run then stop and I actually don't want the perl script to wait for that binary to return. Is the above code supposed to do that, or is it made specifically to wait (assuming I use "wait"?
Well, if you don't care to track the progress of your child processes, then using wait would not be necessary. However, if you don't reap them when they are done, zombie processes will accumulate until the parent process exits. Read this for a detailed explanation and solutions for dealing with this problem.
Using the code I posted earlier, the parent process will not stop execution. Try the fork man page or read here for some info on process forking, it may make things a little clearer. That page talks about forks in C but it is very similar in perl, and forking is a feature of the operating system, not a particular language.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.