Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Running more than one process (the very basics)

by bsdbudha (Initiate)
on Oct 02, 2001 at 01:41 UTC ( [id://115996]=note: print w/replies, xml ) Need Help??


in reply to Running more than one process (the very basics)
in thread Reminder program

well i tried fork(); before the subroutine calls, and it hangs as usual
i'll give those perldocs a look
remember, i want to execute 4 subroutines at once, each doing a system call to `cat file`..
and all 4 subroutines have an infinite loop with a sleep statement inside...

Replies are listed 'Best First'.
Re: Re: Running more than one process (the very basics)
by C-Keen (Monk) on Oct 02, 2001 at 01:52 UTC
    I am not sure if i got your problem right. Would you please post your code or something that will clarify the problem?

    C-Keen

      thx for sticking to the post : }
      fork();
      &a_sched;
      fork();
      &b_sched;
      fork();
      &c_sched;
      fork();
      &d_sched;
      fork();
      &e_sched;

      sub a_sched
      {
      $var = `cat a.sched`;
      print $var;
      for(;;){
      sleep 1209600;
      print $var;
      }
      }
      each *_sched sub is the same except the cat file is different
      and the number of seconds to sleep.
      what I want to do is execute each subroutine simutaneously,
      so all the reminders print at once (cat), and they keep printing depending on the sleep. maybe my use of sleep is wrong,

      any ideas much appreciated.

        There is on error in your code: sleep uses seconds as an argument. Until you want to wait forever you should choose a smaller value.
        The way you handle your processes is a little bit awkward since you do not have any control over them after the forks are run. I cam up with a little script like this:

        #!/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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://115996]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-03-28 11:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found