[comp.windows.x] CONTROL C: one solution

Francois.Bitz@SAM.CS.CMU.EDU (08/30/89)

I am posting this since I got quite a few requests
to send back what I found about how to interrupt
a process...

The solution which works fine (for me) is to do the following:

Write a simple program that starts up the main
X application and then pop-ups a one  button menu...
which when pressed will send a SIGINT interrupt to
the application. Note that in some Unix operating systems
there are 1-2 USER definable (SIGUSER1,..) that could be
used  instead of SIGINT; but  this is less portable
than SIGINT. Below  are the listings of a simple program
to test  this. The flag ENB_INTR can be set to 0 when
executing critical sections (where no interrupts are
desired); setjmp(int_catch) can be written anywhere
where we want the program to 'jump' when interrupts
occur (usually at the top of an  application loop).
These have worked for me on Unix Sys V and 4.3 bsd.

/******************** PROGRAM #1 **********************/
/* 	THIS starts up X application 		     */
#include <stdio.h>
#include <signal.h>
#include <setjmp.h>

int pid=0;

main()
{

pid = fork();

if (pid == -1) {
	printf("error in fork()\n");
	}
if (pid ==0) {	/* we are in child process now     */
		/* printf("in child process \n");  */
        execl("application",0);
        exit(1);
        }
/* we are in parent process here */

/* could pop-up a small widget now ...  .*/
/* when button pressed then we execute	 */
/*	kill(pid,SIGINT); 		 */

/* or could write the following for test:*/
while(1) {
	/* check event for pressed key.... */
	/* if pressed button 		   */
		/* NOTE: getchar is  here just for test	      */
		/* could be replaced with checking events.... */
	getchar();	
	kill(pid,SIGINT); 
	}

}


/******************** PROGRAM #2 **********************/
/*	 application.c				      */
/* 	THIS is X application 			      */

#include <signal.h>
#include <setjmp.h>

jmp_buf int_catch;
int ENB_INTR=0;

void handle_int()
{
if (ENB_INTR) { longjmp(int_catch);	}  
	else {	/* nothing happens */	}
}


main()
{

signal(SIGINT,handle_int);

setjmp(int_catch);	/* where to jump back after inter. */
ENB_INTR=1;

while(1) {

	/* do application stuff */

	}
}