다중 서브 쓰레드는 개별적으로 힙을 가지나요?(예 : heap_info가 1개? n개?)

#include <stdio.h> #include <stdlib.h> #include <pthread.h> void* threadFunc1() { int t1_stack=0x41; char *t1_heap=malloc(0x10); char *t1_hhp=malloc(0x10000); printf("&t1_stack=%p\n",&t1_stack); printf("t1_heap=%p\n",t1_heap); printf("t1_code=%p\n",threadFunc1); return NULL; } void* threadFunc2() { int t2_stack=0x41; char *t2_heap=malloc(0x10); printf("&t2_stack=%p\n",&t2_stack); printf("t2_heap=%p\n",t2_heap); printf("t2_code=%p\n",threadFunc2); return NULL; } void* threadFunc3() { int t3_stack=0x41; char *t3_heap=malloc(0x10); printf("&t3_stack=%p\n",&t3_stack); printf("t3_heap=%p\n",t3_heap); printf("t3_code=%p\n",threadFunc3); return NULL; } int data=0x44; int bss; int main() { void* (*threads_other[3])(void *) = {threadFunc1, threadFunc2, threadFunc3}; pthread_t threads_other_id[3]; pthread_t threads_id[2]; int ids1[3]; int ids2[2]; int ret; int m_stack=0x43; char *m_heap=malloc(0x10); char *m_heap_mmapChunk=malloc(0x23000); printf("&m_stack=%p\n",&m_stack); printf("m_heap=%p\n",m_heap); printf("m_heap_mmapChunk=%p\n",m_heap_mmapChunk); printf("m_code=%p\n",main); printf("&data=%p\n",&data); printf("&bss=%p\n",&bss); printf("libc=%p\n",system); for (int i = 0; i < 3; i++) { ids1[i] = i; ret = pthread_create(&threads_other_id[i], NULL, threads_other[i], &ids1[i]); if (ret) { printf("Error creating thread %d\n", i); exit(1); } } for (int i = 0; i < 2; i++) { ids2[i] = i; ret = pthread_create(&threads_id[i], NULL, threadFunc1, &ids2[i]); if (ret) { printf("Error creating thread %d\n", i); exit(1); } } // 모두 join 해주기 for (int i = 0; i < 3; i++) { pthread_join(threads_other_id[i], NULL); } for (int i = 0; i < 2; i++) { pthread_join(threads_id[i], NULL); } printf("All threads finished\n"); return 0; }
이 소스는 대충 아래와 같은 동작을 합니다
1.threadFunc1 기반의 쓰레드 생성 ->malloc(0x10), malloc(0x10000)
2.threadFunc2 기반의 쓰레드 생성 ->malloc(0x10), malloc(0x10000)
3.threadFunc3 기반의 쓰레드 생성 ->malloc(0x10), malloc(0x10000)
4.threadFunc1 기반의 쓰레드 생성 ->malloc(0x10), malloc(0x10000)
5.threadFunc1 기반의 쓰레드 생성 ->malloc(0x10), malloc(0x10000)
그리고 gdb에서 pthread_join 부분에 브레이크 포인트를 잡고 vmmap으로 출력 시, 아래와 같이 페이지가 보이는데,
0x555555559000 0x55555557a000 rw-p 21000 0 [heap] =main 쓰레드 힙
0x7ffff0000000 0x7ffff0031000 rw-p 31000 0 [anon_7ffff0000] =sub 쓰레드 힙
0x7ffff0031000 0x7ffff4000000 ---p 3fcf000 0 [anon_7ffff0031]
0x7ffff4c00000 0x7ffff4c01000 ---p 1000 0 [anon_7ffff4c00]
0x7ffff4c01000 0x7ffff5401000 rw-p 800000 0 [anon_7ffff4c01] =sub 쓰레드 스택
0x7ffff5600000 0x7ffff5601000 ---p 1000 0 [anon_7ffff5600]
0x7ffff5601000 0x7ffff5e01000 rw-p 800000 0 [anon_7ffff5601] =sub 쓰레드 스택
0x7ffff6000000 0x7ffff6001000 ---p 1000 0 [anon_7ffff6000]
0x7ffff6001000 0x7ffff6801000 rw-p 800000 0 [anon_7ffff6001] =sub 쓰레드 스택
0x7ffff6a00000 0x7ffff6a01000 ---p 1000 0 [anon_7ffff6a00]
0x7ffff6a01000 0x7ffff7201000 rw-p 800000 0 [anon_7ffff6a01] =sub 쓰레드 스택
0x7ffff7400000 0x7ffff7401000 ---p 1000 0 [anon_7ffff7400]
0x7ffff7401000 0x7ffff7c01000 rw-p 800000 0 [anon_7ffff7401] =sub 쓰레드 스택
여기서 threadFunc1~3 모두
0x7ffff0000000 0x7ffff0031000 rw-p 31000 0 [anon_7ffff0000] =sub 쓰레드 힙
을 힙으로 사용합니다
그리고 자세히 해당 힙을 살펴보면,
0x7ffff0000000 ~0x7ffff0000030-1 ->heap_info(0x30 크기)
0x7ffff0000030~0x7ffff000008d0-1 ->struct malloc_state(0x898)+padding(0x8)
0x7ffff000008d0~0x7ffff00000b60-1 ->tcache_perthread_struct (0x280)+chunk header(0x10)
0x7ffff00000b60~0x7ffff0030c30-1 ->chunk (여러개의 청크들...)
0x7ffff0030c30~0x7ffff0031000-1 ->top chunk
이런형태입니다
즉, 아래와 같습니다
-----------------------------------
heap_info
malloc_state(sub arena)
tcache
chunks (기타 여러개의 청크들...)
top chunk
-----------------------------------
즉, 이와같은 분석내용을 정리하자면,
각각의 쓰레드는 쓰레드마다 개별적으로 heap_info 등을 소유하는 것이 아닌
하나의 heap_info 등을 공유하는 것을 확인할 수 있는데...
문제는 아래의 링크에서 설명한 내용과 달라서...
이 링크의 내용이 잘못된 것인지...
아니면, 제가 테스트한게 해당 링크에서 설명한 것과는 다른 것을 테스트한것인지...
궁금해서 질문드립니다
(링크)
https://tribal1012.tistory.com/78
(문제의 이미지)
->이미지는 해당 링크에도 있습니다,("thread_arena를 그림으로 나타낸 경우(여러개의 힙 영역)")
일단 이 이미지를 제가 이해한대로라면, 아래와 같다는 의미입니까?
---------------------
서브 쓰레드1
heap_info
malloc_state
tcache
chunks
top chunk
---------------------
서브 쓰레드2
heap_info
tcache
chunks
top chunk
---------------------
...
첨부 | 파일 크기 |
---|---|
![]() | 94.57 KB |
댓글 달기