parent/child

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2008
Posts: 12
Reputation: peaceful_soul is an unknown quantity at this point 
Solved Threads: 0
peaceful_soul peaceful_soul is offline Offline
Newbie Poster

parent/child

 
0
  #1
Feb 29th, 2008
hello friends

well can anybody tell me why does this code
  1. for (i = 0; i < 10; i++){
  2. childpid = fork();
  3.  
  4. if (0 == childpid){ /* This is the line that is different in parts A and B. */
  5. execlp("some_prog", "some_prog", (char *)0);
  6.  
  7. exit(2);
  8. }
  9. else if (childpid < 0){
  10. fprintf(stderr, "Error with fork.\n");
  11. exit(1);
  12. }
create children from the same parent ( the main method i assume)

and this code:
  1. for (i = 0; i < 10; i++){
  2. childpid = fork();
  3.  
  4. if (childpid > 0){ /* This is the line that is different in parts A and B. */
  5. execlp("some_prog", "some_prog", (char *)0);
  6. fprintf(stderr, "Error with exec.\n");
  7. exit(2);
  8. }
  9. else if (childpid < 0){
  10. fprintf(stderr, "Error with fork.\n");
  11. exit(1);
  12. }
  13. }
Create this a child that will create a child and we will have a relation like this

Parent-->Child-->Child-->Child-->....and so on.

thank you
Last edited by Ancient Dragon; Feb 29th, 2008 at 6:09 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: parent/child

 
0
  #2
Feb 29th, 2008
If you google the fork() command you'll learn why.

  1. t_pid pid;
  2.  
  3. pid = fork();
  4.  
  5. if (pid > 0) {
  6. printf( "I am the parent. My child's PID is %u\n", pid );
  7. }
  8. else if (pid == 0) {
  9. puts( "I am the child." );
  10. }
  11. else {
  12. puts( "I am the parent. I lost my fork." );
  13. }

Now you can look back and see what the difference is.

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 12
Reputation: peaceful_soul is an unknown quantity at this point 
Solved Threads: 0
peaceful_soul peaceful_soul is offline Offline
Newbie Poster

Re: parent/child

 
0
  #3
Feb 29th, 2008
i did google it ...i know it return either 0 for the child...-1 error and childip for the parent..but still i cannot know why .. can u help?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,454
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1476
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: parent/child

 
0
  #4
Feb 29th, 2008
you need to read the description for fork()
On success, the PID of the child process is returned in
the parent's thread of execution, and a 0 is returned in
the child's thread of execution. On failure, a -1 will be
returned in the parent's context, no child process will be
created, and errno will be set appropriately.
The second program you posted never checks for existance of the child so it just continues to create children of children etc. My guess is that when i == 0 the program will create 9 * 8* 7 * 6 ... *1, or 9! children.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 5
Reputation: sukhmal.sena is an unknown quantity at this point 
Solved Threads: 1
sukhmal.sena sukhmal.sena is offline Offline
Newbie Poster

Re: parent/child

 
0
  #5
Feb 29th, 2008
  1. execlp("some_prog", "some_prog", (char *)0);

This is because of the exec functions replace the program running in a process with another program.
When a program calls an exec function, that process immediately ceases executing that
program and begins executing a new program from the beginning, assuming that the
exec call doesn’t encounter an error.

So now if you consider the firstcase:
  1. for (i = 0; i < 10; i++){
  2. childpid = fork();
  3.  
  4. if (0 == childpid){ /* This is the line that is different in parts A and B. */
  5. execlp("some_prog", "some_prog", (char *)0);
  6.  
  7. exit(2);
  8. }
  9. else if (childpid < 0){
  10. fprintf(stderr, "Error with fork.\n");
  11. exit(1);
  12. }
Here exec function is called in the child process, so whenever a new child is created in the for loop, that child goes on to run as new program("some prog") and will not return unless it encounters an error and the for loop is being run in the parent process.

Where as in the second case:
  1. for (i = 0; i < 10; i++){
  2. childpid = fork();
  3.  
  4. if (childpid > 0){ /* This is the line that is different in parts A and B. */
  5. execlp("some_prog", "some_prog", (char *)0);
  6. fprintf(stderr, "Error with exec.\n");
  7. exit(2);
  8. }
  9. else if (childpid < 0){
  10. fprintf(stderr, "Error with fork.\n");
  11. exit(1);
  12. }
  13. }
Here exec function is called in the parent process, so parent goes on to run new program ("some prog") and the child will continue the loop.

Hope you got a better understanding of the issue at hand.

Happy Coding,
/Sukhmal
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 12
Reputation: peaceful_soul is an unknown quantity at this point 
Solved Threads: 0
peaceful_soul peaceful_soul is offline Offline
Newbie Poster

Re: parent/child

 
0
  #6
Mar 3rd, 2008
alright thanks guys :=)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC