package main;
$main::VERSION = (q$Revision: 1.7 $ =~ /(\d+)/g)[0];
use strict;
use warnings;
my @children;
foreach my $fork_id ( qw( odin:3 dva:5 tri:7 chetyre:9 pyat:11 ) ) {
my $pid = fork();
if ($pid) {
# parent
push(@children, $pid . q(:) . $fork_id);
} elsif ($pid == 0) {
# child
my ($fork_name, $sleep_time) = split(/:/, $fork_id);
my $total_time = 100;
my $run_time = 0;
while ( $run_time < $total_time ) {
sleep $sleep_time;
print qq(Unga! $fork_id/$$, slept for $sleep_time, $run_time\n);
$run_time += $sleep_time;
} # while ( $run_time < $total_time )
exit 0;
} # if ($pid)
} # foreach my $fork_name
foreach ( @children ) {
my $pid = (split(/:/, $_))[0];
waitpid($pid, 0);
} # foreach ( @children )
####
package main;
$main::VERSION = (q$Revision: 1.7 $ =~ /(\d+)/g)[0];
use strict;
use warnings;
use threads;
# these just call &sub1 with the arguments being the thread's
# name and the amount of time to sleep prior to printing out the
# thread's name and information again
my $thr1 = threads->create(\&sub1, q(odin), 3);
my $thr2 = threads->create(\&sub1, q(dva), 5);
my $thr3 = threads->create(\&sub1, q(tri), 7);
my $thr4 = threads->create(\&sub1, q(chetyre), 9);
my $thr5 = threads->create(\&sub1, q(pyat), 11);
$thr1->join();
$thr2->join();
$thr3->join();
$thr4->join();
$thr5->join();
sub sub1 {
my $thread_name = shift;
my $sleep_time = shift;
my $total_time = 100;
my $run_time = 0;
while ( $run_time < $total_time ) {
sleep $sleep_time;
my $thread = threads->self();
# threads->tid() is the thread ID #, $$ is the Perl PID
print qq(Unga! $thread_name/$$-) . $thread->tid()
. qq(, slept for $sleep_time, $run_time\n);
$run_time += $sleep_time;
}
}
####
package main;
$main::VERSION = (q$Revision: 1.7 $ =~ /(\d+)/g)[0];
use strict;
use warnings;
my @children;
#foreach my $fork_name ( qw( odin:3 dva:5 tri:7 chetyre:9 pyat:11 ) ) {
foreach my $fork_name ( qw( odin dva tri chetyre ) ) {
my $pid = fork();
if ($pid) {
# parent
push(@children, $pid . q(:) . $fork_name);
} elsif ($pid == 0) {
# child
my $thread_obj = Thread::Creator->new($fork_name);
my @threads = $thread_obj->get_thread_list();
foreach my $curr_thread ( @threads ) {
#print qq($$: joining thread ) . $curr_thread->tid() . qq(\n);
$curr_thread->join();
#$curr_thread->detach();
}
exit 0;
# next;
} # if ($pid)
} # foreach my $fork_name
foreach ( @children ) {
my $pid = (split(/:/, $_))[0];
waitpid($pid, 0);
} # foreach ( @children )
package Thread::Creator;
use strict;
use warnings;
use threads;
my @thread_list;
sub new {
my $class = shift;
my $fork_name = shift;
my $self = bless ({}, $class);
foreach my $thread_tmpl ( qw( uno:3 dos:5 tres:7 cuatro:11 ) ) {
my ($thr_name, $sleep_time) = split(/:/, $thread_tmpl);
print qq(fork: $fork_name; creating thread '$thr_name', with a )
. qq(sleep time of $sleep_time\n);
my $thr = threads->create(
#sub { $self->do_work($fork_name, $thr_name, $sleep_time) }
sub { Thread::Creator->do_work($fork_name, $thr_name, $sleep_time) }
);
#$thr->detach();
#$thr->join();
push(@thread_list, $thr);
} # foreach my $thread_tmpl ( qw( uno:3 dos:5 tres:7 cuatro:11 ) )
return $self;
} # sub new
sub get_thread_list {
return @thread_list;
} # sub get_thread_list
sub do_work {
my $self = shift;
my $fork_name = shift;
my $thread_name = shift;
my $sleep_time = shift;
#my $total_time = 30;
my $total_time = 100;
my $run_time = 0;
while ( $run_time < $total_time ) {
sleep $sleep_time;
print qq(Unga! $fork_name/$$ -> $thread_name/) . threads->tid()
. qq(, slept for $sleep_time, $run_time\n);
$run_time += $sleep_time;
} # while ( $run_time < $total_time )
#exit 0;
} # sub do_work