linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Linux process...
@ 2009-03-30  6:06 beginner_h4x3r
  2009-03-30  6:15 ` Rahul K Patel
  2009-03-30 11:36 ` Fabian Ischia
  0 siblings, 2 replies; 8+ messages in thread
From: beginner_h4x3r @ 2009-03-30  6:06 UTC (permalink / raw)
  To: linux-c-programming

Hi All..

I am a beginner hacker, i want to learn Linux from scratch. I read
some resources on Linux's process management. Process duplicates it's
page table to it's child process, right? so i wrote demonstrate code
to prove this.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main (void) {
  pid_t child;
  int stack_int;

  child = fork ();
  if (child == 0) {
      sleep (1); /* ;p */
      printf ("child process stack_int value %i, address: %p\n",
stack_int, &stack_int);
      exit (0);
    }
  if (child == -1) {
      perror ("fork");
      return -1;
    }
  stack_int = 32;
  printf ("main process stack_int value %i, address: %p\n", stack_int,
&stack_int);
  waitpid (child, NULL, 0);

  return 0;
}

The output is:
main process stack_int value 32, address: 0xbf9c66ec
child process stack_int value 8495092, address: 0xbf9c66ec

stack_int value is different from parent and it's child.

My question: why the stack_int has a same address between parent and
it's child ?, but confusedly... they have a different value, i was
though it should be different, since process duplicate it's page to
child, please explain me. ;)

Thanks before.

--- curious_hacker

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

* Re: Linux process...
  2009-03-30  6:06 Linux process beginner_h4x3r
@ 2009-03-30  6:15 ` Rahul K Patel
  2009-03-30  6:57   ` beginner_h4x3r
  2009-03-30 11:36 ` Fabian Ischia
  1 sibling, 1 reply; 8+ messages in thread
From: Rahul K Patel @ 2009-03-30  6:15 UTC (permalink / raw)
  To: beginner_h4x3r; +Cc: linux-c-programming

it's offset address and not absolute address.
base addresses of your parent and child process will be different so
final absolute address (base + offset) will be different for both.

beginner_h4x3r wrote:
> Hi All..
>
> I am a beginner hacker, i want to learn Linux from scratch. I read
> some resources on Linux's process management. Process duplicates it's
> page table to it's child process, right? so i wrote demonstrate code
> to prove this.
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <unistd.h>
> #include <sys/types.h>
> #include <sys/wait.h>
>
> int main (void) {
>   pid_t child;
>   int stack_int;
>
>   child = fork ();
>   if (child == 0) {
>       sleep (1); /* ;p */
>       printf ("child process stack_int value %i, address: %p\n",
> stack_int, &stack_int);
>       exit (0);
>     }
>   if (child == -1) {
>       perror ("fork");
>       return -1;
>     }
>   stack_int = 32;
>   printf ("main process stack_int value %i, address: %p\n", stack_int,
> &stack_int);
>   waitpid (child, NULL, 0);
>
>   return 0;
> }
>
> The output is:
> main process stack_int value 32, address: 0xbf9c66ec
> child process stack_int value 8495092, address: 0xbf9c66ec
>
> stack_int value is different from parent and it's child.
>
> My question: why the stack_int has a same address between parent and
> it's child ?, but confusedly... they have a different value, i was
> though it should be different, since process duplicate it's page to
> child, please explain me. ;)
>
> Thanks before.
>
> --- curious_hacker
> --
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>
>
> Email Scanned for Virus & Dangerous Content by : www.CleanMailGateway.com
>
>
>   


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

* Re: Linux process...
  2009-03-30  6:15 ` Rahul K Patel
@ 2009-03-30  6:57   ` beginner_h4x3r
  2009-03-30  7:11     ` Mohana Sundaram
  0 siblings, 1 reply; 8+ messages in thread
From: beginner_h4x3r @ 2009-03-30  6:57 UTC (permalink / raw)
  To: Rahul K Patel; +Cc: linux-c-programming

