[comp.lang.c] Killing a background process from a C program

marwood@ncs.dnd.ca (Gordon Marwood) (07/19/90)

I am in the process of converting a Bourne shell script to C, and I am
having trouble finding out how to identify and kill background
processes.  With the Bourne Shell approach this was simple, using $!.

What I would like to do is start a background process at one point in the
C program, and at a later time kill it.  Currently I am invoking the
background process with system("background_process &");, but none of the
texts that I have available help me to proceed any further.

Any assistance would be appreciated.

Gordon Marwood
Internet: marwood@ncs.dnd.ca

dkeisen@Gang-of-Four.Stanford.EDU (Dave Eisen) (07/20/90)

In article <1990Jul19.151728.17448@ncs.dnd.ca> marwood@ncs.dnd.ca (Gordon Marwood) writes:
>
>What I would like to do is start a background process at one point in the
>C program, and at a later time kill it.  Currently I am invoking the
>background process with system("background_process &");, but none of the
>texts that I have available help me to proceed any further.
>

System is not a powerful enough tool for this, it is designed for only
the simplest of process control situations. You'll have to fork and
exec the new process yourself. Fork will create a new process and return
the pid of that process, at some later point use kill (pid, signal_number)
to kill it. For more details, drop me a note or (better still) pick up
a copy of Marc Rochkind's Advanced Unix Programming. (BTW -- even though
it is a UNIX book and not a C book, I think it should be mentioned
in the Frequently Asked Questions posting as an excellent reference for
C programming under UNIX.)

Followups redirected to comp.unix.questions.



--
Dave Eisen                      	    Home: (415) 323-9757
dkeisen@Gang-of-Four.Stanford.EDU           Office: (415) 967-5644
1447 N. Shoreline Blvd.
Mountain View, CA 94043

larrym@pi19.pnfi.forestry.ca (Larry Marshall) (07/20/90)

marwood@ncs.dnd.ca (Gordon Marwood) writes:

>What I would like to do is start a background process at one point in the
>C program, and at a later time kill it.  Currently I am invoking the
>background process with system("background_process &");, but none of the
>texts that I have available help me to proceed any further.

The latest Computer Language (July) has an article on manipulating Unix
processes in C.  It discusses the monitoring of PIDs and includes code for 
terminating those processes.

Larry Marshall
Petawawa National Forestry Institute

venkat@matrix.UUCP (D Venkatrangan) (07/27/90)

In article <1990Jul19.151728.17448@ncs.dnd.ca> marwood@ncs.dnd.ca (Gordon Marwood) writes:
>I am in the process of converting a Bourne shell script to C, and I am
>having trouble finding out how to identify and kill background
>processes.  With the Bourne Shell approach this was simple, using $!.
>
>What I would like to do is start a background process at one point in the
>C program, and at a later time kill it.  Currently I am invoking the
>background process with system("background_process &");, but none of the
>texts that I have available help me to proceed any further.
>
>Any assistance would be appreciated.
>

Instead of system(), try using vfork() followed by execlp().

In foreground program, do something like:

	if ((child_pid = vfork()) == -1)
		perror("vfork failed");

	if (child_pid == 0) {
		execlp(execfile, execfile, arg1, arg2, ..., (char *)0);
		/* should not return */
		perror("exec failed");
	}

	/* parent */

	/* if we get killed... */
	(void)signal(SIGINT, kill_child);

/* call kill_child() to send a user defined signal to child. */
/* or just call kill(child_pid, SIGUSR1); */

void
kill_child(sigval)
int sigval;
{
	if (sigval == SIGINT)
		kill(child_pid, SIGUSR1);
}

In child, during initialization do:

	(void)signal(SIGUSR1, childsighandler);


and define:

void
childsighandler(sigval)
int sigval;
{
	if (sigval == SIGUSR1) {	/* the parent is sending something */
		cleanup();
		exit();
	}
}