Perl Process Management Guide — Fork, Exec, and Process Control
In this tutorial, you will learn about Perl Process Management Guide. We cover key concepts, practical examples, and best practices to help you master this topic.
Perl process management covers fork (create child process), exec (replace current process), wait/waitpid (collect child exit status), and system/qx (run external commands) -- with POSIX::_exit and $? for exit code handling.
Fork
my $pid = fork();
if (not defined $pid) {
die "Fork failed: $!";
}
if ($pid == 0) {
# Child process
print "Child: PID = $$\n";
sleep(2);
exit(0);
}
# Parent process
print "Parent: child PID = $pid\n";
my $child = waitpid($pid, 0);
print "Child $child exited with status: $?\n";
Exec
# Replace current process with external command
exec("ls", "-la", "/tmp")
or die "exec failed: $!";
# Never reached if exec succeeds
System Command
# system - run command, return exit status
my $exit_code = system("ls", "-la");
my $exit_code = system("ls -la /tmp"); # shell (with warnings)
# Capture output (backticks/qx)
my $output = `ls -la`;
my $output = qx(ls -la);
# Check exit status
system("grep pattern file.txt");
if ($? == -1) {
print "Failed to execute: $!\n";
} elsif ($? & 127) {
printf "Killed by signal %d\n", ($? & 127);
} else {
printf "Exited with %d\n", ($? >> 8);
}
Parallel Processing
my @urls = qw(url1 url2 url3);
my @pids;
foreach my $url (@urls) {
my $pid = fork();
if ($pid == 0) {
# Child process
`curl -s $url > /dev/null`;
exit;
}
push @pids, $pid;
}
# Wait for all children
foreach my $pid (@pids) {
waitpid($pid, 0);
}
print "All downloads complete\n";
Common Mistakes
1. Zombie processes
Uncollected child processes become zombies. Always call wait() or waitpid() and set $SIG{CHLD} handler if not waiting.
2. Fork in loops without exit
Children continue the loop. Always call exit after child work, or the child will fork grandchildren.
3. File handles after fork
File descriptors are shared. Both parent and child share the same file position. Use $| = 1 for flushing or close handles in child.
Practice Questions
1. What does fork return? 0 in the child process, child PID in the parent, undef on failure.
2. How do you prevent zombie processes?
Call wait() or waitpid() for each child, or set $SIG{CHLD} = 'IGNORE' to auto-reap.
3. What is the difference between exec and system? exec replaces the current process. system forks and waits. system returns exit code, exec never returns on success.
FAQ
{{< faq question="How do I get the PID of the current process?" >}}
The special variable $$ contains the current process ID.
{{< /faq >}}
{{< faq question="What is $? after system?" >}}
$? holds the exit status. $? >> 8 is the exit code. $? & 127 is the signal number. $? & 128 is core dump flag.
{{< /faq >}}
{{< faq question="Can I communicate with child processes?" >}}
Yes. Use pipe(): pipe(my $read, my $write) before fork, then the child can write to $write and parent reads from $read.
{{< /faq >}}
What's Next
Now learn about memory management in Perl.
| Topic | Description | Link |
|---|---|---|
| Memory | Memory management and leaks | {{< ref "30-memory" >}} |
| Python | Compare with Python | Python |
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro