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

I have never ever done IPC before (that I know of :) ). Lets say I have a program that spawns 2 children.

  • One of the children listens on a socket for data from a server (AF_INET, DGRAM).
  • The other child watches a log (using the tail -f concept -- does NOT use system(tail...)) that gets continually written to.
  • The main module (daemonized already) spawns these two kiddies off. It will react to the work they perform.
  • Now, since the child processes fork(), I still have to keep them in a loop to watch what they spit out, right? ...thing is is that I would then have two different loops of which neither are really related to each other so I don't want to nest the loops that they are in. Am I correct?

    eg.

  • This will not work because the second loop won't get used until the first one is over which means that it will never get called...
  • main: fork(child1); fork(child2); # endless loop until signal caught from # shell (child1): loop(read from child1) { react } # endless loop until signal caught from # shell (child2): loop(read from child2) { react } main END:
  • This next loop will not work because the innermost loop will be the only loop running so the outer loop will never get munched on.
  • main: fork(child1); fork(child2); # endless loop until signal caught from # shell (child1): loop(read from child1) { react # endless loop until signal caught from # shell (child2): loop(read from child2) { react } } main END:
  • I have to be able to do something like this:
    main: fork(child1); fork(child2); # endless loop until signal caught from # shell (child1): loop(true) { check child1 # call sub reacting from input; check child2 # call sub reacting from input; } # loop until a signal is caught. END main:
    How would this be done? I have read through the Perl cookbook on IPC and I can't seem to pick up how I would do something like this using the information provided in their examples and documentation. I have read perldoc perlipc but I admit that I am so new to this stuff that a lot of it is jargon that is over my head.

    I have already written the meat of the three modules. This is the biggest part that stumps me.

    I think what I really need is some documentation that doesn't talk above my head :( One of these days I will understand the high-talk but I have the terrible problem where I can get lost in the words very quickly. If anyone has a site or something out there that is easily read I would love that. I also learn quite quickly from source code. If there is some code out there that would give me an idea of how to do what I am trying to do that would be very helpful as well.

    Thanks guys.

    ----------
    - Jim

  • Replies are listed 'Best First'.
    Re: A challenge...at least for me.
    by bikeNomad (Priest) on Jun 06, 2001 at 22:18 UTC
      If what you want to do is to react to data coming from two processes, you can use select(). This will let you wait until there is data available from either of the two children. Just arrange to have each of the processes writing to a different file handle.

      Look at IO::Select for an OO interface to select.

      A possible advantage of using select is that you may not need to make a separate process to watch the log file.

    Re: A challenge...at least for me.
    by Anonymous Monk on Jun 06, 2001 at 22:30 UTC
      Make this
      pipe(*A,*B); $child1=fork; $child2=fork; close B; while(<A>){ react }
      where *every* child writes to B, only the message is formatted to distinguish the children.
        This looks very promising, however, don't you mean every child writes to A instead of B? (because it seems like you are closing B and then reading from A). Am I misinterpretting your code somehow?

        ----------
        - Jim

          A pipe links two Filehandles thus: What you syswrite into B can you read in A (or the other way around, I haven't the doku handy). Think of it as a pipe (Name says it all). On a water pipe you don't fill the tap to get water out of it, you use the other end. BTW the code was for the parent.