All of lore.kernel.org
 help / color / mirror / Atom feed
* what will connect the fork() with its following code ? a simple example below:
@ 2005-09-06  9:15 Sat.
  2005-09-06 10:37 ` Bernd Petrovitsch
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Sat. @ 2005-09-06  9:15 UTC (permalink / raw)
  To: linux-kernel

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.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: what will connect the fork() with its following code ? a simple example below:
  2005-09-06  9:15 what will connect the fork() with its following code ? a simple example below: Sat.
@ 2005-09-06 10:37 ` Bernd Petrovitsch
       [not found] ` <003a01c5b2d6$610d6360$6464a8c0@pc0001>
  2005-09-06 16:58 ` Valdis.Kletnieks
  2 siblings, 0 replies; 7+ messages in thread
From: Bernd Petrovitsch @ 2005-09-06 10:37 UTC (permalink / raw)
  To: walking.to.remember; +Cc: linux-kernel

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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: what will connect the fork() with its following code ? a simple example below:
       [not found] ` <003a01c5b2d6$610d6360$6464a8c0@pc0001>
@ 2005-09-06 11:59   ` Sat.
  2005-09-06 12:41     ` linux-os (Dick Johnson)
  2005-09-06 13:01     ` Dirk Gerdes
  0 siblings, 2 replies; 7+ messages in thread
From: Sat. @ 2005-09-06 11:59 UTC (permalink / raw)
  To: Dirk Gerdes; +Cc: linux-kernel

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 <mail@dirk-gerdes.de>:
> 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." <walking.to.remember@gmail.com>
> To: <linux-kernel@vger.kernel.org>
> 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 majordomo@vger.kernel.org
> > More majordomo info at  http://vger.kernel.org/majordomo-info.html
> > Please read the FAQ at  http://www.tux.org/lkml/
> >
> 
> 


-- 
Sat.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: what will connect the fork() with its following code ? a simple example below:
  2005-09-06 11:59   ` Sat.
@ 2005-09-06 12:41     ` linux-os (Dick Johnson)
  2005-09-06 13:01     ` Dirk Gerdes
  1 sibling, 0 replies; 7+ messages in thread
From: linux-os (Dick Johnson) @ 2005-09-06 12:41 UTC (permalink / raw)
  To: Sat.; +Cc: Dirk Gerdes, Linux kernel


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 <mail@dirk-gerdes.de>:
>> 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." <walking.to.remember@gmail.com>
>> To: <linux-kernel@vger.kernel.org>
>> 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 DeliveryErrors@analogic.com - and destroy all copies of this information, including any attachments, without reading or disclosing them.

Thank you.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: what will connect the fork() with its following code ? a simple example below:
  2005-09-06 11:59   ` Sat.
  2005-09-06 12:41     ` linux-os (Dick Johnson)
@ 2005-09-06 13:01     ` Dirk Gerdes
  1 sibling, 0 replies; 7+ messages in thread
From: Dirk Gerdes @ 2005-09-06 13:01 UTC (permalink / raw)
  To: walking.to.remember; +Cc: linux-kernel

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." <walking.to.remember@gmail.com>
To: "Dirk Gerdes" <mail@dirk-gerdes.de>
Cc: <linux-kernel@vger.kernel.org>
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 <mail@dirk-gerdes.de>:
> 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." <walking.to.remember@gmail.com>
> To: <linux-kernel@vger.kernel.org>
> 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 majordomo@vger.kernel.org
> > More majordomo info at  http://vger.kernel.org/majordomo-info.html
> > Please read the FAQ at  http://www.tux.org/lkml/
> >
>
>


-- 
Sat.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: what will connect the fork() with its following code ? a simple example below:
  2005-09-06  9:15 what will connect the fork() with its following code ? a simple example below: Sat.
  2005-09-06 10:37 ` Bernd Petrovitsch
       [not found] ` <003a01c5b2d6$610d6360$6464a8c0@pc0001>
@ 2005-09-06 16:58 ` Valdis.Kletnieks
  2005-09-07  7:08   ` Bernd Petrovitsch
  2 siblings, 1 reply; 7+ messages in thread
From: Valdis.Kletnieks @ 2005-09-06 16:58 UTC (permalink / raw)
  To: walking.to.remember; +Cc: linux-kernel

[-- Attachment #1: Type: text/plain, Size: 803 bytes --]

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. 


[-- Attachment #2: Type: application/pgp-signature, Size: 226 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: what will connect the fork() with its following code ? a simple example below:
  2005-09-06 16:58 ` Valdis.Kletnieks
@ 2005-09-07  7:08   ` Bernd Petrovitsch
  0 siblings, 0 replies; 7+ messages in thread
From: Bernd Petrovitsch @ 2005-09-07  7:08 UTC (permalink / raw)
  To: Valdis.Kletnieks; +Cc: walking.to.remember, linux-kernel

On Tue, 2005-09-06 at 12:58 -0400, Valdis.Kletnieks@vt.edu 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


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2005-09-07  7:08 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-09-06  9:15 what will connect the fork() with its following code ? a simple example below: Sat.
2005-09-06 10:37 ` Bernd Petrovitsch
     [not found] ` <003a01c5b2d6$610d6360$6464a8c0@pc0001>
2005-09-06 11:59   ` Sat.
2005-09-06 12:41     ` linux-os (Dick Johnson)
2005-09-06 13:01     ` Dirk Gerdes
2005-09-06 16:58 ` Valdis.Kletnieks
2005-09-07  7:08   ` Bernd Petrovitsch

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.