bsdbudha has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to write a script where you enter in a reminder, its priority, and once you run it, the reminder will print " "; according to the priority (6 times a day to twice a month). What I want to do in kicking off the program is execute 4 separate scripts all at once, within the main script, that all print the reminders listed for each priority, and sleep (# of seconds) according to the priority. Well this seems to be not possible, it will just hang on the first script executed until a ctrl C is applied, and so on for each of the four script. 1) besides running cron jobs for each script, is there any way to simulateously execute 4 scripts at once? 2) : D Has anyone wrote a similar program like this before and open sourced it? Please don't flame me too hard, my first posting. ; ]

Replies are listed 'Best First'.
Running more than one process (the very basics)
by C-Keen (Monk) on Oct 01, 2001 at 20:20 UTC
    Well first of all congrats to your first posting! It would be easier to read if you'use html tags to format your post. See also the Site How To.

    To your questions: It is possible to run 4 scripts simultanously by using the fork function. Ifyou are running a unix like OS this should work, i don't know about windows systems though.
    Back to fork -- er work: The fork function will create a new process and return the process id of the created process - known as child process. The Camel Book states the safest way to check fork for errors:

    FORK: { if ($pid == fork){ #deal with the parent process #pid of child is stored in $pid }elsif (defined $pid){ #handling child process #parent pid is available via the getppid function }elsif ($! =~/No more processes/){ # EAGAIN, this error we should be able to fix sleep 5; redo FORK; } else { #this should'nt happen at all die "cannot execute fork: $!\n"; }

    Don't forget to do a reset on all your open buffers with setting $|. To avoid Zombies you should wait for your childs to exit. Use always the exit() function to end a process! The function used to wait for a child is as you may have guessed wait wait will wait and return whenever a child process exits. The status of the process is then stored in $? and the return value represents the process id of the exiting child process. If there is no child process at al you will get a -1 back. Another way to avoid Zombies is to install a Signal handler that will take care of you SIGCHLD signals: $SIG{CHLD} = sub(wait);

    I hope this is enough to get you going. For reference use perldoc -f fork and perldoc -f wait.

    Regards,
    C-Keen

    Edit by tye

      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...
        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

Re: Reminder program
by jeroenes (Priest) on Oct 01, 2001 at 20:14 UTC
    Are you sure your sub-scripts close at all?

    You always can try forking, to fork them off and run for themselves. Another Good Thing is Proc::Daemon.

    HTH,
    Jeroen
    "We are not alone"(FZ)

      well the way i've thought it up, the scripts aren't supposed to close at all.. they: print "go wash your car"; for (;;){ sleep 1209600; print "go wash your car"; } #instead of "go wash your car", its `cat sched.lst` and there is 4 scripts like this that get kicked off within the main script; if possible i want to execute simulatanously.
        The only way you can pull that off is by forking your scripts. Otherwise they will hang the calling script.

        Of course there are terminal issues, so make sure all the scripts are run in the background, otherwise there is only one script that can print. Also mind you the buffering of your OS.

        It is probably better to keep it all in one script.

        Cheers,

        Jeroen

        PS: You can use simple HTML tags and the CODE tag in your posts.