don@umd5.UUCP (02/18/86)
I've got a question ... Our library here is a bit shy on good Unix books, (people take them out and then never return them) and the online and Programmer's manuals haven't been able to answer my question clearly ... The question is: After issuing the fork(), I know the parent process gets the pid of the child, and the child is a copy of the parent process, but where does the execution of the child process continue from ? (assuming the child process was created with no errors) In other words, if I wanted to put an execl() in the child to overwrite the child, where would it go ? Additionally, how can I be certain that I won't ever overwrite the parent process ? Without code fragments, I've not been able to get a clear picture of how this works. AtDhVaAnNkCsE -- --==---==---==-- ".. all mimsy were the Borogroves .." ARPA: don@umd5.UMD.EDU BITNET: don%umd5@umd2 UUCP: ..!{ seismo!umcp-cs, ihnp4!rlgvax }!cvl!umd5!don (NOTE: Please mail to umcp-cs!cvl!umd5!don NOT umd5!cvl!umcp-cs!don) umcp-cs ::= mimsy.UMD.EDU | maryland.ARPA | umcp-cs.UUCP
ron@brl-smoke.ARPA (Ron Natalie <ron>) (02/21/86)
> After issuing the fork(), I know the parent process gets the pid of the > child, and the child is a copy of the parent process, but where does the > execution of the child process continue from ? (assuming the child process > was created with no errors) In other words, if I wanted to put an execl() > in the child to overwrite the child, where would it go ? Additionally, how > can I be certain that I won't ever overwrite the parent process ? > Without code fragments, I've not been able to get a clear picture of how > this works. > Neglecting certain funny versions of 4.2 as done by National Semiconductor, the fork creates a totally seperate copy of all the nonshared program sections. This is entirely analogous to having allocated room for a new running copy of the program. Every program your shell starts up is handled by fork and execl. No working UNIX would overwrite the parent when the child does something. Since the NSC memory management unit is a little better than that of a VAX they accomplish the same thing by setting all the writable areas to generate a fault when accessed by either the parent or child. It then makes a seperate copy of just that page. VFORK, in 4 BSD is a realization that many programs do fork/exec in quick succession. VFORK does not copy the data areas, but suspends the parent until the child executes a separate exec. -Ron
latham@bsdpkh.UUCP (Ken Latham) (02/22/86)
> I've got a question ... >....but where does the execution of the child process continue from ? > In other words, if I wanted to put an execl() in the child to overwrite > the child, where would it go ? A common phrase used is: ......PARENT if ( (childid = fork()) == 0) { DO execl(); } if ( childid != -1 ) werror=wait(&status); /* status being a status structure */ REST OF PARENT ....; ( Yes, I know it's not standard indentation ..... :-) ) You get two complete copies of the process executing from the same point in process with the same data, stack ... well everything EXCEPT the return code from fork(). The parent gets the PID of the kid, the kid gets 0. ( other restrictions may apply see fork(2), void where prohibited) the execl() will never be executed if the fork() call fails. If you are using OS9 however you get a NEW process as a child ... never mind I'm getting carried away. Be careful the above will NOT work if the CHILD doesn't do an exec() of some kind ( or exit inside the if ). The child would execute what was inside the if ... drop out .... execute the parent code... etc. Although you may WANT to do this some times! Ken Latham, AT&T-IS (via AGS Inc.), Orlando , FL uucp: ihnp4!bsdpkh!latham
guy@sun.uucp (Guy Harris) (02/23/86)
> Neglecting certain funny versions of 4.2 as done by National Semiconductor, > the fork creates a totally seperate copy of all the nonshared program > sections.... Other UNIX implementations do "copy-on-write" forks too, such as the paging S5 versions from AT&T-IS. I suspect some independently-done paging V7/S3/S5 versions and other 4BSD ports do so. "copy-on-write" doesn't affect the behavior of programs other than (one hopes) making them run faster, so even on "copy-on-write" systems one can think of the fork copying all the nonshared sections. -- Guy Harris {ihnp4, decvax, seismo, decwrl, ...}!sun!guy guy@sun.arpa (yes, really)
greg@utcsri.UUCP (Gregory Smith) (02/25/86)
In article <868@umd5.UUCP> don@umd5.UUCP writes: >The question is: >After issuing the fork(), I know the parent process gets the pid of the >child, and the child is a copy of the parent process, but where does the >execution of the child process continue from ? From the stork process, of course.
don@umd5.UUCP (02/28/86)
Thanks to all who responded to my question. I've posted questions to the net before (not net.lang.c), but never have I received responses of quality in great quantity. Thanks again. A friend of mine was kind enough to lend me his copy of _Advanced UNIX Programming_ by Marc J. Rochkind. Between the responses and the book, I'm sure I've got fork() figured. (I'm still waiting for the campus library to obtain copies of the books I requested from one of the other campuses.) -- --==---==---==-- ".. all mimsy were the borogoves .." ARPA: don@umd5.UMD.EDU BITNET: don%umd5@umd2 UUCP: ..!{ seismo!umcp-cs, ihnp4!rlgvax }!cvl!umd5!don (NOTE: Please mail to umcp-cs!cvl!umd5!don NOT umd5!cvl!umcp-cs!don) umcp-cs ::= mimsy.UMD.EDU | maryland.ARPA | umcp-cs.UUCP