[comp.lang.c] fork

eric@ux.acs.umn.edu (Merlinus Ambrosius) (11/10/90)

Can you explain why in this piece of code, fork() is returning a value
other than -1 or 0?  When it works fine in other situations in the same
program?  I tried changing 
    switch (fork ()) {
to
    switch (fix = fork ()) {
where fix is declared as an int, no difference (as expected), fix does get
the pid of the child.

    switch (fork ()) {
    case -1:
        Msg (errno, "fork");
        /*NOTREACHED*/
    case 0:
        break;
    default:
        Attacher ();
        /*NOTREACHED*/
    }

It always ends up in the default case, which shouldn't be happening.
For now, I'm just going to stick a break in the default before it calls
Attacher(), but any ideas on what could be going on here would be wonderful.

Thanks,
		Eric
-- 
/----------"Oh carrots are divine, you get a dozen for dime, its maaaagic."--
|Eric (the "Mentat-Philosopher") Hendrickson	      University of Minnesota
|eric@ux.acs.umn.edu	ehend@hp370b.cfsmo.honeywell.com   The game is afoot!
\---"What does 'masochist' and 'amnesia' mean?   Beats me, I don't know."----

gwyn@smoke.brl.mil (Doug Gwyn) (11/10/90)

In article <2691@ux.acs.umn.edu> eric@ux.acs.umn.edu (Merlinus Ambrosius) writes:
>Can you explain why in this piece of code, fork() is returning a value
>other than -1 or 0?

Because fork() returns:
	-1		if unable to create another process
	0		in the newly created process branch
	the process id	of the new process, in the parent branch

By the way, this is a UNIX-specific question, not a C language question.

>... any ideas on what could be going on here would be wonderful.

I would think they would be necessary.

ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) (11/10/90)

In article <2691@ux.acs.umn.edu>, eric@ux.acs.umn.edu (Merlinus Ambrosius) writes:
> Can you explain why in this piece of code, fork() is returning a value
> other than -1 or 0?

Because it ought to.  RTFM.  If you're talking about the UNIX fork(2)
system call, it yields one of three results:
    -1 => something went wrong, we've still just one process and this is it
     0 => forking worked, and _this_ process is the child
     N => forking worked, _this_ process is the parent, and the
	  child process has process it N.  If you are going to wait for
	  the child to finish, you _need_ this number.
(Look at the DIAGNOSTICS section of the fork(2) manual page.)

> |Eric (the "Mentat-Philosopher") Hendrickson	      University of Minnesota

Um, if you're a Mentat, how come you need a computer?  Mentats were
supposed to be the human replacements for computers after the Butlerian Jihad.
-- 
The problem about real life is that moving one's knight to QB3
may always be replied to with a lob across the net.  --Alasdair Macintyre.

dak@sq.sq.com (David A Keldsen) (11/15/90)

>In article <2691@ux.acs.umn.edu>, (Merlinus Ambrosius) writes:

[A question about fork()]

ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) writes:

>Because it ought to.  RTFM.  

[...deleted to remove useful content...]

>> |Eric (the "Mentat-Philosopher") Hendrickson	      University of Minnesota

>Um, if you're a Mentat, how come you need a computer?  Mentats were
>supposed to be the human replacements for computers after the Butlerian Jihad.

Well, obviously this mentat is working on a hidden project on Ix.
Recall that Ix skirted the limits of what was legal...

It would be called "Plan Ix," I suppose.  Which just goes to show, this
*really should* be in comp.unix.questions.  ;-)

Dak
-- 
David A. 'Dak' Keldsen of SoftQuad, Inc. email: dak@sq.com  phone: 416-963-8337
"You needn't go on making remarks like that," Humpty Dumpty said:  "they're
not sensible, and they put me out."
     -- _Through the Looking Glass & What Alice Found There_ by Lewis Carroll

eager@ringworld.Eng.Sun.COM (Michael J. Eager) (11/15/90)

In article <2691@ux.acs.umn.edu> eric@ux.acs.umn.edu (Merlinus Ambrosius) writes:
>Can you explain why in this piece of code, fork() is returning a value
>other than -1 or 0?  When it works fine in other situations in the same
>
>    switch (fork ()) {
>    case -1:
>        Msg (errno, "fork");
>        /*NOTREACHED*/
>    case 0:
>        break;
>    default:
>        Attacher ();
>        /*NOTREACHED*/
>    }


RTFM :-)
RETURN VALUES
     On success, fork()  returns  0  to  the  child  process  and
     returns  the  process  ID of the child process to the parent
     process.  On failure, fork() returns -1 to the  parent  pro-
     cess, sets errno to indicate the error, and no child process
     is created.

It looks like fork is returning the PID of the child process. Isn't
this what you wanted?

-- Mike Eager