Master-Master-Memory Model

xiaoxiao2021-04-10  394

Master-Master-Memory Model

Learn Linux's memory model, may not allow you to significantly improve programming capabilities, but as a basic knowledge point should be familiar. When you take a train out, you are so knowing that you will not know about the place along the way and you can still reach the target. But you are very clear about the whole road, every time you know where you are, know the local customs, compare what you want, the journey may be more interesting.

Similarly, understanding Linux memory models, you know what kind of location per memory, each variable is in the system. This will also make you feel happy, know this, sometimes it will make your life more loose. Look at the address of the variable, you can get roughly whether this is a valid address. A variable is destroyed, you can roughly infer whom is a suspect.

Linux's memory model, generally:

address

effect

Description

> = 0xc000 0000

Nuclear virtual memory

User code is not visible area

<0xc000 0000

Stack (User Stack)

ESP points to the top of the stack

Idle memory

> = 0x4000 0000

File mapping area

<0x4000 0000

Idle memory

HEAP (Running)

Adding a heap through the BRK / SBRK system call, grow up.

.DATA, .BSS (readout)

Load from the executable

> = 0x0804 8000

.RODATA (read-only)

Load from the executable

<0x0804 8000

Reserved area

Many books have similar descriptions, this map is taken from "in-depth understanding of computer system" P603, and makes a slight change. This picture is more likely to understand, but there is still two shortcomings. The following additions:

1. The first point is about running.

To illustrate this problem, let's run a test program and observe the results:

#include

Int main (int Argc, char * argv [])

{

INT first = 0;

INT * p0 = malloc (1024);

INT * p1 = malloc (1024 * 1024);

INT * P2 = Malloc (512 * 1024 * 1024);

INT * P3 = Malloc (1024 * 1024 * 1024);

Printf ("Main =% P Print =% P / N", Main, Printf;

Printf ("first =% p / n", & first);

Printf ("P0 =% P P1 =% P P2 =% P p3 =% P / N", P0, P1, P2, P3);

GetChar ();

Return 0;

}

After running, the output is:

Main = 0x8048404 print = 0x8048324

First = 0xBFCD1264

P0 = 0x9253008 P1 = 0xB7EC0008 P2 = 0x97ebf008 P3 = 0x57ebe008

l Main and Print two functions are code segments (.Text), their address meets the description of the table.

l First is the first temporary variable. Since there are some environment variables before first, its value is not 0xBFFFFFF, but 0xBFCD1264, which is normal.

l P0 is allocated in the heap, its address is less than 0x4000 0000, which is normal. L but P1 and P2 are also allocated in the heap, and its address is greater than 0x4000 0000, which does not match the table one.

The reason is that the position of the running pile is related to the memory management algorithm, that is, the implementation of Malloc. With regard to the memory management algorithm, we have a detailed description in the subsequent article, which is only a brief description. In the memory management algorithm implemented in the Glibc, Malloc small block memory is allocated in memory less than 0x4000 0000, and expands through BRK / SBRK, and allocates large block memory, MalloC calls MMAP implementation, allocated addresses In the file mapping area, its address is greater than 0x4000 000 0000.

See a little clear from the MAPS file:

00514000-00515000 r-xp 00514000 00:00 0

00624000-0063E000 r-xp 00000000 03:01 718192 / LIB / LD-

2.3.5

.so

0063E000

-0063F

000 r-xp 00019000 03:01 718192 / lib / ld-

2.3.5

.so

0063f

000-00640000 rwxp

0001A

000 03:01 718192 / LIB / LD-

2.3.5

.so

00642000-00766000 r-xp 00000000 03:01 718193 / LIB / LIBC-

2.3.5

.so

00766000-00768000 r-xp 00124000 03:01 718193 / LiB / libc-

2.3.5

.so

00768000

-0076A

000 RWXP 00126000 03:01 718193 / LiB / libc-

2.3.5

.so

0076A

000

-0076C

000 rwxp

0076A

000 00:00 0

08048000-08049000 r-xp 00000000 03:01 1307138 / ROOT/Test/Mem/t.exe

08049000

-0804a

000 rw-p 00000000 03:01 1307138 / ROOT/Test/Mem/t.exe

09F

5D000

-09f

7e000 rw-p

09F

5D000 00:00 0 [HEAP]

57E

2F

000-B

7f

35000 RW-P 57E

2F

000 00:00 0

B

7f

44000-B

7f

45000 rw-p b

7f

44000 00:00 0

BFB

2F

000-bfb45000 rw-p bfb

2F

000 00:00 0 [stack]

2. The second is about multithreading.

The current application, multi-threaded. The model described in Table cannot be applied to a multi-threaded environment. According to the table, the program has the most stack space on G, in fact, in the case of multi-thread, the stack space that can be used is very limited. To illustrate this problem, we look at another test:

#include

#include

Void * thread_proc (void * param)

{

INT first = 0;

INT * p0 = malloc (1024);

INT * p1 = malloc (1024 * 1024);

Printf ("(0x% x): first =% p / n", pthread_self (), & first); printf ("(0x% x): p0 =% p p1 =% p / n", pthread_self (), p0 , P1);

Return 0;

}

#define n 5

Int main (int Argc, char * argv [])

{

INT first = 0;

INT i = 0;

Void * Ret = NULL;

PTHREAD_T TID [N] = {0};

Printf ("first =% p / n", & first);

For (i = 0; i

{

Pthread_create (TID I, NULL, Thread_Proc, NULL);

}

For (i = 0; i

{

Pthread_join (TID [I], & RET);

}

Return 0;

}

After running, the output is:

First = 0xBFD3D

35C

(0xB

7f

2cbb0): first = 0xB

7f

2C

454

(0xB

7f

2cbb0): p0 = 0x84D52D8 p1 = 0xB

4C

27008

(0xB752BBB0): first = 0xB752B454

(0xB752BBB0): P0 = 0x84d56e0 p1 = 0xB4B26008

(0xB6B2ABB0): first = 0xB6b

2A

454

(0xB6B2ABB0): P0 = 0x84D5AE8 P1 = 0xB

4A

25008

(0xB6129BB0): first = 0xB6129454

(0xB6129BB0): P0 = 0x84d5ef0 p1 = 0xB4924008

(0xB5728BB0): first = 0xB5728454

(0xB5728BB0): P0 = 0x84D

62F

8 p1 = 0xB7e

2C

008

Let's take a look:

The main thread is between the stack of the first thread: 0xBFD3D

35C

- 0xB

7f

2C

454 = 0x7e

10f

08 =

126M

The distance between the first thread and the stack of the second thread: 0xB

7f

2C

454 - 0xB752B454 = 0xA01000 =

10m

The distance between several other threads is

10m

.

In other words, the main thread has the largest spatial

126M

And the stack space of the ordinary thread is only

10m

The range will result in a stack in the range.

The consequences of the stack are more serious, or the segmentation fault errors, or inexplicable errors.

TRACKBACK: http://tb.blog.9cbs.net/trackback.aspx?postid=814268