2005-09-06 09:15:53

by Sat.

[permalink] [raw]
Subject: what will connect the fork() with its following code ? a simple example below:

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
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 ?

very confused :(

any help will appreciate !



--
Sat.


2005-09-06 10:38:05

by Bernd Petrovitsch

[permalink] [raw]
Subject: Re: what will connect the fork() with its following code ? a simple example below:

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

2005-09-06 11:59:52

by Sat.

[permalink] [raw]
Subject: Re: what will connect the fork() with its following code ? a simple example below:

here is a snip in 0.11 version linux ,
in linux/init/main.c


179 if (!(pid=fork())) {
180 close(0);
181 if (open( "/etc/rc",O_RDONLY,0))
182 _exit(1);
183 execve( "/bin/sh",argv_rc,envp_rc);
184 _exit(2);
185 }

natually, the code from 180 to 184 is runned by the new process, what
I can understand is why the new process know that the next code will
run is close(0) and why it know It will end at line 184 ?

so ,I feel that there should be some connection between them . but
what the relationship in depth is ?

thanks your help :)


2005/9/6, Dirk Gerdes <[email protected]>:
> There is no connection between a child an its parent.
> The child only gets a copy of the code.
> If there were a pointer to a child or to the parent, you wouldn't need any
> signals.
> The processes could communicate directly.
>
> regards
>
> ----- Original Message -----
> From: "Sat." <[email protected]>
> To: <[email protected]>
> Sent: Tuesday, September 06, 2005 11:15 AM
> Subject: what will connect the fork() with its following code ? a simple
> example below:
>
>
> > 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
> > 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 ?
> >
> > very confused :(
> >
> > any help will appreciate !
> >
> >
> >
> > --
> > Sat.
> > -
> > To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> > the body of a message to [email protected]
> > More majordomo info at http://vger.kernel.org/majordomo-info.html
> > Please read the FAQ at http://www.tux.org/lkml/
> >
>
>


--
Sat.

2005-09-06 12:41:08

by linux-os (Dick Johnson)

[permalink] [raw]
Subject: Re: what will connect the fork() with its following code ? a simple example below:


On Tue, 6 Sep 2005, Sat. wrote:

> here is a snip in 0.11 version linux ,
> in linux/init/main.c
>
>
> 179 if (!(pid=fork())) {
> 180 close(0);
> 181 if (open( "/etc/rc",O_RDONLY,0))
> 182 _exit(1);
> 183 execve( "/bin/sh",argv_rc,envp_rc);
> 184 _exit(2);
> 185 }
>
> natually, the code from 180 to 184 is runned by the new process, what
> I can understand is why the new process know that the next code will
> run is close(0) and why it know It will end at line 184 ?
>
> so ,I feel that there should be some connection between them . but
> what the relationship in depth is ?
>
> thanks your help :)
>

After the fork() system call, fork() returns the child's pid
(or an error) to the parent, and a ZERO (0) to the child. That
way the code 'knows' if the child or the parent is executing.


switch ((pid = fork()))
{
case 0: // Child runs here
printf("Child\n");
sleep(1);
exit(0);
case -1: // This is for errors
fprintf(stderr, "fork() failed (%s)\n", strerror(errno));
exit(1);
default: // The parent executes here
printf("Parent waits for child with pid %d\n", pid);
wait(&status);
printf("Child executed with %d status\n", status >> 8);
}

