| | |
parent/child
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Feb 2008
Posts: 12
Reputation:
Solved Threads: 0
hello friends 
well can anybody tell me why does this code
create children from the same parent ( the main method i assume)
and this code:
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

well can anybody tell me why does this code
c Syntax (Toggle Plain Text)
for (i = 0; i < 10; i++){ childpid = fork(); if (0 == childpid){ /* This is the line that is different in parts A and B. */ execlp("some_prog", "some_prog", (char *)0); exit(2); } else if (childpid < 0){ fprintf(stderr, "Error with fork.\n"); exit(1); }
and this code:
c Syntax (Toggle Plain Text)
for (i = 0; i < 10; i++){ childpid = fork(); if (childpid > 0){ /* This is the line that is different in parts A and B. */ execlp("some_prog", "some_prog", (char *)0); fprintf(stderr, "Error with exec.\n"); exit(2); } else if (childpid < 0){ fprintf(stderr, "Error with fork.\n"); exit(1); } }
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
If you google the fork() command you'll learn why.
Now you can look back and see what the difference is.
Hope this helps.
C Syntax (Toggle Plain Text)
t_pid pid; pid = fork(); if (pid > 0) { printf( "I am the parent. My child's PID is %u\n", pid ); } else if (pid == 0) { puts( "I am the child." ); } else { puts( "I am the parent. I lost my fork." ); }
Now you can look back and see what the difference is.
Hope this helps.
you need to read the description for fork()
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.
•
•
•
•
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.
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.
•
•
Join Date: Feb 2008
Posts: 5
Reputation:
Solved Threads: 1
C Syntax (Toggle Plain Text)
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:
C Syntax (Toggle Plain Text)
for (i = 0; i < 10; i++){ childpid = fork(); if (0 == childpid){ /* This is the line that is different in parts A and B. */ execlp("some_prog", "some_prog", (char *)0); exit(2); } else if (childpid < 0){ fprintf(stderr, "Error with fork.\n"); exit(1); }
Where as in the second case:
C Syntax (Toggle Plain Text)
for (i = 0; i < 10; i++){ childpid = fork(); if (childpid > 0){ /* This is the line that is different in parts A and B. */ execlp("some_prog", "some_prog", (char *)0); fprintf(stderr, "Error with exec.\n"); exit(2); } else if (childpid < 0){ fprintf(stderr, "Error with fork.\n"); exit(1); } }
Hope you got a better understanding of the issue at hand.
Happy Coding,
/Sukhmal
![]() |
Similar Threads
- Parent/Child Windows References (JavaScript / DHTML / AJAX)
- Parent / child class problems (C++)
- ASP.NET 2.0, Parent/Child Data Control? (ASP.NET)
Other Threads in the C Forum
- Previous Thread: Source code for printf, scanf, cin, cout?
- Next Thread: boundary values
| Thread Tools | Search this Thread |
* ansi api array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile createcopyoffile createprocess() csyntax directory dynamic fflush file floatingpointvalidation fork forloop frequency getlasterror getlogicaldrivestrin givemetehcodez graphics gtkgcurlcompiling gtkwinlinux hardware highest homework i/o ide inches initialization intmain() iso km license linked linkedlist linux linuxsegmentationfault list logical_drives loopinsideloop. lowest match matrix microsoft motherboard mqqueue multi mysql oddnumber odf open opendocumentformat openwebfoundation pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string strings suggestions test testautomation unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi






