From: sharp@AICENTER.MDC.COM (Dave Sharp)
Newsgroups: comp.windows.x.motif
Subject: Problem with XtAppAddInput reading from child process.
Date: 26 Aug 92 13:22:06 GMT
Distribution: inet
Organization: The Internet

Netters-

	I am posting the following for a friend. Please respond
to me. Thanks.

--Dave Sharp			sharp@aicenter.mdc.com
--McDonnell-Douglas Corp

------------------------------------------------------------

We are writing a program that needs to spawn children to handle 
semaphores from external processes.  Under certain conditions, the
child will need to interrupt the parent to pass along certain 
high priority information.

We were hoping to use XtAppAddInput to implement a communications
interrupt handler, but have not had any success in getting
it to work.

Below is a sample program that should work according to all the
books, but doesn't.  The parent creates a pipe, forks a child and
sets up XtAppAddInput to trigger on data in the pipe so that it can be
read. If the data is directly read by the parent after waiting for
a while, the data is read correctly. Our suspicion is therefore
focused on the XtAppAddInput.

The child simply writes on the pipe back to the parent.

If anyone can improve our minds as to what we are doing incorrectly, 
it would be most appreciated.

We are currently running on Sun SPARCstations, SunOS 4.1.2 and Motif
1.1.3.

Thanks,
Bob Byrne

/***********************************************************************/

#include <stdio.h>
#include <time.h>
#include <X11/Intrinsic.h>

#define DATA "This is a test message."

struct tm *ptr;
time_t lt;       
static int sockets[2] = {0};
int child;
XtInputId val;
int i;
float j;
XtAppContext app_context;
char buf[1024];

void readit(void);
void print_time(void);
void print_time2(void);


void main(void)
{
/*
  Initialize the X interface
*/
   XtToolkitInitialize();
   app_context = XtCreateApplicationContext();

/*
  Create the communications pipes
*/
   if (pipe(sockets) < 0) {
      perror("pipe open");
      exit(10);
   }

/*
  Fork off the child to send us data.
*/
   if ((child=fork()) == -1)
      perror("fork");

   if (child) {
/*
** parent process
*/
      printf("Child pid: %i\n", child);
      print_time();
      close(sockets[1]); 
      val = XtAppAddInput(app_context,
			  sockets[0],
			  XtInputReadMask,
			  readit,
			  NULL);
      printf("XtAppAddInput ID is: %i\n", val);

/* 
 Do-Nothing Loop to chew time
*/
      for (i=0; i<7500000; i++) {
	 j = i * 3.1415927;
      }

/* READIT HERE WORKS! */

/*
  print the data sent from the child
*/
      printf("-->%s###\n", buf);
      print_time();
      close(sockets[0]);
   }

   else {
/*
** child process
*/
      sleep(1);
      close(sockets[0]);
      print_time2();

      if(write(sockets[1], DATA, sizeof(DATA)) < 0)
	 perror("child write");
      close(sockets[1]);
      print_time2();
   }

}

void readit(void)
{
   printf("In readit\n");
   if (read(sockets[0], buf, sizeof(DATA)) < 0)
      perror("parent read");
}

void print_time(void)
{
   lt = time(NULL);
   ptr = localtime(&lt);
   printf(asctime(ptr));
}
void print_time2(void)
{
   lt = time(NULL);
   ptr = localtime(&lt);
   printf("*****");
   printf(asctime(ptr));
}
Newsgroups: comp.windows.x.motif