From owner-freebsd-hackers Tue Mar 23 5: 9:10 1999 Delivered-To: freebsd-hackers@freebsd.org Received: from post-20.mail.demon.net (post-20.mail.demon.net [194.217.242.27]) by hub.freebsd.org (Postfix) with ESMTP id B11EB14C2B for ; Tue, 23 Mar 1999 05:09:07 -0800 (PST) (envelope-from marko@uk.radan.com) Received: from [158.152.75.22] (helo=uk.radan.com) by post-20.mail.demon.net with smtp (Exim 2.10 #2) id 10PQvD-0002oa-0K for hackers@freebsd.org; Tue, 23 Mar 1999 13:08:44 +0000 Organisation: Radan Computational Ltd., Bath, UK. Phone: +44-1225-320320 Fax: +44-1225-320311 Received: from support-3.uk.radan.com (support-3 [193.114.228.220]) by uk.radan.com (8.6.10/8.6.10) with ESMTP id NAA01358 for ; Tue, 23 Mar 1999 13:08:40 GMT Received: from uk.radan.com by support-3.uk.radan.com (SMI-8.6/SMI-SVR4) id NAA05722; Tue, 23 Mar 1999 13:08:37 GMT Message-ID: <36F79238.5FC1DA7C@uk.radan.com> Date: Tue, 23 Mar 1999 13:08:08 +0000 From: Mark Ovens Organization: Radan Computational Ltd X-Mailer: Mozilla 4.5 [en] (X11; I; SunOS 4.1.3_U1 sun4m) X-Accept-Language: en-GB MIME-Version: 1.0 To: hackers@freebsd.org Subject: Problem with fork() in 3.1 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG Hi, Firstly, my apologies if this is an inappropriate question to post here, but I have asked in -questions and received no response and I am completely stumped. If anyone could spare a couple of minutes to look at this for me I would be very grateful (my learning curve is currently flat-lining). I have been learning about Inter Process Comms and have written the 2 progs included below. It all worked under 2.2.8 however when I u/g to 3.1-R (from the CD) it no longer works :-(. Under 2.2.8 the output was: % demo RESULT! - Circumference-106.814150 % however, under 3.1 this happens: % demo RESULT! - % The string returned from d2c doesn't appear. This happens (in 3.1) with both the 2.2.8 compiled progs (aout) and the 3.1 compiled progs (ELF). If I add ``sleep(5)'' to both progs ``ps'' confirms that the exec()'d program (d2c) is running. Someone else posted a message to -questions with exactly the same problem using rfork(). FWIW, as well as working under 2.2.8, these programs run properly under SunOS 4.1.3. BTW. I realize that this code is not good enough for real-world applications but, as I said, I'm learning so starting simple seems that best way. TIA for your time. -----file - demo.c-------- #include #include #include #include #include #include #include void main() { int fd[2]; int df[2]; pid_t pid; char input[BUFSIZ], result[BUFSIZ]; if (pipe(fd) < 0) { perror("pipe failed"); exit(EXIT_FAILURE); } if (pipe(df) < 0) { perror("pipe failed"); exit(EXIT_FAILURE); } if ((pid = fork()) < 0) { perror("fork failed"); exit(EXIT_FAILURE); } if (pid > 0) /* parent */ { close(fd[0]); close(df[1]); read(df[0], input, BUFSIZ - 1); if (strstr(input, "Diameter")) write(fd[1], "34\n", 3); read(df[0], result, BUFSIZ - 1); fprintf(stderr, "RESULT! - %s\n", result); close(fd[1]); close(df[0]); } else { /* child */ if (fd[0] != STDIN_FILENO) { if (dup2(fd[0], STDIN_FILENO) != STDIN_FILENO) { perror("dup2 fd[0] failed"); exit(EXIT_FAILURE); } } if (df[1] != STDOUT_FILENO) { if (dup2(df[1], STDOUT_FILENO) != STDOUT_FILENO) { perror("dup2 df[1] failed"); exit(EXIT_FAILURE); } } close(fd[1]); close(df[0]); if (execvp("./d2c", NULL) < 0) { perror("execvp failed"); exit(EXIT_FAILURE); } close(fd[0]); /* Don't think these are really needed */ close(df[1]); /* as execvp() only returns on failure */ /* & then exit() will be called above */ } } -----file - d2c.c---------- #include #include #include #define PI 3.141592653 int main(void) { double circ; char input[BUFSIZ]; printf("%s", "Diameter: "); fflush(stdout); fgets(input, BUFSIZ-1, stdin); circ = atof(input) * PI; printf("Circumference-%f\n", circ); fflush(stdout); return(0); } ----------------------------------- -- FreeBSD - The Power To Serve http://www.freebsd.org My Webpage http://www.users.globalnet.co.uk/~markov _______________________________________________________________ Mark Ovens, CNC Apps Engineer, Radan Computational Ltd. Bath UK CAD/CAM solutions for Sheetmetal Working Industry mailto:marko@uk.radan.com http://www.radan.com To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message