Program from "Unix Network Programming" 
Author Message
 Program from "Unix Network Programming"

I wonder if someone could explain something odd about the following
program, from W.Richard Stevens' "Unix Network Programming". When I
ran it on my Linux system, it printed the following:

parent: child pid = 143, parent pid = 142
$ child: child pid = 143, parent pid = 1

The $ above is my shell prompt; after the second line the program just
hang, without producing a further shell prompt. It seems that the
parent process conditional is being executed before the child one. Why
is this? And why does the program hang? The program is:

main())
{{
     int childpid;;
     if ( (childpid = fork()) == -1) {{
         perror("can't fork");;
         exit(1);;
     } else if (childpid == 0) { {
              /* child process */
              printf("child: child pid = %d, parent pid = %d\n",,
                             getpid(), getppid());;
              exit(0);;
     } else {{
              /* parent process */
              printf("parent: child pid = %d, parent pid = %d\n",,
                             childpid, getpid());;
              exit(0);

Quote:
}
}

Also, why is the parent pid in the child process 1? I thought the
process with pid 1 was init.

If you could help, I'd be very grateful.

David King


     }

Quote:
}



Tue, 04 May 1999 03:00:00 GMT  
 Program from "Unix Network Programming"

Quote:

>I wonder if someone could explain something odd about the following
>program, from W.Richard Stevens' "Unix Network Programming". When I
>ran it on my Linux system, it printed the following:

Wrong newsgroup. You're asking about fork(), getpid(), getppid() which
as Unix system calls and have nothing to do with the C language. Try
comp.unix.programmer.

Quote:
>parent: child pid = 143, parent pid = 142
>$ child: child pid = 143, parent pid = 1

>The $ above is my shell prompt; after the second line the program just
>hang, without producing a further shell prompt. It seems that the
>parent process conditional is being executed before the child one. Why
>is this? And why does the program hang? The program is:

>main())
>{{
>     int childpid;;
>     if ( (childpid = fork()) == -1) {{

Why do you have all of these doubled up characters such as )) and {{ ?
As it standand this isn't valid C syntax.

Quote:
>         perror("can't fork");;
>         exit(1);;
>     } else if (childpid == 0) { {
>              /* child process */
>              printf("child: child pid = %d, parent pid = %d\n",,
>                             getpid(), getppid());;

I think you'll find that getpid() and getppid() don't return int, they return
pid_t which is not approriate to pass to %d. I suggest you cast it.

Quote:
>              exit(0);;
>     } else {{
>              /* parent process */
>              printf("parent: child pid = %d, parent pid = %d\n",,
>                             childpid, getpid());;
>              exit(0);
>}
>}

>Also, why is the parent pid in the child process 1? I thought the
>process with pid 1 was init.

Briefly, If the parent of a process terminates the process is inherited by
init process.

--
-----------------------------------------


-----------------------------------------



Tue, 04 May 1999 03:00:00 GMT  
 Program from "Unix Network Programming"


Quote:

>I wonder if someone could explain something odd about the following
>program, from W.Richard Stevens' "Unix Network Programming". When I
>ran it on my Linux system, it printed the following:

>parent: child pid = 143, parent pid = 142
>$ child: child pid = 143, parent pid = 1

>The $ above is my shell prompt; after the second line the program just
>hang, without producing a further shell prompt. It seems that the
>parent process conditional is being executed before the child one. Why
>is this? And why does the program hang? The program is:

>main())

[I assume this )) is a typo]
Quote:
>{{

[I assume this is a typo]

Quote:
>     int childpid;;

[I assume this ;; is a typo]

Quote:
>     if ( (childpid = fork()) == -1) {{
>         perror("can't fork");;

[I assume this ;; is a typo]
Quote:
>         exit(1);;

[I assume this is a typo]

Quote:
>     } else if (childpid == 0) { {

[I assume this {{ is a typo]
Quote:
>              /* child process */
>              printf("child: child pid = %d, parent pid = %d\n",,
>                             getpid(), getppid());;

[I assume this ;; is a typo]

Quote:
>              exit(0);;

[I assume this ;; is a typo]

Quote:
>     } else {{

[I assume this {{ is a typo]
Quote:
>              /* parent process */
>              printf("parent: child pid = %d, parent pid = %d\n",,
>                             childpid, getpid());;

[I assume this ;; is a typo]
Quote:
>              exit(0);
>}
>}

[I assume this {{ is a typo : sticky keys maybe?  ]

No, both programs have exited already.  The second "$" is your prompt and the
output from the child just appears after it.  Press return and you should get
another prompt (but you could just type a command instead).

Quote:
>Also, why is the parent pid in the child process 1? I thought the
>process with pid 1 was init.

By the time the child gets to that point, the parent program has already
exited.  Children with dead parents get adopted by init (PID 1).  This means
that what the child says is correct.

Summary:  all this is the EXPECTED behaviour.

Quote:
>If you could help, I'd be very grateful.

I hope that this does indeed help.

--
James Youngman       VG Gas Analysis Systems |The trouble with the rat-race
 Before sending advertising material, read   |is, even if you win, you're
http://www.law.cornell.edu/uscode/47/227.html|still a rat.



Tue, 04 May 1999 03:00:00 GMT  
 Program from "Unix Network Programming"

Quote:

>I wonder if someone could explain something odd about the following
>program, from W.Richard Stevens' "Unix Network Programming". When I
>ran it on my Linux system, it printed the following:

Read the name of this newsgroup _carefully_.  Can you see any of the
words "Unix", "Network", "Programming" or "Linux"?  I have to admit that
I can't.  Do I need new glasses or do you need to engage your brain when
selecting your newsgroup(s)?

Dan
--
Dan Pop
CERN, CN Division

Mail:  CERN - PPE, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland



Tue, 04 May 1999 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. "The C Programming Language" programs

2. catch "Abnomal Program Termination" signal

3. "The Practice of Programming"

4. Learning "real" Programming

5. Newbie Question: "C Programming Language"

6. setup program, defaulting to "everyone"

7. calculating "e", simple program.

8. "C"PROGRAMMING

9. shweetie Pi program "contest"

10. Having "namespace" issues with Program

11. "The C Programming Languange" (K+C)

12. "The C Programming Language"

 

 
Powered by phpBB® Forum Software