다중 서브 쓰레드는 개별적으로 힙을 가지나요?(예 : 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
---------------------
...

File attachments: 
첨부파일 크기
Image icon 문제의 이미지 (94.57 KB

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.