commit 5c9629680290a8de7a11b305401a7c587e78b582 Author: Mario Fetka Date: Fri Sep 15 13:56:01 2017 +0200 Imported Upstream version 96.042201 diff --git a/MANIFEST b/MANIFEST new file mode 100644 index 0000000..434cd82 --- /dev/null +++ b/MANIFEST @@ -0,0 +1,5 @@ +MANIFEST +Makefile.PL +README +lib/Proc/Forkfunc.pm +t/fork.t diff --git a/Makefile.PL b/Makefile.PL new file mode 100644 index 0000000..9f2baea --- /dev/null +++ b/Makefile.PL @@ -0,0 +1,9 @@ + +use ExtUtils::MakeMaker; + +WriteMakefile( + 'VERSION' => 96.042201, + 'NAME' => 'Proc::Forkfunc', + 'dist' => { COMPRESS=>"gzip", SUFFIX=>"gz" }, + ); + diff --git a/README b/README new file mode 100644 index 0000000..c0bc899 --- /dev/null +++ b/README @@ -0,0 +1,11 @@ + +This is a simple wrapper for fork. It will wait for there +to be a process available. It will fork off a perl function. + +To build/install it, use + + perl Makefile.PL + make + make test + make install + diff --git a/lib/Proc/Forkfunc.pm b/lib/Proc/Forkfunc.pm new file mode 100644 index 0000000..aef1f99 --- /dev/null +++ b/lib/Proc/Forkfunc.pm @@ -0,0 +1,67 @@ + +package Proc::Forkfunc; + +require Exporter; +require POSIX; +use Carp; + +@ISA = (Exporter); +@EXPORT = qw(forkfunc); + +use vars qw($VERSION); +$VERSION = 96.041701; + +use strict; + +sub forkfunc +{ + my ($func, @args) = @_; + + my $pid; + + { + if ($pid = fork()) { + # parent + return $pid; + } elsif (defined $pid) { + # child + &$func(@args); + croak "call to child returned\n"; + } elsif ($! == &POSIX::EAGAIN) { + my $o0 = $0; + $0 = "$o0: waiting to fork"; + sleep 5; + $0 = $o0; + redo; + } else { + croak "Can't fork: $!"; + } + } +} + +1; + +__END__ + +=head1 NAME + +Proc::Forkfunk -- fork off a function + +=head1 SYNOPSIS + + use Proc::Forkfunc; + + forkfunc(\&child_func,@child_args); + +=head1 DESCRIPTION + +Fork off a process. Call a function on the child process +the function should be passed in as a reference. +The child function should not return. + +Logic copied from somewhere, probably Larry Wall. + +=head1 AUTHOR + +David Muir Sharnoff + diff --git a/t/fork.t b/t/fork.t new file mode 100755 index 0000000..72dcff0 --- /dev/null +++ b/t/fork.t @@ -0,0 +1,43 @@ +#!/usr/local/bin/perl -w + +$| = 1; + +print "1..4\n"; + +use Proc::Forkfunc; + +forkfunc(sub { exit 1} ); + +&wok(1); + +forkfunc(sub { exit $_[0] }, 2); + +&wok(2); + +forkfunc(\&pok3); + +&wok(3); + +forkfunc(\&pok, 4); + +&wok(4); + +sub pok3 +{ + exit 3; +} + +sub pok +{ + exit $_[0]; +} + +sub wok +{ + my ($ws) = @_; + + wait(); + my $st = $? >> 8; + + print($st == $ws ? "ok $ws\n" : "not ok $ws\n"); +}