putting C programs in the background

David Sherman dave at lsuc.UUCP
Fri Jan 25 07:35:57 AEST 1985


||	pid = fork();
||	if( pid < 0 )
||	{
||	    /* Print error message */
||	    exit(1);
||	}
||	else if( pid > 0 )
||	{
||	    /* parent */
||	    exit(0);
||	}
||	/* else pid == 0, this is the child.  Continue */

It's all stylistics, but my personal preference, particularly if the
parent is going to wait around, is

	switch(fork())
	{
	case -1;
		/* error message, maybe try again later */
		break;
	case 0:	/* child */
		/* exec or whatever */
	default:	/* parent */
		/* do things, then wait() */
	}

Of course, if done properly and not for some quick hack, that would
be switch(pid=fork()), and so on.

Dave Sherman
-- 
{utzoo pesnta nrcaero utcs}!lsuc!dave
{allegra decvax ihnp4 linus}!utcsrgv!lsuc!dave



More information about the Comp.lang.c mailing list