in reply to Re: Re: Re: Running more than one process (the very basics)
in thread Reminder program
#!/usr/bin/perl -w use strict; sub sched{ my $file = shift || die "Missing parameter for subroutine call\n"; my $time = shift || die "Missing second parameter on subroutine call +\n"; my $cmd = `cat $file`; while (1){ print $cmd; sleep $time; } } # main program my $pid; # process id my @childs; # will hold all pids of our children $SIG{CHLD} = sub{wait}; # avoid Zombies $SIG{TERM} = sub{ kill -9, @childs; exit(0) }; # allow to end program by pressinc Ctrl-C or a kill call #this will hold all of our tasks my @tasks = ( { file => "msg.1", time => 10, }, { file => "msg.2", time => 2, }, { file => "msg.3", time => 5, }, ); # looping through the array of hashes and forking with each element FORLOOP: for my $i ( 0..$#tasks){ FORK: { if ($pid = fork){ # first fork # we are parent push @childs,$pid; next FORLOOP; }elsif (defined $pid){ # so we are a child sched($tasks[$i]{file},$tasks[$i]{time}); exit(0); # we should never reach this }elsif ($! =~/No more process/){ sleep 5; redo FORK; }else{ die "Something is rotten in the state or Denmark: $!\n"; } } } print "started all childs\n"; while (1){} # necessary so we can kill all of our childs
The hash is just a convenient way to handle all of your files, maybe you want to read those values from a file later. Also note the signal handling which enables you to stop the beasts with a control-c or a kill call.
Hope this helps,
C-Keen
|
|---|