Re: what will connect the fork() with its following code ? asimple example below:

From: Bernd Petrovitsch
Date: Tue Sep 06 2005 - 05:38:26 EST


On Tue, 2005-09-06 at 17:15 +0800, Sat. wrote:
> if(!(pid=fork())){
> ......
> printk("in child process");
> ......
> }else{
> .....
> printk("in father process");
> .....
> }
>
> this is a classical example, when the fork() system call runs, it will
> build a new process and active it . while the schedule() select the
> new process it will run. this is rather normal.
>
> but there is always a confusion in my minds.
> because , sys_fork() only copies father process and configure some new
> values., and do nothing . so the bridge between the new process and
> its following code, printk("in child process"), seems disappear . so I

No, it is the same point as in the parent process - just the fork()
call. In fact the fork() returns twice - once within the parent process
(returning as result the pid of the child) and once within the child
(returning 0) [ and we ignore the error case for simplicity ].
Basically you are not forced to do a if() or switch() on the return
value(s) of fork()
---- snip ----
printf("1. fork() in process %d returned %d\n", getpid(), fork());
printf("2. fork() in process %d returned %d\n", getpid(), fork());
printf("3. fork() in process %d returned %d\n", getpid(), fork());
printf("4. fork() in process %d returned %d\n", getpid(), fork());
printf("5. fork() in process %d returned %d\n", getpid(), fork());
---- snip ----
Put this in a main() function and try it out.

> always believe that the new process should have a pointer which point
> the code "printk("in child process");". except this , there are not
> any connection between them ?

The kernel doesn't know (or care) about the C code (e.g. if(), ...) in
your application.
The "pointer" (if you want to call it) is the normal instruction pointer
(or what else it is called on your CPU).

> very confused :(

Bernd
--
Firmix Software GmbH http://www.firmix.at/
mobil: +43 664 4416156 fax: +43 1 7890849-55
Embedded Linux Development and Services

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/