Date: Thu, 25 Apr 2002 23:37:59 +0200 From: Paul Everlund <tdv94ped@cs.umu.se> To: Bsd Neophyte <bsdneophyte@yahoo.com> Cc: freebsd-questions@FreeBSD.ORG Subject: Re: what are parent and child processes all about? Message-ID: <3CC87737.9E146891@cs.umu.se> References: <20020425201553.43809.qmail@web20106.mail.yahoo.com>
next in thread | previous in thread | raw e-mail | index | archive | help
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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?3CC87737.9E146891>