gamblor01
09-09-2004, 02:09 AM
So I'm a little confused about what exactly is going on when processes are created. So I have a parent process and it's going to call fork and spawn a child. Then the child's address space is going to be initialized and the code, data, stack, etc. segments are copied to the child, so that immediately after fork is finished...they'll contain the exact same thing. Now, then the return value comes in, and the parent process gets the PID of the child, the child gets zero. Now the child runs it's stuff, and so the code would look something like this:
// currently in the parent process
main()
// code here
int pid = fork(); // create some child process
if (pid == 0) // we're a child
{
exec (argc, argv0, argv1, etc.)
}
else
{
// parent process continues here, maybe waits
}
I guess what I really don't understand is this. Fork copies the code, data, and stack segments from the parent process. The code segment is read only. If the code segment is copied and is read only, and fork is what spawns new processes, how are new programs initialized?
Let's say we have init running xfs, pdflush, login, etc. Now login has BASH as a child. Now BASH has smbd as a child. Init can't possibly preempt all possible programs that could ever and will ever be written and neither can BASH or any process. This must be where exec comes into play, and calls the newly written program??? ...so a process is not the same as a program? I mean, I know the notion of a process is really just an abstraction, but I don't get what's going on. The code segment in the address space of process smbd for example, is not the same as the actual code for smbd? Let's say the BASH shell (which is a process yeah?) spawns the smbd child process. So then the smbd process contains the code segment for BASH. So how does the smbd process know to execute the code for smbd? I think that's what exec does...correct me if I'm wrong.
Can anyone please shed some light on this for me? :D
// currently in the parent process
main()
// code here
int pid = fork(); // create some child process
if (pid == 0) // we're a child
{
exec (argc, argv0, argv1, etc.)
}
else
{
// parent process continues here, maybe waits
}
I guess what I really don't understand is this. Fork copies the code, data, and stack segments from the parent process. The code segment is read only. If the code segment is copied and is read only, and fork is what spawns new processes, how are new programs initialized?
Let's say we have init running xfs, pdflush, login, etc. Now login has BASH as a child. Now BASH has smbd as a child. Init can't possibly preempt all possible programs that could ever and will ever be written and neither can BASH or any process. This must be where exec comes into play, and calls the newly written program??? ...so a process is not the same as a program? I mean, I know the notion of a process is really just an abstraction, but I don't get what's going on. The code segment in the address space of process smbd for example, is not the same as the actual code for smbd? Let's say the BASH shell (which is a process yeah?) spawns the smbd child process. So then the smbd process contains the code segment for BASH. So how does the smbd process know to execute the code for smbd? I think that's what exec does...correct me if I'm wrong.
Can anyone please shed some light on this for me? :D