Hi Rahul,

What is the means of process's base address and offset? It's means
child's base address is offset? how to know child's process stack_int
address in my demonstration code.

Thanks in advance.

--- curious_hacker

On Mon, Mar 30, 2009 at 1:15 PM, Rahul K Patel
<rahulk.patel@einfochips.com> wrote:
> it's offset address and not absolute address.
> base addresses of your parent and child process will be different so
> final absolute address (base + offset) will be different for both.

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

* Re: Linux process...
  2009-03-30  6:57   ` beginner_h4x3r
@ 2009-03-30  7:11     ` Mohana Sundaram
  0 siblings, 0 replies; 8+ messages in thread
From: Mohana Sundaram @ 2009-03-30  7:11 UTC (permalink / raw)
  To: beginner_h4x3r; +Cc: Rahul K Patel, linux-c-programming

Please read about virtual memory.. In a nutshell the printed addresses
are virtual addresses not absolute addresses.

Thanks,
- Mohan.
The expert at anything was once a beginner.




On Mon, Mar 30, 2009 at 12:27, beginner_h4x3r <nightdecoder@gmail.com> wrote:
> Hi Rahul,
>
> What is the means of process's base address and offset? It's means
> child's base address is offset? how to know child's process stack_int
> address in my demonstration code.
>
> Thanks in advance.
>
> --- curious_hacker
>
> On Mon, Mar 30, 2009 at 1:15 PM, Rahul K Patel
> <rahulk.patel@einfochips.com> wrote:
>> it's offset address and not absolute address.
>> base addresses of your parent and child process will be different so
>> final absolute address (base + offset) will be different for both.
> --
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* Re: Linux process...
  2009-03-30  6:06 Linux process beginner_h4x3r
  2009-03-30  6:15 ` Rahul K Patel
@ 2009-03-30 11:36 ` Fabian Ischia
  2009-03-31  1:12   ` beginner_h4x3r
  1 sibling, 1 reply; 8+ messages in thread
From: Fabian Ischia @ 2009-03-30 11:36 UTC (permalink / raw)
  To: beginner_h4x3r; +Cc: linux-c-programming

 From the example code, I think the answer is a bit simpler than it 
looks like. The address space is "duplicated" not "shared". Whatever you 
do in one process "after" the fork will not affect the other process.
The Child process has not initialized the variable, so being a stack 
variable it just contains garbage.

Fabian

beginner_h4x3r wrote:
> Hi All..
>
> I am a beginner hacker, i want to learn Linux from scratch. I read
> some resources on Linux's process management. Process duplicates it's
> page table to it's child process, right? so i wrote demonstrate code
> to prove this.
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <unistd.h>
> #include <sys/types.h>
> #include <sys/wait.h>
>
> int main (void) {
>   pid_t child;
>   int stack_int;
>
>   child = fork ();
>   if (child == 0) {
>       sleep (1); /* ;p */
>       printf ("child process stack_int value %i, address: %p\n",
> stack_int, &stack_int);
>       exit (0);
>     }
>   if (child == -1) {
>       perror ("fork");
>       return -1;
>     }
>   stack_int = 32;
>   printf ("main process stack_int value %i, address: %p\n", stack_int,
> &stack_int);
>   waitpid (child, NULL, 0);
>
>   return 0;
> }
>
> The output is:
> main process stack_int value 32, address: 0xbf9c66ec
> child process stack_int value 8495092, address: 0xbf9c66ec
>
> stack_int value is different from parent and it's child.
>
> My question: why the stack_int has a same address between parent and
> it's child ?, but confusedly... they have a different value, i was
> though it should be different, since process duplicate it's page to
> child, please explain me. ;)
>
> Thanks before.
>
> --- curious_hacker
> --
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>   

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

* Re: Linux process...
  2009-03-30 11:36 ` Fabian Ischia
