explanantion of fork()?

explanantion of fork()?

Post by System Administrato » Sat, 11 Apr 1998 04:00:00



if I call fork() with:

 switch(pid = fork())

I know that when 0 is returned it means success for a child being created, and you do
whatever you want the child to do, and then _exit(0);

When greater than 0 is returned, I'm under the understanding that the parent is getting
the child's pid. But what happens then? Is the child still created, or is it an error and the
child isn't created, or what?

I guess I'm just confused as how there can be 2 successes on a fork() or how i handle the > 0
one.

Email, if possible. Thanx.

-Tony
---------------------------------------------------------------------------
Anthony J. Biacco                           Network Administrator/Engineer
.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-.
         "The only way to predict the future, is to invent it."
.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-.

                      http://cygnus.ncohafmuta.com/
---------------------------------------------------------------------------

 
 
 

explanantion of fork()?

Post by Chris Engebrets » Sat, 11 Apr 1998 04:00:00




|> When greater than 0 is returned, I'm under the understanding that
|> the parent is getting the child's pid. But what happens then? Is the
|> child still created, or is it an error and the child isn't created,
|> or what?
|>
|> I guess I'm just confused as how there can be 2 successes on a
|> fork() or how i handle the > 0 one.

Because fork() is called once, and returns twice (when successful);
once to the parent and once to the child (that's the whole point of
calling fork() in the first place!)  The comp.unix.programmer FAQ
list, which you should have read before posting here, contains more
introductory information on fork().

(Followups to comp.unix.programmer; processes have nothing to do
with the C language as discussed in comp.lang.c.)

Regards,

--
Chris Engebretson - Raytheon STX Corporation | Ph#: (605)594-6829
USGS EROS Data Center, Sioux Falls, SD 57198 | Fax: (605)594-6940

Opinions are not those of  Raytheon Systems Company  or the USGS.

 
 
 

explanantion of fork()?

Post by Andrew Giert » Sat, 11 Apr 1998 04:00:00


[Followups set.]

 System> if I call fork() with:
 System>  switch(pid = fork())

 System> I know that when 0 is returned it means success for a child
 System> being created, and you do whatever you want the child to do,
 System> and then _exit(0);

 System> When greater than 0 is returned, I'm under the understanding
 System> that the parent is getting the child's pid. But what happens
 System> then? Is the child still created, or is it an error and the
 System> child isn't created, or what?

The child is created. The process divides into two almost-identical
processes inside the fork() call, and in one of them (the child) the
call returns 0, in the other (the parent) the call returns the PID of
the child.

 System> I guess I'm just confused as how there can be 2 successes on
 System> a fork() or how i handle the > 0 one.

Having fork() succeed differently in the two processes is the easiest
way to tell them apart :-)

As to how to handle it, this is the simple case:

  switch(pid = fork())
  {
      case -1: /* error */
          perror("fork failed"); /* or whatever */
          break;

      case 0: /* child */
          /* do whatever the child should do */
          _exit(0);

      default: /* parent */
          /* do whatever the parent should do */
  }
  /* this point not reached in the child, in this example, due to the _exit */
  /* so you could have put the parent stuff here instead */

But in a more complex case, it might not have been appropriate to have
the _exit() call inside the switch (see the process group example in
the FAQ).

--
Andrew.

comp.unix.programmer FAQ: see <URL: http://www.erlenstar.demon.co.uk/unix/>
                           or <URL: http://www.whitefang.com/unix/>

 
 
 

explanantion of fork()?

Post by Fletcher Glen » Sat, 11 Apr 1998 04:00:00


The best way to view fork() is to understand that at the fork()
statement you now have two independent processes executing from
the same point in the code.  fork() is a way to pursue two separate
goals at the same time with a single program source.  Try thinking
of two separate program listings - one for the parent, one for the
child.  It may make it easier to see that two execution paths
exist after the fork().

--
                Fletcher Glenn

                To email: remove "notforspam" from my return address


> if I call fork() with:

>  switch(pid = fork())

> I know that when 0 is returned it means success for a child being created, and you do
> whatever you want the child to do, and then _exit(0);

> When greater than 0 is returned, I'm under the understanding that the parent is getting
> the child's pid. But what happens then? Is the child still created, or is it an error and the
> child isn't created, or what?

