Re: system and (v)fork
On 2001-Jul-03 22:50:18 -0700, Gregory Neil Shapiro [EMAIL PROTECTED] wrote: djhill Why wouldn't he use vfork() instead of fork()? If there is anything that modifies memory or file descripts between the fork() and exec*() call, you can't use vfork(). To expand on Gregory's answer somewhat: Traditionally, fork(2) copied the complete address space to provide two identical copies of the process. This was a fairly expensive operation, especially if the process was large and the child was just going to call exec(2). vfork(2) optimised this process by avoiding the address space duplication. Instead the parent process is stopped and the child executes in the _parents_ address space until it issues an exec(), at which point the child is given its own address space containing the newly exec'd image. The parent then continues. The child code between the fork() and subsequent exec() must be carefully written because any changes to memory (including stack) or open files will also be reflected in the parent. Modern Unices (including 4.4BSD derivatives) avoid most of the inefficiences associated with fork() by just copying the page tables: Both processes are given separate address spaces, but they both point to the same physical memory or swap. All pages are marked `read-only' so that any writes to them will trap - at which point that page will be duplicated and marked R/W for both processes. This approach provides most of the efficiency gains offered by vfork(), without the child-can-affect-the-parent oddities. Peter To Unsubscribe: send mail to [EMAIL PROTECTED] with unsubscribe freebsd-current in the body of the message
Re: system and (v)fork
vfork(2) [...] The child code between the fork() and subsequent exec() must be carefully written because any changes to memory (including stack) or open files will also be reflected in the parent. Not open files: indeed, the main thing you typically want to do before the exec() is opening, closing and duping files. Stdio FILE structures on the other hand are just like any other memory, so you probably don't want to touch them (hence the warning in the man page to use _exit() rather than exit()). -- Richard To Unsubscribe: send mail to [EMAIL PROTECTED] with unsubscribe freebsd-current in the body of the message
system and (v)fork
Hello - Reading Advanced Programming in the UNIX Environment by Richard W. Stevens, I see that he says that vfork() should be used instead of fork() when you just need to use one of the exec() functions, since it doesn't need to fully copy the address space. Later in the book, he has an example system() which uses fork() to run /bin/sh -c via the execl() function. Why wouldn't he use vfork() instead of fork()? I ran FreeBSD's version of system() both by the default (using fork()) and by using vfork() I ran a loop 1000 times that called system(echo); Here are my results: time ./app fork() vfork() 1. 4.528 3.056 0.050 0.058 2.078 1.492 2. 3.652 2.865 0.060 0.060 2.036 1.484 3. 3.735 3.022 0.068 0.041 2.031 1.506 As you can see, vfork() performed better. But, I am sure there is good reasoning for using fork() over vfork() in the system() call, and I am just curious why. Can anyone explain this? Thanks - David To Unsubscribe: send mail to [EMAIL PROTECTED] with unsubscribe freebsd-current in the body of the message
Re: system and (v)fork
djhill Reading Advanced Programming in the UNIX Environment by Richard djhill W. Stevens, I see that he says that vfork() should be used instead djhill of fork() when you just need to use one of the exec() functions, djhill since it doesn't need to fully copy the address space. djhill Later in the book, he has an example system() which uses fork() to djhill run /bin/sh -c via the execl() function. djhill Why wouldn't he use vfork() instead of fork()? If there is anything that modifies memory or file descripts between the fork() and exec*() call, you can't use vfork(). To Unsubscribe: send mail to [EMAIL PROTECTED] with unsubscribe freebsd-current in the body of the message
Re: system and (v)fork
On Tue, 3 Jul 2001 22:50:18 -0700 Gregory Neil Shapiro [EMAIL PROTECTED] wrote: djhill Reading Advanced Programming in the UNIX Environment by Richard djhill W. Stevens, I see that he says that vfork() should be used instead djhill of fork() when you just need to use one of the exec() functions, djhill since it doesn't need to fully copy the address space. djhill Later in the book, he has an example system() which uses fork() to djhill run /bin/sh -c via the execl() function. djhill Why wouldn't he use vfork() instead of fork()? If there is anything that modifies memory or file descripts between the fork() and exec*() call, you can't use vfork(). Ahh, understood. Thanks for the quick response - David To Unsubscribe: send mail to [EMAIL PROTECTED] with unsubscribe freebsd-current in the body of the message