@ 2009-03-31  1:12   ` beginner_h4x3r
  2009-03-31  5:57     ` Glynn Clements
  2009-03-31 23:45     ` stephan
  0 siblings, 2 replies; 8+ messages in thread
From: beginner_h4x3r @ 2009-03-31  1:12 UTC (permalink / raw)
  To: Fabian Ischia; +Cc: linux-c-programming

Okay, so the child process was not actually access it's parent
variable, the child given a copy (i have learned about Copy On Write
mechanism too).  But it is like a C language issue: we can access any
variable which declared in that function in this case main() function.
So in my code, when i try to access stack_int variable in child
process, it's not wrong, compiler even recognize this as 'valid'
approach... How about my conclusion?

On Mon, Mar 30, 2009 at 6:36 PM, Fabian Ischia <fischia@somanetworks.com> wrote:
> From the example code, I think the answer is a bit simpler than it looks
> like. The address space is "duplicated" not "shared". Whatever you do in one
> process "after" the fork will not affect the other process.
> The Child process has not initialized the variable, so being a stack
> variable it just contains garbage.
>
> Fabian
>
> beginner_h4x3r wrote:
>>
>> Hi All..
>>
>> I am a beginner hacker, i want to learn Linux from scratch. I read
>> some resources on Linux's process management. Process duplicates it's
>> page table to it's child process, right? so i wrote demonstrate code
>> to prove this.
>>
>> #include <stdio.h>
>> #include <stdlib.h>
>> #include <unistd.h>
>> #include <sys/types.h>
>> #include <sys/wait.h>
>>
>> int main (void) {
>>  pid_t child;
>>  int stack_int;
>>
>>  child = fork ();
>>  if (child == 0) {
>>      sleep (1); /* ;p */
>>      printf ("child process stack_int value %i, address: %p\n",
>> stack_int, &stack_int);
>>      exit (0);
>>    }
>>  if (child == -1) {
>>      perror ("fork");
>>      return -1;
>>    }
>>  stack_int = 32;
>>  printf ("main process stack_int value %i, address: %p\n", stack_int,
>> &stack_int);
>>  waitpid (child, NULL, 0);
>>
>>  return 0;
>> }
>>
>> The output is:
>> main process stack_int value 32, address: 0xbf9c66ec
>> child process stack_int value 8495092, address: 0xbf9c66ec
>>
>> stack_int value is different from parent and it's child.
>>
>> My question: why the stack_int has a same address between parent and
>> it's child ?, but confusedly... they have a different value, i was
>> though it should be different, since process duplicate it's page to
>> child, please explain me. ;)
>>
>> Thanks before.
>>
>> --- curious_hacker
>> --
>> To unsubscribe from this list: send the line "unsubscribe
>> linux-c-programming" in
>> the body of a message to majordomo@vger.kernel.org
>> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>>
>

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

* Re: Linux process...
  2009-03-31  1:12   ` beginner_h4x3r
@ 2009-03-31  5:57     ` Glynn Clements
  2009-03-31 23:45     ` stephan
  1 sibling, 0 replies; 8+ messages in thread
From: Glynn Clements @ 2009-03-31  5:57 UTC (permalink / raw)
  To: beginner_h4x3r; +Cc: Fabian Ischia, linux-c-programming


beginner_h4x3r wrote:

> Okay, so the child process was not actually access it's parent
> variable, the child given a copy (i have learned about Copy On Write
> mechanism too).  But it is like a C language issue: we can access any
> variable which declared in that function in this case main() function.
> So in my code, when i try to access stack_int variable in child
> process, it's not wrong, compiler even recognize this as 'valid'
> approach... How about my conclusion?

What about it? If you compile with warnings enabled, gcc will tell you
that the first printf() is using the value of an uninitialised
variable.

-- 
Glynn Clements <glynn@gclements.plus.com>

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

