UNIX and/or Gosling Emacs problem.

Chris Torek chris at umcp-cs.UUCP
Fri Aug 30 01:04:28 AEST 1985


If you want your program to run "in the background", it should do its
own forking *and* close fd's 0, 1, and 2 *and* (to be nice) it should
probably give up its control terminal as well.

Note that I do not mean "background" in the sense the C shell uses.

#include <stdio.h>
#include <sys/ioctl.h>

/*
 * Continue running as a completely detached process.
 */
backgroundify()
{
	register int pid, tt;

	fflush(stdout);		/* clean up */
	fflush(stderr);
	if ((pid = vfork()) < 0) {
		perror("backgroundify: fork");
		return (-1);
	}
	if (pid)		/* parent */
		exit(0);
	if ((tt = open("/dev/tty", 2)) >= 0) {
		(void) ioctl(tt, TIOCNOTTY, (char *)0);
		(void) close(tt);
	}
	(void) close(0);
	(void) close(1);
	(void) close(2);
	return (0);
}
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 4251)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris at umcp-cs		ARPA:	chris at maryland



More information about the Comp.unix mailing list