> I guess I'm just confused as how there can be 2 successes on a fork() or how i handle the > 0
> one.

> Email, if possible. Thanx.

> -Tony
> ---------------------------------------------------------------------------
> Anthony J. Biacco                           Network Administrator/Engineer
> .-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-.
>          "The only way to predict the future, is to invent it."
> .-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-.

>                       http://cygnus.ncohafmuta.com/
> ---------------------------------------------------------------------------

 
 
 

explanantion of fork()?

Post by Will Ro » Sat, 11 Apr 1998 04:00:00


: if I call fork() with:

:  switch(pid = fork())

: I know that when 0 is returned it means success for a child being created, and you do
: whatever you want the child to do, and then _exit(0);

: When greater than 0 is returned, I'm under the understanding that the parent is getting
: the child's pid. But what happens then? Is the child still created, or is it an error and the
: child isn't created, or what?

: I guess I'm just confused as how there can be 2 successes on a fork() or how i handle the > 0
: one.

fork() returns twice (hence the name).  In the parent, it returns the
child's pid, and in the child it returns 0.  Note that PIDs start at 1.
It is therefore possible to distinguish between parent and child processes.
fork() returns -1 on failure, but in that case it must obviously be
in the parent.

Will

 
 
 

explanantion of fork()?

Post by System Administrato » Sun, 12 Apr 1998 04:00:00


Thanx to everyone for their responses.

-Cygnus
---------------------------------------------------------------------------
Anthony J. Biacco                           Network Administrator/Engineer
.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-.
         "The only way to predict the future, is to invent it."
.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-.

                      http://cygnus.ncohafmuta.com/
---------------------------------------------------------------------------



> : if I call fork() with:
> :  switch(pid = fork())
> : I know that when 0 is returned it means success for a child being created, and you do
> : whatever you want the child to do, and then _exit(0);
> : When greater than 0 is returned, I'm under the understanding that the parent is getting
> : the child's pid. But what happens then? Is the child still created, or is it an error and the
> : child isn't created, or what?
> : I guess I'm just confused as how there can be 2 successes on a fork() or how i handle the > 0
> : one.
> fork() returns twice (hence the name).  In the parent, it returns the
> child's pid, and in the child it returns 0.  Note that PIDs start at 1.
> It is therefore possible to distinguish between parent and child processes.
> fork() returns -1 on failure, but in that case it must obviously be
> in the parent.
> Will


 
 
 

explanantion of fork()?

Post by Randy Kaelb » Tue, 14 Apr 1998 04:00:00



> if I call fork() with:
>  switch(pid = fork())
> When greater than 0 is returned, I'm under the understanding that the
> parent is getting the child's pid. But what happens then? Is the child
> still created, or is it an error and the child isn't created, or what?

The only time no child is created is when fork() returns -1. The parent
process continues merrily along, with fork returning the pid of the child.
The child continues running, after returning 0.

Your switch would probably (in most instances) look something like this:

   switch(pid = fork())
   {
      case 0:
        do_child_things();
        break;
      case -1:
        fprintf(stderr, "fork: %s\n", sys_errlist[errno]);
        do_error_things();
        break;
      default:
        do_parent_things();
        possibly_calling_a_function();
        from_the_wait_family();
        unless_youve_ignored_SIGCHLD();
        break;
   }

--
Randy Kaelber                       "How long until my soul gets it right?

                                    I call on the resting soul of Galileo,
                                   King of night vision, King of insight."
                                        - Emily Saliers

 
 
 

1. comsat forking, forking and forking (3.2.0)


94

Has anyone an idea on what could have caused all those comsat
processes to appear in less than one night? (I got really heavy mail
traffic this nite, so I guess it's related).

Would replacing comsat with the BSD one help solving the problem?

--

   "No keyboard, press F1 to continue"

2. 1.1.40 slow to get on net

3. fork call without using function fork()

4. Could not Implement MDI in UIM/X

5. Efficiency: fork() w/ shared libraries vs. fork()/exec()

6. exportfs Equivalent

7. to fork or to pre-fork

8. smail incoming messages stall in input

9. PPP pid - it forks - right? How do I wait for the fork PID?

10. glibc: __fork() vs fork()

11. beginner fork question

12. Question: How are subshells forked

13. Newbie fork() question