Date: Mon, 8 Feb 1999 12:39:01 +1030 From: Greg Lehey <grog@lemis.com> To: Masahiro Ariga <mariga@cd.mbn.or.jp> Cc: freebsd-questions@FreeBSD.ORG Subject: fork() programming example (was: Is my BSD wrong installed?) Message-ID: <19990208123900.U86778@freebie.lemis.com> In-Reply-To: <000601be52c8$2b4f32a0$064ca8c0@gateway>; from Masahiro Ariga on Mon, Feb 08, 1999 at 03:31:49AM %2B0900 References: <000601be52c8$2b4f32a0$064ca8c0@gateway>
next in thread | previous in thread | raw e-mail | index | archive | help
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 <stdio.h> > > 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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?19990208123900.U86778>