Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watnot!watmath!clyde!cbatt!ihnp4!ptsfa!lll-lcc!styx!mordor!sri-spam!sri-unix!husc6!seismo!brl-adm!brl-sem!ron From: ron@brl-sem.UUCP Newsgroups: comp.lang.c Subject: Re: Fork and Exec Message-ID: <707@brl-sem.ARPA> Date: Wed, 1-Apr-87 10:53:39 EST Article-I.D.: brl-sem.707 Posted: Wed Apr 1 10:53:39 1987 Date-Received: Sat, 4-Apr-87 07:17:39 EST References: <6635@brl-adm.ARPA> Organization: Electronic Brain Research Lab Lines: 46 In article <6635@brl-adm.ARPA>, Primixsys!bagpiper@csvax.caltech.edu writes: > What is the difference between fork and exec in a unix environment and > furthermore how does that map into a MS-Dos environment?? > Fork: causes an exact copy of the currently running process to be made. The two copies differ only in the return that the fork function makes. The child always sees a return value of zero and the parent sees the return value which is the process id of the child (if the child wants to know the parents process ID, it can either do a getppid, which will not be the former parent process ID if that process has exitted, or the parent could have done a getpid before the fork). When processes have sections that can't be written by a user process (write protected text) or on machines with slick enough memory management, the fork will actually allow multiple processes to share unchanged regions rather than making a copy. exec: Replaces the currently running process, with the process specified in it's arguments. This obliterates the current process. As a result, the only return from exec that the calling program will ever see is an error (if it succeeds, the caller won't be there anymore). If you would like to run a new process concurrently with the existing one you need to fork first, and then obliterate one of the two processes by exec-ing the new process. Use exec by itself if you just want to "chain" into another program (this is exactly analogous and is the origin of the "exec" command in the shell). If you want to make a new process without disturbing the current process, use fork then exec. Now fork is a bit costly with slicker programs that use lots of memory. The shell frequently does fork followed by not much more than exec. This means that there is a lot of copying of the shell that lives only a few instructions before it gets wiped out by the subsequent exec. To alleviate this, Berkeley has added "vfork" to their system. Vfork, works like fork (creating a duplicate process) except that the new process uses the exact same memory image as the old one. You have to be very careful doing this, but if the new process doesn't mess with memory before doing the exec, you can get away with it and avoid a lot of memory copying. -Ron