>
> 2005/9/6, Dirk Gerdes <[email protected]>:
>> There is no connection between a child an its parent.
>> The child only gets a copy of the code.
>> If there were a pointer to a child or to the parent, you wouldn't need any
>> signals.
>> The processes could communicate directly.
>>
>> regards
>>
>> ----- Original Message -----
>> From: "Sat." <[email protected]>
>> To: <[email protected]>
>> Sent: Tuesday, September 06, 2005 11:15 AM
>> Subject: what will connect the fork() with its following code ? a simple
>> example below:
>>
>>
>>> 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
>>> 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 ?
>>>
>>> very confused :(
>>>
>>> any help will appreciate !

sys_fork() should never be executed from inside the kernel because
it is going to copy a process' code and data. The kernel isn't a
process. To make a process from inside the kernel, you need to use:

//
// Code to execute as a kernel thread.
//

int thread_code(void *ptr)
{
daemonize("%s", "Kernel Thread!");
allow_signal(SIGTERM);
for(;;) // Run forever
{
do_something();

set_current_state(TASK_INTERRUPTIBLE);
if(signal_pending(current))
complete_and_exit(&exit_flag);
}
}

//
// Code to start the kernel thread.
//

pid = kernel_thread(thread_code, NULL, CLONE_FS|CLONE_FILES);


//
// Code to stop the kernel thread.
//

kill_proc(pid, SIGTERM);
wait_for_completion(&exit_flag);


>>>
>>>
>>>
>>> --
>>> Sat.



Cheers,
Dick Johnson
Penguin : Linux version 2.6.13 on an i686 machine (5589.54 BogoMips).
Warning : 98.36% of all statistics are fiction.
.
I apologize for the following. I tried to kill it with the above dot :

****************************************************************
The information transmitted in this message is confidential and may be privileged. Any review, retransmission, dissemination, or other use of this information by persons or entities other than the intended recipient is prohibited. If you are not the intended recipient, please notify Analogic Corporation immediately - by replying to this message or by sending an email to [email protected] - and destroy all copies of this information, including any attachments, without reading or disclosing them.

Thank you.

2005-09-06 12:56:07

by Dirk Henning Gerdes

[permalink] [raw]
Subject: Re: what will connect the fork() with its following code ? a simple example below:

fork returns 0 to the child and the pid of the child to the parent.

both child and parent get the same code, so the child gets true in the
if-statement and the parent gets false.

it would be the same as

pid = fork();
if (pid == 0){
// child
}
else{
// parent
}


----- Original Message -----
From: "Sat." <[email protected]>
To: "Dirk Gerdes" <[email protected]>
Cc: <[email protected]>
Sent: Tuesday, September 06, 2005 1:59 PM
Subject: Re: what will connect the fork() with its following code ? a simple
example below:


here is a snip in 0.11 version linux ,
in linux/init/main.c


179 if (!(pid=fork())) {
180 close(0);
181 if (open( "/etc/rc",O_RDONLY,0))
182 _exit(1);
183 execve( "/bin/sh",argv_rc,envp_rc);
184 _exit(2);
185 }

natually, the code from 180 to 184 is runned by the new process, what
I can understand is why the new process know that the next code will
run is close(0) and why it know It will end at line 184 ?

so ,I feel that there should be some connection between them . but
what the relationship in depth is ?

thanks your help :)


2005/9/6, Dirk Gerdes <[email protected]>:
> There is no connection between a child an its parent.
> The child only gets a copy of the code.
> If there were a pointer to a child or to the parent, you wouldn't need any
> signals.
> The processes could communicate directly.
>
> regards
>
> ----- Original Message -----
> From: "Sat." <[email protected]>
> To: <[email protected]>
> Sent: Tuesday, September 06, 2005 11:15 AM
> Subject: what will connect the fork() with its following code ? a simple
> example below:
>
>
> > 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
> > 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 ?
> >
> > very confused :(
> >
> > any help will appreciate !
> >
> >
> >
> > --
> > Sat.
> > -
> > To unsubscribe from this list: send the line "unsubscribe linux-kernel"
> > in
> > the body of a message to [email protected]
> > More majordomo info at http://vger.kernel.org/majordomo-info.html
> > Please read the FAQ at http://www.tux.org/lkml/
> >
>
>


--
Sat.

2005-09-06 16:58:20

by Valdis Klētnieks

[permalink] [raw]
Subject: Re: what will connect the fork() with its following code ? a simple example below:

On Tue, 06 Sep 2005 17:15:51 +0800, "Sat." said:

Not a kernel problem, please consult an intro-to-C list next time....

> if(!(pid=fork())){
> ......
> printk("in child process");
> ......
> }else{
> .....
> printk("in father process");
> .....
> }
>

> values., and do nothing . so the bridge between the new process and
> its following code, printk("in child process"), seems disappear

I'm assuming you actually meant printf() (which is the userspace stdio call)
rather than printk() (which is the inside-the-kernel variant).

'man setbuf' - most likely the output of the child process is buffered and
never actually output before it exits. You want to set stdout to be
line-buffered or unbuffered, or write to stderr (unbuffered by default) rather
than stdout.


Attachments:
(No filename) (226.00 B)

2005-09-07 07:08:56

by Bernd Petrovitsch

[permalink] [raw]
Subject: Re: what will connect the fork() with its following code ? a simple example below:

On Tue, 2005-09-06 at 12:58 -0400, [email protected] wrote:
> On Tue, 06 Sep 2005 17:15:51 +0800, "Sat." said:
>
> Not a kernel problem, please consult an intro-to-C list next time....
>
> > if(!(pid=fork())){
> > ......
> > printk("in child process");
> > ......
> > }else{
> > .....
> > printk("in father process");
> > .....
> > }
> >
>
> > values., and do nothing . so the bridge between the new process and
> > its following code, printk("in child process"), seems disappear
>
> I'm assuming you actually meant printf() (which is the userspace stdio call)
> rather than printk() (which is the inside-the-kernel variant).

I actually assumed the same.

> 'man setbuf' - most likely the output of the child process is buffered and
> never actually output before it exits. You want to set stdout to be

It is buffered since the above printf() lacks a terminating "\n". So
either put a "\n" at the end or call "fflush(stdout);"

> line-buffered or unbuffered, or write to stderr (unbuffered by default) rather
> than stdout.

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