On 07/13/2014 04:53 PM, Paul LaFollette wrote:
Kind people,
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.

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.
Thank you
Paul

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.

So you have to ask yourself: What do you gain from using Python if you eliminate all the tools Python provides?

Gary Herron





-------------------------
Paul S. LaFollette, Jr
CIS Department
Temple University
+1 215 204 6822
paul.lafolle...@temple.edu <mailto:paul.lafolle...@temple.edu>
http://knight.cis.temple.edu/~lafollet <http://knight.cis.temple.edu/%7Elafollet>

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <errno.h>

int main(int argc, char **argv)
{
  void handler(int);
  pid_t pid;

  signal(SIGCONT, handler);

  pid = fork();
  if (pid < 0) //failure
  {
    printf("Unable to fork\n");
    exit(1);
  }
  else if (pid > 0) // parent
  {
    while (1)
    {
      printf("Parent waiting for child to do something\n");
      pause();
      printf("Parent doing nifty stuff.\n");
      sleep(1);
      printf("Parent waking child\n");
      errno = 0;
      if (kill(pid, SIGCONT) < 0)
        perror("Parent failed to SIGCONT child.");
    }
  }
  else //pid == 0 so child
  {
    while (1)
    {
printf (" Child doing useful work.\n");
      sleep(1);
printf(" Child waking parent\n");
      if (kill(getppid(), SIGCONT) < 0)
        perror("Child failed to SIGCONT parent.");
printf(" Child waiting for parent to do something\n");
      pause();
    }
  }
}

void handler(int signum)
{
}
===============================================================================
Output:
Parent waiting for child to do something
                                    Child doing useful work.
                                     Child waking parent
Child waiting for parent to do something
Parent doing nifty stuff.
Parent waking child
Parent waiting for child to do something
                                    Child doing useful work.
                                     Child waking parent
Child waiting for parent to do something
Parent doing nifty stuff.
Parent waking child
Parent waiting for child to do something
                                    Child doing useful work.
                                     Child waking parent
Child waiting for parent to do something




-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to