From owner-freebsd-questions Sun Feb 7 18:09:15 1999 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id SAA25478 for freebsd-questions-outgoing; Sun, 7 Feb 1999 18:09:15 -0800 (PST) (envelope-from owner-freebsd-questions@FreeBSD.ORG) Received: from allegro.lemis.com (allegro.lemis.com [192.109.197.134]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id SAA25449 for ; Sun, 7 Feb 1999 18:09:06 -0800 (PST) (envelope-from grog@freebie.lemis.com) Received: from freebie.lemis.com (freebie.lemis.com [192.109.197.137]) by allegro.lemis.com (8.9.1/8.9.0) with ESMTP id MAA25628; Mon, 8 Feb 1999 12:39:03 +1030 (CST) Received: (from grog@localhost) by freebie.lemis.com (8.9.2/8.9.0) id MAA47168; Mon, 8 Feb 1999 12:39:01 +1030 (CST) Date: Mon, 8 Feb 1999 12:39:01 +1030 From: Greg Lehey To: Masahiro Ariga Cc: freebsd-questions@FreeBSD.ORG Subject: fork() programming example (was: Is my BSD wrong installed?) Message-ID: <19990208123900.U86778@freebie.lemis.com> References: <000601be52c8$2b4f32a0$064ca8c0@gateway> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 0.95.1i In-Reply-To: <000601be52c8$2b4f32a0$064ca8c0@gateway>; from Masahiro Ariga on Mon, Feb 08, 1999 at 03:31:49AM +0900 WWW-Home-Page: http://www.lemis.com/~grog Organization: LEMIS, PO Box 460, Echunga SA 5153, Australia Phone: +61-8-8388-8286 Fax: +61-8-8388-8725 Mobile: +61-41-739-7062 Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by hub.freebsd.org id SAA25465 Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG On Monday, 8 February 1999 at 3:31:49 +0900, Masahiro Ariga wrote: > My name is Masahiro Ariga. > I had a spare PC,so I installed Free BSD on it and began studying UNIX > programing.Although I made some C programs on Windows environment,I am > completely at lost in UNIX programing so I bought UNIX program book and made > sample run on my BSD machine. > But its output never become the same as the book's answer.I cannot tell it > is because of my BSD machine being wrongly installed,or it is because > "That's UNIX." > Please teach me. > Sample is as follows, > [Sample program] > #include > > main() > { > if(fork()==0) > printf("I am son!\n"); > else > printf("I am father!\n"); > } > > And,Book says it's output should be, >> sample > I am son! > I am father! >> ■ ----■ is cursor mark. > > But my machine's output is as follows, > > % sample > I am father! > % I am son! > ■ > > Here,two questions arise, > (1) why my output is father's line and son's line exchanged ? In this example, you have three processes involved: 1. Your shell, which spawns the first incidence of sample and waits for it to finish. 2. The first incidence of sample, which prints ``I am father!'' and finishes. 3. The second incidence of sample, which prints ``I am son!'' and finishes. Note that the parent process (2) doesn't wait for the child process to finish, so it's possible to have the child process still running when the shell starts its prompt. This is what you're seeing here, and, to use your terminology, ``That's UNIX'' :-) The example in the book is flawed, and it's a coincidence whether the prompt comes before the child output or not, If you want to fix it, make the parent wait for the child: main() { int pid = fork (); if(pid == 0) printf("I am son!\n"); else { printf("I am father!\n"); wait (pid); } } For the pedantic: pid is of type pid_t, which may or may not be the same as int. You need to include sys/types.h for the definition of pid_t, which is rather beyond the scope of this example. > (2)On second output,why % mark appears first,and why not returns to normal > prompt. I'm not sure what you mean by this. It appears that your prompt is %, and that's what you get in either case. Greg -- When replying to this message, please copy the original recipients. For more information, see http://www.lemis.com/questions.html See complete headers for address, home page and phone numbers finger grog@lemis.com for PGP public key To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message