Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 27 Dec 1996 15:10:02 -0700 (MST)
From:      Softweyr LLC <softweyr@xmission.com>
To:        kline@tera.com (Gary Kline)
Cc:        questions@freebsd.org
Subject:   Re: multi-media app runs out of forking capabilities
Message-ID:  <199612272210.PAA21484@xmission.xmission.com>
In-Reply-To: <199612271912.LAA11283@athena.tera.com> from "Gary Kline" at Dec 27, 96 11:12:30 am

next in thread | previous in thread | raw e-mail | index | archive | help
Gary Kline asked:
> 		Is there a way that one program can send a flag
> 		to another program?  Assuming yes, where is some
> 		sample code??

According to Softweyr LLC:
% Sure.  Signals are a simple, but error prone way.  A UNIX-domain socket
% or a FIFO would work well, or even a MSGQ.  You can find instructions
% on using most (or maybe all) of these in W. Richard Stevens' excellent
% books _UNIX Network Programming_ (0-13-949876-1) and _Advanced
% Programming in the UNIX Environment_ (0-201-56317-7), both from
% Prentice Hall.

> 	I've already tried a FIFO; close, but no cigar.  Looks like
> 	spawning a new proc is the bbest way.   I have an older version
> 	of Stevens' networking text.   Probably ought to check out the
> 	_Advanced_ book.   Do you know when it was published, off-hand?
> 	Anyway, thanks for the pointers.

You can find it currently at www.bookpool.com.  The copyright date is
1990, but it is certainly still "current."

If yo've already tried a FIFO and can't get your information from one
process to another, none of the other options are going to help you;
they are more generalized forms of communication.  You should carefull
consider what it is that the parent process really needs to tell the
child process.

% Your 'can't fork' messages may be caused by the per-process limits on
% child processes.  Are you gathering the status of your child processes
% with wait(2) at any point?  If not, all of your child processes will
% remain counted against your per-process limit until you wait() for them.
% Richards UNIX book will explain the UNIX process model and the relation-
% ship between parent and child processes better than I can here.  ;^)

> 	This is most probably it.  I can't do a wait because the
> 	program I'm forking infinite-loops until I change state.
> 	The wait() would wait infinitely.

Not unless you tell it to...

Use wait3 with options set to WNOHANG.  If no chile processes have
exited, it will just return 0 immediately.  You definitely need a good
book on UNIX programming; none of this is difficult, but you just
cannot find this information in the man pages.

> 	Hmm.  There's got to be other ways of doing this....

Probably, there always are.  You just need a few good lessons in

	The UNIX Way (tm)

I just found this cool little snippet of code in _UNIX Network
Programming_:

	sigchld_parent()
	{
	    union wait status;
	    int pid;

	again:
	    pid = wait3(&status, WNOHANG | WUNTRACED, (struct rusage *) 0);
	    if (pid == 0)
		return;

	    goto again;
	}

Early in your main program, you need to handle SIGCHLD with this routine:

	...
	signal(SIGCHLD, sigchld_parent);
	...


This will automagically reap (and ignore) the return status of *all*
child processes.  Does this help?

I'm certain we've gone beyone the scope of freebsd-questions now;
fell free to email directly if you have more questions.

-- 
          "Where am I, and what am I doing in this handbasket?"

Wes Peters                                                       Softweyr LLC
http://www.xmission.com/~softweyr                       softweyr@xmission.com



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199612272210.PAA21484>