pointer address is physical or virtual. - Linux

This is a discussion on pointer address is physical or virtual. - Linux ; Hi All, When we create a pointer o say an integer variable or a function. And then print it what it should print? Address in the physical memory where it is stored or virtual address? Thanks and regards, Prasad....

+ Reply to Thread
Results 1 to 14 of 14

Thread: pointer address is physical or virtual.

  1. pointer address is physical or virtual.

    Hi All,

    When we create a pointer o say an integer variable or a function. And
    then print it what it should print?

    Address in the physical memory where it is stored or virtual address?

    Thanks and regards,
    Prasad.


  2. Re: pointer address is physical or virtual.

    Prasad wrote:
    > Hi All,
    >
    > When we create a pointer o say an integer variable or a function. And
    > then print it what it should print?
    >
    > Address in the physical memory where it is stored or virtual address?


    In general, this would be the kernel virtual address (being in
    comp.os.linux.development.system).
    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett


  3. Re: pointer address is physical or virtual.

    "Prasad" writes:

    > Hi All,
    >
    > When we create a pointer o say an integer variable or a function. And
    > then print it what it should print?
    >
    > Address in the physical memory where it is stored or virtual address?
    >
    > Thanks and regards,
    > Prasad.
    >


    It depends on what the reader needs.

    You might convert it to text! "Pointer 1 is to the function Foo" for
    example.

  4. Re: pointer address is physical or virtual.

    Okay, I will post the thing which confussed me.

    This is the program i am executing which used instrument-function
    facility of GCC.
    (it adds two function which are called automatically when any function
    begins its execution and exits its execution.)

    [Prasad@prasadjoshi test]$ cat instrument_functions.c
    #include

    void __cyg_profile_func_enter (void *this_fn, void *call_site)
    __attribute__ ((no_instrument_function)) ;
    void __cyg_profile_func_exit (void *this_fn, void *call_site)
    __attribute__ ((no_instrument_function)) ;

    void afun ()
    {
    printf ( "In afun ().\n" );
    }

    int main ()
    {
    int i = 0;
    printf ( "In Main.\n" ) ;
    afun () ;
    i = 10 ;
    while (1) ;
    }
    void __cyg_profile_func_enter (void *this_fn, void *call_site)
    {
    printf ( "In Entry Function.\t" ) ;
    printf ( "Pointer : %p\n", this_fn ) ;
    }
    void __cyg_profile_func_exit (void *this_fn, void *call_site)
    {
    printf ( "In Exit Function.\t" ) ;
    printf ( "Pointer : %p\n", this_fn ) ;
    }
    [Prasad@prasadjoshi test]$ gcc instrument_functions.c -o
    instrument_functions1 -finstrument-functions
    [Prasad@prasadjoshi test]$ gcc instrument_functions.c -o
    instrument_functions -finstrument-functions

    Created two copies of it for simultaneous execution, ran them from two
    different terminals

    [Prasad@prasadjoshi test]$ ./instrument_functions
    In Entry Function. Pointer : 0x804844e
    In Main.
    In Entry Function. Pointer : 0x8048414
    In afun ().
    In Exit Function. Pointer : 0x8048414

    [Prasad@prasadjoshi test]$ ./instrument_functions1
    In Entry Function. Pointer : 0x804844e
    In Main.
    In Entry Function. Pointer : 0x8048414
    In afun ().
    In Exit Function. Pointer : 0x8048414

    Now, I am not getting how could these both processes where loaded in
    the same memory as they are having same addresses for the functions?

    To add to the confussion, i tried with nm utility.
    [Prasad@prasadjoshi test]$ nm instrument_functions | grep -i 804844e
    0804844e T main
    [Prasad@prasadjoshi test]$ nm instrument_functions | grep -i 8048414
    08048414 T afun

    Now, I am not getting how could the main() and afun() addresses print
    by nm utility are same as the one printed by executing process.

    So finally I have two questions.
    1. Whether addresses printed from user program are virtual one or
    physicall one.
    ( if they are physicall then how could thes two processes are loaded in
    same memory).
    2. and what nm utility prints ==> virtyal addresses or physicall one.
    ( i know this is a disk file and whree the program is loaded does not
    depend on it but, still. )

    Thanks and regards.
    Praasd

    Hadron wrote:
    > "Prasad" writes:
    >
    > > Hi All,
    > >
    > > When we create a pointer o say an integer variable or a function. And
    > > then print it what it should print?
    > >
    > > Address in the physical memory where it is stored or virtual address?
    > >
    > > Thanks and regards,
    > > Prasad.
    > >

    >
    > It depends on what the reader needs.
    >
    > You might convert it to text! "Pointer 1 is to the function Foo" for
    > example.



  5. Re: pointer address is physical or virtual.

    Okay, I will post the thing which confussed me.

    This is the program i am executing which used instrument-function
    facility of GCC.
    (it adds two function which are called automatically when any function
    begins its execution and exits its execution.)

    [Prasad@prasadjoshi test]$ cat instrument_functions.c
    #include

    void __cyg_profile_func_enter (void *this_fn, void *call_site)
    __attribute__ ((no_instrument_function)) ;
    void __cyg_profile_func_exit (void *this_fn, void *call_site)
    __attribute__ ((no_instrument_function)) ;

    void afun ()
    {
    printf ( "In afun ().\n" );
    }

    int main ()
    {
    int i = 0;
    printf ( "In Main.\n" ) ;
    afun () ;
    i = 10 ;
    while (1) ;
    }
    void __cyg_profile_func_enter (void *this_fn, void *call_site)
    {
    printf ( "In Entry Function.\t" ) ;
    printf ( "Pointer : %p\n", this_fn ) ;
    }
    void __cyg_profile_func_exit (void *this_fn, void *call_site)
    {
    printf ( "In Exit Function.\t" ) ;
    printf ( "Pointer : %p\n", this_fn ) ;
    }
    [Prasad@prasadjoshi test]$ gcc instrument_functions.c -o
    instrument_functions1 -finstrument-functions
    [Prasad@prasadjoshi test]$ gcc instrument_functions.c -o
    instrument_functions -finstrument-functions

    Created two copies of it for simultaneous execution, ran them from two
    different terminals

    [Prasad@prasadjoshi test]$ ./instrument_functions
    In Entry Function. Pointer : 0x804844e
    In Main.
    In Entry Function. Pointer : 0x8048414
    In afun ().
    In Exit Function. Pointer : 0x8048414

    [Prasad@prasadjoshi test]$ ./instrument_functions1
    In Entry Function. Pointer : 0x804844e
    In Main.
    In Entry Function. Pointer : 0x8048414
    In afun ().
    In Exit Function. Pointer : 0x8048414

    Now, I am not getting how could these both processes where loaded in
    the same memory as they are having same addresses for the functions?

    To add to the confussion, i tried with nm utility.
    [Prasad@prasadjoshi test]$ nm instrument_functions | grep -i 804844e
    0804844e T main
    [Prasad@prasadjoshi test]$ nm instrument_functions | grep -i 8048414
    08048414 T afun

    Now, I am not getting how could the main() and afun() addresses print
    by nm utility are same as the one printed by executing process.

    So finally I have two questions.
    1. Whether addresses printed from user program are virtual one or
    physicall one.
    ( if they are physicall then how could thes two processes are loaded in
    same memory).
    2. and what nm utility prints ==> virtyal addresses or physicall one.
    ( i know this is a disk file and whree the program is loaded does not
    depend on it but, still. )

    Thanks and regards.
    Praasd

    Hadron wrote:
    > "Prasad" writes:
    >
    > > Hi All,
    > >
    > > When we create a pointer o say an integer variable or a function. And
    > > then print it what it should print?
    > >
    > > Address in the physical memory where it is stored or virtual address?
    > >
    > > Thanks and regards,
    > > Prasad.
    > >

    >
    > It depends on what the reader needs.
    >
    > You might convert it to text! "Pointer 1 is to the function Foo" for
    > example.



  6. Re: pointer address is physical or virtual.

    On Fri, 15 Dec 2006, Prasad wrote:

    > Created two copies of it for simultaneous execution, ran them from two
    > different terminals
    > [Prasad@prasadjoshi test]$ ./instrument_functions
    > In Entry Function. Pointer : 0x804844e
    > In Main.
    > In Entry Function. Pointer : 0x8048414
    > In afun ().
    > In Exit Function. Pointer : 0x8048414
    > [Prasad@prasadjoshi test]$ ./instrument_functions1
    > In Entry Function. Pointer : 0x804844e
    > In Main.
    > In Entry Function. Pointer : 0x8048414
    > In afun ().
    > In Exit Function. Pointer : 0x8048414
    > Now, I am not getting how could these both processes where loaded in
    > the same memory as they are having same addresses for the functions?
    > So finally I have two questions.
    > 1. Whether addresses printed from user program are virtual one or
    > physicall one.


    Virtual.

    Both the processess have the same virtual addresses, but kernel keeps
    different page tables for each process, so that they are resolved to
    different physical addresses when accessed.

    --
    Jiri Kosina

  7. Re: pointer address is physical or virtual.

    Thanks a lot jiri,

    What about the addresses printed by printf()
    do pointers always contain (print / point to) virtual addresses and not
    the physicall one?

    Thanks and regards,
    Prasad.

    Jiri Kosina wrote:
    > On Fri, 15 Dec 2006, Prasad wrote:
    >
    > > Created two copies of it for simultaneous execution, ran them from two
    > > different terminals
    > > [Prasad@prasadjoshi test]$ ./instrument_functions
    > > In Entry Function. Pointer : 0x804844e
    > > In Main.
    > > In Entry Function. Pointer : 0x8048414
    > > In afun ().
    > > In Exit Function. Pointer : 0x8048414
    > > [Prasad@prasadjoshi test]$ ./instrument_functions1
    > > In Entry Function. Pointer : 0x804844e
    > > In Main.
    > > In Entry Function. Pointer : 0x8048414
    > > In afun ().
    > > In Exit Function. Pointer : 0x8048414
    > > Now, I am not getting how could these both processes where loaded in
    > > the same memory as they are having same addresses for the functions?
    > > So finally I have two questions.
    > > 1. Whether addresses printed from user program are virtual one or
    > > physicall one.

    >
    > Virtual.
    >
    > Both the processess have the same virtual addresses, but kernel keeps
    > different page tables for each process, so that they are resolved to
    > different physical addresses when accessed.
    >
    > --
    > Jiri Kosina



  8. Re: pointer address is physical or virtual.

    Prasad wrote:

    > Hi All,
    >
    > When we create a pointer o say an integer variable or a function. And
    > then print it what it should print?


    The value of the pointer.



    > Address in the physical memory where it is stored or virtual address?


    The C language intentionally doesn't attempt to define the meaning of a
    pointer's value. However, in the context of a user-space Linux application,
    the pointer will generally refer to an address offset from where the
    application is mapped in virtual memory.

    Run the following program twice and you'd expect the same value in each
    instance:

    #include
    #include
    #include
    int main() {
    printf("%p\n", "Hello world");
    sleep(10);
    return EXIT_SUCCESS;
    }

    >
    > Thanks and regards,
    > Prasad.



  9. Re: pointer address is physical or virtual.

    On Fri, 15 Dec 2006, Prasad wrote:

    > What about the addresses printed by printf() do pointers always contain
    > (print / point to) virtual addresses and not the physicall one?


    Yes, user space programs always operate on virtual addresses.

    --
    Jiri Kosina

  10. Re: pointer address is physical or virtual.

    "Prasad" writes:

    > Hi All,
    >
    > When we create a pointer o say an integer variable or a function. And
    > then print it what it should print?
    >
    > Address in the physical memory where it is stored or virtual address?


    As a general rule of thumb, a user process never sees a physical
    address. The whole point is that all the user process ever sees is
    physical addresses; managing the physical address resource is up to
    the OS.

    If you had an application that was one of the tiny set of exceptions
    to this rule, you'd have to be doing enough very special things that
    you'd know it.
    --
    Joseph J. Pfeiffer, Jr., Ph.D. Phone -- (505) 646-1605
    Department of Computer Science FAX -- (505) 646-1002
    New Mexico State University http://www.cs.nmsu.edu/~pfeiffer

  11. Re: pointer address is physical or virtual.

    Joe Pfeiffer writes:
    > "Prasad" writes:
    >> When we create a pointer o say an integer variable or a function. And
    >> then print it what it should print?
    >>
    >> Address in the physical memory where it is stored or virtual address?

    >
    > As a general rule of thumb, a user process never sees a physical
    > address. The whole point is that all the user process ever sees is
    > physical addresses; managing the physical address resource is up to
    > the OS.
    >
    > to this rule, you'd have to be doing enough very special things that
    > you'd know it.


    On a system with a MMU and with the MMU enabled, 'physical address' is
    a term without meaning from the perspective of application code (and
    mostly kernel code, too). Exceptions are only the code that manages
    (or accesses) the page tables themselves and device drivers that need
    to pass adresses of memory buffers to be used by devices for DMA
    operations to these devices if no separate I/O address translation
    unit exists. In the latter case, 'physical addresses' are basically
    just cookies whose actual values don't matter for the code
    communicating them to devices.

  12. Re: pointer address is physical or virtual.

    Does that mean, addresses printed are virtual and not physical.

    Thanks and regards,
    Prasad.

    Jiri Kosina wrote:
    > On Fri, 15 Dec 2006, Prasad wrote:
    >
    > > Created two copies of it for simultaneous execution, ran them from two
    > > different terminals
    > > [Prasad@prasadjoshi test]$ ./instrument_functions
    > > In Entry Function. Pointer : 0x804844e
    > > In Main.
    > > In Entry Function. Pointer : 0x8048414
    > > In afun ().
    > > In Exit Function. Pointer : 0x8048414
    > > [Prasad@prasadjoshi test]$ ./instrument_functions1
    > > In Entry Function. Pointer : 0x804844e
    > > In Main.
    > > In Entry Function. Pointer : 0x8048414
    > > In afun ().
    > > In Exit Function. Pointer : 0x8048414
    > > Now, I am not getting how could these both processes where loaded in
    > > the same memory as they are having same addresses for the functions?
    > > So finally I have two questions.
    > > 1. Whether addresses printed from user program are virtual one or
    > > physicall one.

    >
    > Virtual.
    >
    > Both the processess have the same virtual addresses, but kernel keeps
    > different page tables for each process, so that they are resolved to
    > different physical addresses when accessed.
    >
    > --
    > Jiri Kosina



  13. Re: pointer address is physical or virtual.

    Thanks a lot for all ur help and description. I found a lot of
    information from all of the replies.
    Thanks to all very much.

    Prasad.

    Prasad wrote:
    > Hi All,
    >
    > When we create a pointer o say an integer variable or a function. And
    > then print it what it should print?
    >
    > Address in the physical memory where it is stored or virtual address?
    >
    > Thanks and regards,
    > Prasad.



  14. Re: pointer address is physical or virtual.

    "Prasad" wrote:
    >Does that mean, addresses printed are virtual and not physical.


    Absolutely.

    However, just to warp your perspective a little more... the
    physical addresses in the instance cited are almost certainly
    going to be identical too! Most executable files are read only
    code, which allows the same physical memory to be mapped into
    multiple instances of logical memory.

    Hence multiple invocations of a program do *not* cause added
    memory to be used to hold the same code. The executable file is
    paged into one physical location as required (which means only
    parts of it may ever actually be in physical memory) and only
    the data sections of the program are unique for each invocation.

    --
    Floyd L. Davidson
    Ukpeagvik (Barrow, Alaska) floyd@apaflo.com

+ Reply to Thread