<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><br>-------------------------<br>Paul S. LaFollette, Jr<br>CIS Department<br>Temple University<br>+1 215 204 6822<br><a href="mailto:paul.lafollette@temple.edu">paul.lafollette@temple.edu</a><br><a href="http://knight.cis.temple.edu/~lafollet">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>