<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <div class="moz-cite-prefix">On 07/13/2014 04:53 PM, Paul LaFollette
      wrote:<br>
    </div>
    <blockquote
cite="mid:CA+dsdf0iwt_Z9iDoFTUu8XD6m70onQ3Gb2BoDDU_fjY9qF=gmA@mail.gmail.com"
      type="cite">
      <meta http-equiv="Content-Type" content="text/html;
        charset=ISO-8859-1">
      <div dir="ltr">Kind people,<br>
        I have thrown together a little C/UNIX program that forks a
        child process, then proceeds to let the child and parent
        alternate.  Either can run until it pauses itself and wakes the
        other.<br>
        <br>
        I would like to know if there be a way to create the same
        behavior in Python 3, preferably in a non-platform dependent
        fashion.  I would prefer to use processes rather than threads,
        but could live with threads if I had to.  I've studied the
        documentation for the multiprocessing and thread modules, but I
        can't see an easy way to do what I want to do.  I need minimal
        communication between processes beyond what i have described, so
        creating queues or pipes seems like overkill.  Unlike what I
        have written here, I will want to exec the child rather than
        write the whole thing in the else clause.  Is there a reasonably
        simple way to do this?  A reference to useful documentation
        would be appreciated.  Sample code even more so, of course.<br>
        Thank you<br>
        Paul<br>
      </div>
    </blockquote>
    <br>
    There is a quite reasonable way to do what you want,  but that
    involves the multiprocessing and queue modules, both of which you
    have eliminated from consideration.<br>
    <br>
    So you have to ask yourself:  What do you gain from using Python if
    you eliminate all the tools Python provides?<br>
    <br>
    Gary Herron<br>
    <br>
    <br>
    <br>
    <br>
    <blockquote
cite="mid:CA+dsdf0iwt_Z9iDoFTUu8XD6m70onQ3Gb2BoDDU_fjY9qF=gmA@mail.gmail.com"
      type="cite">
      <div dir="ltr"><br>
        -------------------------<br>
        Paul S. LaFollette, Jr<br>
        CIS Department<br>
        Temple University<br>
        +1 215 204 6822<br>
        <a moz-do-not-send="true"
          href="mailto:paul.lafollette@temple.edu">paul.lafollette@temple.edu</a><br>
        <a moz-do-not-send="true"
          href="http://knight.cis.temple.edu/%7Elafollet">http://knight.cis.temple.edu/~lafollet</a>
        <br>
        <br>
        #include <stdlib.h><br>
        #include <stdio.h><br>
        #include <unistd.h><br>
        #include <sys/types.h><br>
        #include <signal.h><br>
        #include <errno.h><br>
        <br>
        int main(int argc, char **argv)<br>
        {<br>
          void handler(int);<br>
          pid_t pid;<br>
        <br>
          signal(SIGCONT, handler);<br>
        <br>
          pid = fork();<br>
          if (pid < 0) //failure<br>
          {<br>
            printf("Unable to fork\n");<br>
            exit(1);<br>
          }<br>
          else if (pid > 0) // parent<br>
          {<br>
            while (1)<br>
            {<br>
              printf("Parent waiting for child to do something\n");<br>
              pause();<br>
              printf("Parent doing nifty stuff.\n");<br>
              sleep(1);<br>
              printf("Parent waking child\n");<br>
              errno = 0;<br>
              if (kill(pid, SIGCONT) < 0)<br>
                perror("Parent failed to SIGCONT child.");<br>
            }<br>
          }<br>
          else //pid == 0 so child<br>
          {<br>
            while (1)<br>
            {<br>
              printf ("                                    Child doing
        useful work.\n");<br>
              sleep(1);<br>
              printf("                                     Child waking
        parent\n");<br>
              if (kill(getppid(), SIGCONT) < 0)<br>
                perror("Child failed to SIGCONT parent.");<br>
              printf("                                     Child waiting
        for parent to do something\n");<br>
              pause();<br>
            }<br>
          }<br>
        }<br>
        <br>
        void handler(int signum)<br>
        {<br>
        }<br>
===============================================================================<br>
        Output:<br>
        Parent waiting for child to do something<br>
                                            Child doing useful work.<br>
                                             Child waking parent<br>
                                             Child waiting for parent to
        do something<br>
        Parent doing nifty stuff.<br>
        Parent waking child<br>
        Parent waiting for child to do something<br>
                                            Child doing useful work.<br>
                                             Child waking parent<br>
                                             Child waiting for parent to
        do something<br>
        Parent doing nifty stuff.<br>
        Parent waking child<br>
        Parent waiting for child to do something<br>
                                            Child doing useful work.<br>
                                             Child waking parent<br>
                                             Child waiting for parent to
        do something<br>
        <br>
      </div>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
    </blockquote>
    <br>
  </body>
</html>