* Re: Linux process...
  2009-03-31  1:12   ` beginner_h4x3r
  2009-03-31  5:57     ` Glynn Clements
@ 2009-03-31 23:45     ` stephan
  1 sibling, 0 replies; 8+ messages in thread
From: stephan @ 2009-03-31 23:45 UTC (permalink / raw)
  To: beginner_h4x3r; +Cc: linux-c-programming

 Initialize the stack variable, when you do its retained thoughout 
execution of both processes. I think when a variable is uninitialized  
its given null value, and doesnt really get copied in a sense, I have 
also read that threads can have their own local thread storage areas.

try a strace on the executable to see what flags clone() is being used 
by glib.


beginner_h4x3r wrote:
> Okay, so the child process was not actually access it's parent
> variable, the child given a copy (i have learned about Copy On Write
> mechanism too).  But it is like a C language issue: we can access any
> variable which declared in that function in this case main() function.
> So in my code, when i try to access stack_int variable in child
> process, it's not wrong, compiler even recognize this as 'valid'
> approach... How about my conclusion?
>
> On Mon, Mar 30, 2009 at 6:36 PM, Fabian Ischia <fischia@somanetworks.com> wrote:
>   
>> From the example code, I think the answer is a bit simpler than it looks
>> like. The address space is "duplicated" not "shared". Whatever you do in one
>> process "after" the fork will not affect the other process.
>> The Child process has not initialized the variable, so being a stack
>> variable it just contains garbage.
>>
>> Fabian
>>
>> beginner_h4x3r wrote:
>>     
>>> Hi All..
>>>
>>> I am a beginner hacker, i want to learn Linux from scratch. I read
>>> some resources on Linux's process management. Process duplicates it's
>>> page table to it's child process, right? so i wrote demonstrate code
>>> to prove this.
>>>
>>> #include <stdio.h>
>>> #include <stdlib.h>
>>> #include <unistd.h>
>>> #include <sys/types.h>
>>> #include <sys/wait.h>
>>>
>>> int main (void) {
>>>  pid_t child;
>>>  int stack_int;
>>>
>>>  child = fork ();
>>>  if (child == 0) {
>>>      sleep (1); /* ;p */
>>>      printf ("child process stack_int value %i, address: %p\n",
>>> stack_int, &stack_int);
>>>      exit (0);
>>>    }
>>>  if (child == -1) {
>>>      perror ("fork");
>>>      return -1;
>>>    }
>>>  stack_int = 32;
>>>  printf ("main process stack_int value %i, address: %p\n", stack_int,
>>> &stack_int);
>>>  waitpid (child, NULL, 0);
>>>
>>>  return 0;
>>> }
>>>
>>> The output is:
>>> main process stack_int value 32, address: 0xbf9c66ec
>>> child process stack_int value 8495092, address: 0xbf9c66ec
>>>
>>> stack_int value is different from parent and it's child.
>>>
>>> My question: why the stack_int has a same address between parent and
>>> it's child ?, but confusedly... they have a different value, i was
>>> though it should be different, since process duplicate it's page to
>>> child, please explain me. ;)
>>>
>>> Thanks before.
>>>
>>> --- curious_hacker
>>> --
>>> To unsubscribe from this list: send the line "unsubscribe
>>> linux-c-programming" in
>>> the body of a message to majordomo@vger.kernel.org
>>> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>>>
>>>       
> --
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>
>   


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

end of thread, other threads:[~2009-03-31 23:45 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-03-30  6:06 Linux process beginner_h4x3r
2009-03-30  6:15 ` Rahul K Patel
2009-03-30  6:57   ` beginner_h4x3r
2009-03-30  7:11     ` Mohana Sundaram
2009-03-30 11:36 ` Fabian Ischia
2009-03-31  1:12   ` beginner_h4x3r
2009-03-31  5:57     ` Glynn Clements
2009-03-31 23:45     ` stephan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).