Re: Memory allocations in linux for processes

2013-03-20 Thread Mulyadi Santosa
On 3/19/13, Niroj Pokhrel  wrote:
>
> Hi Mulyadi .
> Thank you very much But I still have a minor confusion .
> All I ran was this short program
>
> #include
> int main()
> {
> while(1)
> {
> }
> return 0;
> }

well, before your program is loaded, certainly one or more memory
region must be allocated in RAM, right? if not, where will it be
stored ?

Like Valdis said, strace utility could help you understanding things
during the binary execution. You'll see lots of mmap, I promise :)

-- 
regards,

Mulyadi Santosa
Freelance Linux trainer and consultant

blog: the-hydra.blogspot.com
training: mulyaditraining.blogspot.com

___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: Memory allocations in linux for processes

2013-03-19 Thread Valdis . Kletnieks
On Tue, 19 Mar 2013 20:41:55 +0530, Niroj Pokhrel said:

> #include
> int main()
> {
> while(1)
> {
> }
> return 0;
> }
>
> I don't understand where does mmap or malloc come in to play in this code.

Unless you linked it statically, a lot of stuff happens before you ever
get to main() - namely, any shared library linking and mapping.  Run
strace on your binary and see how many system calls happen before you hit
the infinite loop.


pgpjT3sQt6KU0.pgp
Description: PGP signature
___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: Memory allocations in linux for processes

2013-03-19 Thread Niroj Pokhrel
On Tue, Mar 19, 2013 at 8:27 PM, Mulyadi Santosa
wrote:

> Hi Niroj
>
> Please see inlined answer below...
>
> On Mon, Mar 18, 2013 at 5:28 PM, Niroj Pokhrel 
> wrote:
> > Hi all,
> > As I have read, whenever we execute a program, the memory is allocated in
> > different sections viz stack, text and data segment.
>
> Yup, that is correct
>
> > But, since we have used
> > loader and linker the three allocations will happen for them too.
>
> Correct again. Linker and loader will be the part of process address space.
>
> > But when I ran a program and did pmap pid, I saw several other fields
> which
> > I have no idea of. What is the [anon] and what is it doing here ?
>
> Anon is short of anonymous. It is a region of memory created by
> function e.g malloc() or mmap()
>
> >Why the
> > memory is being implemented for it and there are so many of them ?
>
> Drawing from above explanation, we can easily conclude somewhere in
> the program, it does malloc() or mmap().
>
> > And also the stack ( 3rd last element), is it the process stack??
>
> Yes, it's process' stack
>
> >Can
> > somebody please explain.
> > Thanking you all in advance.
> >
> > 5448:   ./a.out
> > 0040  4K r-x--  /home/n.pokhrel/Personal/mywork/a.out
> > 0060  4K r  /home/n.pokhrel/Personal/mywork/a.out
> > 00601000  4K rw---  /home/n.pokhrel/Personal/mywork/a.out
> > 7f57f5e18000   1512K r-x--  /lib/libc-2.11.1.so
> > 7f57f5f92000   2044K -  /lib/libc-2.11.1.so
> > 7f57f6191000 16K r  /lib/libc-2.11.1.so
> > 7f57f6195000  4K rw---  /lib/libc-2.11.1.so
> > 7f57f6196000 20K rw---[ anon ]
> > 7f57f619b000128K r-x--  /lib/ld-2.11.1.so
> > 7f57f6396000 12K rw---[ anon ]
> > 7f57f63b8000  8K rw---[ anon ]
> > 7f57f63ba000  4K r  /lib/ld-2.11.1.so
> > 7f57f63bb000  4K rw---  /lib/ld-2.11.1.so
> > 7f57f63bc000  4K rw---[ anon ]
> > 7fffa2f37000 84K rw---[ stack ]
>
> > 7fffa2fa  4K r-x--[ anon ]
> > ff60  4K r-x--[ anon ]
>
> ok, one of the region above, I think it's VDSO (virtual dynamic shared
> object). It's a region that handle system call. When a process does
> system call, it jumps to VDSO, and it is the VDSO that does the
> actuall syscall
>
> --
> regards,
>
> Mulyadi Santosa
> Freelance Linux trainer and consultant
>
> blog: the-hydra.blogspot.com
> training: mulyaditraining.blogspot.com
>



Hi Mulyadi .
Thank you very much But I still have a minor confusion .
All I ran was this short program

#include
int main()
{
while(1)
{
}
return 0;
}

