From mboxrd@z Thu Jan 1 00:00:00 1970 Message-ID: <212c59d55eb25badcbc00c9fe4121065@vitanuova.com> To: 9fans@cse.psu.edu Subject: Re: [9fans] Fork: useless and painful? From: rog@vitanuova.com In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit Date: Thu, 10 Jul 2003 15:59:34 +0100 Topicbox-Message-UUID: f2b5d788-eacb-11e9-9e20-41e7f4b1d025 > I'm hearing the claim that fork is "useless and painful" (from an IBM K42 > guy). > > Maybe it's just me, but I've always liked fork. fork's kinda nice, but can be confusing. plan9 has really moved away from fork for multithreaded programs towards the "spawn" style proccreate. (rfork for other resource attributes is quite different, i think) a day or so ago, i had occasion to write the following code: int mpipe(void) { char buf[BUFSIZE]; int n, p[2]; pipe(p); if(fork() == 0){ if(fork() == 0){ close(p[1]); return p[0]; } close(p[0]); while((n = read(0, buf, sizeof(buf))) > 0) write(p[1], buf, n); exits(nil); } close(p[0]); while((n = read(p[1], buf, sizeof(buf))) > 0) write(1, buf, n); exits(nil); } there's nothing particularly complicated going on and i don't *think* the implementation is particularly obtuse, but i don't find reading that code straightforward. here's a different structure to the same end: int mpipe(void) { int p[2]; pipe(p); spawn copy(0, p[1]); spawn copy(p[1], 1); return p[0]; } void copy(int fd0, int fd1) { char buf[BUFSIZE]; int n; while((n = read(fd0, buf, sizeof(buf))) > 0) write(fd1, buf, n); } ok, i cheated, assuming that C has been given a "spawn" statement similar to Limbo's. nonetheless, i think the structure itself is clearer, and the meaning of the code more transparent - no possibility of accidentally duplicating the *caller* of mpipe... given that forking a process in plan 9, unlike unix, is not synonymous with forking its file descriptors, surely the only real reason to prefer it is that the call fits more nicely into the C syntax; one doesn't have to bypass typechecking in order to pass arguments to a new process. how often does anyone do anything with fork that could not be done at least as easily with spawn?