From owner-freebsd-questions Thu Apr 25 14:39: 3 2002 Delivered-To: freebsd-questions@freebsd.org Received: from tninet.se (lennier.tninet.se [195.100.94.105]) by hub.freebsd.org (Postfix) with ESMTP id 88F0737B416 for ; Thu, 25 Apr 2002 14:38:56 -0700 (PDT) Received: from cs.umu.se (h72n1c1o1023.bredband.skanova.com [213.64.164.72]) by lennier.tninet.se (BMR ErlangTM/OTP 3.0) with ESMTP id 530407.770679.1019.1s26872117lennier ; Thu, 25 Apr 2002 23:37:59 +0200 Message-ID: <3CC87737.9E146891@cs.umu.se> Date: Thu, 25 Apr 2002 23:37:59 +0200 From: Paul Everlund X-Mailer: Mozilla 4.77 [en] (Windows NT 5.0; U) X-Accept-Language: sv,en MIME-Version: 1.0 To: Bsd Neophyte Cc: freebsd-questions@FreeBSD.ORG Subject: Re: what are parent and child processes all about? References: <20020425201553.43809.qmail@web20106.mail.yahoo.com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG Bsd Neophyte wrote: > > I don't follow what these are... > > this is what my text says... > > "When a process is started, a duplicate of that process is created. This > new process is called the child and the process that created it is called > the parent. The child process then replaces the copy for the code the > parent process created with the code the child process is supposted to > execute." > > The last sentance confuses me the most. Actually the entire thing > confsues me. Why is a child process started anyways? Why does the parent > process spawn a child process in the first place? You can create a child process by fork (see man 2 fork). The process that creates it is called the parent. If you create a server, it works as this (maybe somewhat simplified): 1. Execute the command that starts the server, as httpd. 2. Instantly after it is started, it maybe does some things, then it forks a child. This child process contains a copy of file descriptors, environ- ment variables and so on, and even the executable code. Its a copy of the parent in other words, but it has its own process id. 3. The parent then kills itself, but the child keeps on running in the back- ground handling requests or other things. You get almost(?) the same thing by starting a program with &. > And what's all this code copy stuff all about? The code is copied, as stated before, but the child can execute its "own" code by a simple if (something like this if I remember it correct): . . pid = fork(); if(pid == 0) // This means it's the child process. { do some stuff... maybe in a loop of some kind... } exit(0); // If here the parent/child exits. I guess it's copied as the child then can use the parent process things, and do not have to open files and so on once more. > The next paragraph states... > > "While the command is executing, the shell waits until the child process > has completed. After is completes, the parent process terminates the > child process, and a prompt is displated, ready for a new command" The parent can wait for the child to finish, but it can also, as I wrote before, exit and let the child continue to run in the background. > Why does this happen? I guess it goes back to my confusion about why the > child process is created in the first place. A nice way of doing simultaneous stuff, or making a server. Best regards, Paul To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message