Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

is sharing variables between forked process possible??

by sagar_kempe (Initiate)
on Feb 17, 2006 at 06:53 UTC ( [id://530871]=perlquestion: print w/replies, xml ) Need Help??

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

The steps for the script are as below:
1. Fork the main process into Child & Parent processes.
2. Child (PID ==0) will do some processing (testing that can be conformance or use case anything)
3. Meanwhile parent will run a timer that will have a count set at starting of script
4. If the child process completes before the final count then we have to stop the timer that runs on the parent process
5. If the Child process does not complete before the count (i.e in case the child process hangs) then the parent process will kill the child process.
I don’t have any problem executing step 5. That works totally fine.

The only problem is to execute step 4 that I’m not able to stop the timer on parent process if the child process completes before timer reaches its final count. The parent has to stop the timer if the child process ends before the timer reaches final count.

if (!defined($kidpid = fork())){ # fork returned undef, so failed die "cannot fork: $!"; }elsif ($kidpid == 0) { # fork returned 0, so this branch is the child $TestResult = &$TEST_PROCESS($_[0], $_[1]); # &$TEST_PROCESS is a subroutine that is written in some other file th +at will return 1 if success exit; # terminate the child thread... } else { for ($timer=0;$timer<=$TEST_TIMEOUT;$timer++) {


Here I want to check the status of the child process .i.e. if it is running or not. If the child process does not end before $TEST_TIMEOUT(defined in another script) I want to kill the child process.

The only problem I face is how to check the status of the child process.

Can i share a varible between the child & parent process that will get updated when the child process completes?
} }

Edited by planetscape - fixed code tags

Replies are listed 'Best First'.
Re: is sharing variables between forked process possible??
by salva (Canon) on Feb 17, 2006 at 09:15 UTC
    To share data between different processes you can use a module like IPC::Shareable.

    But if all you need is to set a timer on the child, just use alarm $timeout on the child after forking, and after the code that can hang alarm 0.

    If the timeout expires, as you have not defined any handler for ALRM signal, the child will die, and you will be able to see that on the parent looking at $? (see perlvar doc).

Re: is sharing variables between forked process possible??
by acid06 (Friar) on Feb 17, 2006 at 18:56 UTC
    You could try using forks with forks::shared.


    acid06
    perl -e "print pack('h*', 16369646), scalar reverse $="
Re: is sharing variables between forked process possible??
by vennirajan (Friar) on Feb 17, 2006 at 07:17 UTC
    You can share the variables between forked process by using the function "vfork"

    Regards,
    S.Venni Rajan.
    "A Flair For Excellence."
                     BK Systems.

      another way is to use shared memory to have same value visible from each parent and childs process.

      this is an example:

      
      $ipckey = IPC_PRIVATE; 
      $idshm = shmget( $ipckey, 200, 0666 ) || die "\nCreation shared memory failed $! \n";
      
      $statusproc = "00000";
      shmwrite( $idshm, $statusproc , 0, 100 ) || die "shmwrite $!";
      
      if ( $pid == 0 )
      {
             # do what I  have to do .............
            # end of task
             $statusproc = "1";
             shmwrite( $idshm, $statusproc , 0, 1 ) || warn "\n\n shmwrite $! \n";
      }
      else
      {
          ................
               # check statusproc  from time to time 
              shmread( $idshm, $statusproc , 0, 1 ) || warn "\n\n shmread $! \n";
               if(  $statusproc  eq "1" )  # the child has ended it's task
      
      
        If you are going to use this method, be sure to check for any left over memory segments when done, with ipcs (on linux). These segments are not automatically cleaned up, although some modules may do it for you. To me, it's one of the drawbacks of this form of IPC, especially if your program gets killed before it can clean up, you may have a "zombie-like" shared memory segment (possibly with 777 permissions) floating around in your ram. See forking with Storable and IPC::ShareLite for example.

        I'm not really a human, but I play one on earth. flash japh
      How? vfork is not available from perl.

      And anyway, the manual page for the syscall (on Linux) says:

      The vfork() function has the same effect as fork(), except that the behaviour is undefined if the process created by vfork() either modifies any data other than a variable of type pid_t used to store the return value from vfork(), or returns from the function in which vfork() was called, or calls any other function before successfully calling _exit() or one of the exec() family of functions.

      It doesn't look like a good option!!!

        Not entirely true. You can configure your Perl to use the system vfork() instead of the system fork(). This, however, is not recommended and can lead to unexpected breakages.

      What and where is this "vfork" in Perl?

      I'm not really a human, but I play one on earth. flash japh

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-24 08:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found