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 !
Signature · 1 line
Sat.
Quoted 10 lines · Sat.
if(!(pid=fork())){
......
printk("in child process");
......
}else{
.....
printk("in father process");
.....
}Quoted 8 lines
this is a classical example, when the
fork()system call runs, it will build a new process and active it . while theschedule()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.
Quoted 3 lines
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).
Quoted 1 line
very confused :(
Bernd
Signature · 3 lines
Firmix Software GmbH http://www.firmix.at/
mobil: +43 664 4416156 fax: +43 1 7890849-55
Embedded Linux Development and ServicesA message that is not in the archive (sent off-list or not yet received): 003a01c5b2d6$610d6360$6464a8c0@pc0001
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]>:
Quoted 54 lines
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 thefork()with its following code ? a simple example below:Quoted 36 lines
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 theschedule()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/
Signature · 1 line
Sat.
Quoted 12 lines · Sat.
here is a snip in 0.11 version linux ,
inlinux/init/main.c179 if (!(pid=fork())) {
180 close(0);
181 if (open( "/etc/rc",O_RDONLY,0))
182_exit(1);
183execve( "/bin/sh",argv_rc,envp_rc);
184_exit(2);
185 }
Quoted 9 lines
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);
}Quoted 43 lines
2005/9/6, Dirk Gerdes <[email protected]>:
Quoted 41 lines
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 thefork()with its following code ? a simple example below:Quoted 25 lines
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 theschedule()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);Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
--
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.
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]>:
Quoted 54 lines
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 thefork()with its following code ? a simple example below:Quoted 36 lines
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 theschedule()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/
Signature · 1 line
Sat.
On Tue, 06 Sep 2005 17:15:51 +0800, "Sat." said:
Not a kernel problem, please consult an intro-to-C list next time....
Quoted 11 lines
if(!(pid=fork())){
......
printk("in child process");
......
}else{
.....
printk("in father process");
.....
}Quoted 2 lines
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.
Quoted 15 lines · 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....
Quoted 10 lines
if(!(pid=fork())){ ...... printk("in child process"); ...... }else{ ..... printk("in father process"); ..... }
Quoted 5 lines
Quoted 2 lines
values., and do nothing . so the bridge between the new process and its following code,
printk("in child process"), seems disappearI'm assuming you actually meant
printf()(which is the userspace stdio call) rather thanprintk()(which is the inside-the-kernel variant).
I actually assumed the same.
Quoted 2 lines
'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);"
Quoted 2 lines
line-buffered or unbuffered, or write to stderr (unbuffered by default) rather than stdout.
Bernd
Signature · 3 lines
Firmix Software GmbH http://www.firmix.at/
mobil: +43 664 4416156 fax: +43 1 7890849-55
Embedded Linux Development and Services