I don't understand where does mmap or malloc come in to play in this code.
It would be great if you can give me some insight.
Thanking you in advance.

Yours,
Niroj Pokhrel
___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: Memory allocations in linux for processes

2013-03-19 Thread Mulyadi Santosa
Hi Niroj

Please see inlined answer below...

On Mon, Mar 18, 2013 at 5:28 PM, Niroj Pokhrel  wrote:
> Hi all,
> As I have read, whenever we execute a program, the memory is allocated in
> different sections viz stack, text and data segment.

Yup, that is correct

> But, since we have used
> loader and linker the three allocations will happen for them too.

Correct again. Linker and loader will be the part of process address space.

> But when I ran a program and did pmap pid, I saw several other fields which
> I have no idea of. What is the [anon] and what is it doing here ?

Anon is short of anonymous. It is a region of memory created by
function e.g malloc() or mmap()

>Why the
> memory is being implemented for it and there are so many of them ?

Drawing from above explanation, we can easily conclude somewhere in
the program, it does malloc() or mmap().

> And also the stack ( 3rd last element), is it the process stack??

Yes, it's process' stack

>Can
> somebody please explain.
> Thanking you all in advance.
>
> 5448:   ./a.out
> 0040  4K r-x--  /home/n.pokhrel/Personal/mywork/a.out
> 0060  4K r  /home/n.pokhrel/Personal/mywork/a.out
> 00601000  4K rw---  /home/n.pokhrel/Personal/mywork/a.out
> 7f57f5e18000   1512K r-x--  /lib/libc-2.11.1.so
> 7f57f5f92000   2044K -  /lib/libc-2.11.1.so
> 7f57f6191000 16K r  /lib/libc-2.11.1.so
> 7f57f6195000  4K rw---  /lib/libc-2.11.1.so
> 7f57f6196000 20K rw---[ anon ]
> 7f57f619b000128K r-x--  /lib/ld-2.11.1.so
> 7f57f6396000 12K rw---[ anon ]
> 7f57f63b8000  8K rw---[ anon ]
> 7f57f63ba000  4K r  /lib/ld-2.11.1.so
> 7f57f63bb000  4K rw---  /lib/ld-2.11.1.so
> 7f57f63bc000  4K rw---[ anon ]
> 7fffa2f37000 84K rw---[ stack ]

> 7fffa2fa  4K r-x--[ anon ]
> ff60  4K r-x--[ anon ]

ok, one of the region above, I think it's VDSO (virtual dynamic shared
object). It's a region that handle system call. When a process does
system call, it jumps to VDSO, and it is the VDSO that does the
actuall syscall

-- 
regards,

Mulyadi Santosa
Freelance Linux trainer and consultant

blog: the-hydra.blogspot.com
training: mulyaditraining.blogspot.com

___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Memory allocations in linux for processes

2013-03-18 Thread Niroj Pokhrel
Hi all,
As I have read, whenever we execute a program, the memory is allocated in
different sections viz stack, text and data segment. But, since we have
used loader and linker the three allocations will happen for them too.
But when I ran a program and did pmap pid, I saw several other fields which
I have no idea of. What is the [anon] and what is it doing here ? Why the
memory is being implemented for it and there are so many of them ?
And also the stack ( 3rd last element), is it the process stack?? Can
somebody please explain.
Thanking you all in advance.

5448:   ./a.out
0040  4K r-x--  /home/n.pokhrel/Personal/mywork/a.out
0060  4K r  /home/n.pokhrel/Personal/mywork/a.out
00601000  4K rw---  /home/n.pokhrel/Personal/mywork/a.out
7f57f5e18000   1512K r-x--  /lib/libc-2.11.1.so
7f57f5f92000   2044K -  /lib/libc-2.11.1.so
7f57f6191000 16K r  /lib/libc-2.11.1.so
7f57f6195000  4K rw---  /lib/libc-2.11.1.so
7f57f6196000 20K rw---[ anon ]
7f57f619b000128K r-x--  /lib/ld-2.11.1.so
7f57f6396000 12K rw---[ anon ]
7f57f63b8000  8K rw---[ anon ]
7f57f63ba000  4K r  /lib/ld-2.11.1.so
7f57f63bb000  4K rw---  /lib/ld-2.11.1.so
7f57f63bc000  4K rw---[ anon ]
7fffa2f37000 84K rw---[ stack ]
7fffa2fa  4K r-x--[ anon ]
ff60  4K r-x--[ anon ]
 total
___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies