리눅스에서 malloc동작원리가 궁금합니다.

milkrookie의 이미지

제가 malloc함수를 구현을 했는데요. brk와 sbrk를 이용해서 구현을 했습니다.
메인함수가 시작되면 brk와 sbrk로 특정 메모리 영역을 할당을 해버리고 그 메모리 안에서만 malloc을 사용하는데요.
제가 작성한 malloc함수로 프로그램을 짜서 실행을 시키면 작동이 잘됩니다.
그런데 메모리 할당된 모습을 보려고 제가 작성한 프로그램을 pin tool을 이용해서 돌리니 메모리를 할당할 수 없다고 에러메세지가 발생해버리네요...(에러메세지는 sbrk를 이용해서 할당이 실패해 반환값이 -1이 나오면 출력하도록 제가 만든 메세지입니다.)

메모리를 처음에 할당하는 방법은 sbrk(Memory_size)를 이용해서 할당을 했습니다.(Memory size는 1.7G정도로 잡은 상태입니다. OS는 우분투 12.04 64bit버전이고요.)
그냥 기본 malloc을 이용해서 작성하면 pin tool에서 잘 돌아가는데 제가 만든 malloc을 이용하면 왜 안돌아가는지 그 이유를 모르겠습니다.
혹시 조언좀 해주실수있나요??

bushi의 이미지

glibc malloc() 이라면 소스 보시면 동작원리 정도는 껌이고요.

brk 가 있는 아키텍쳐라도 항상 brk 만 사용하는 것은 아니고,
malloc 할 때마다 매번 brk 나 mmap(MAP_ANONYMOUS) 가 호출되는 것도 아닙니다.
아무튼... 소스 보셔야 아래 내용이 이해가 가실겁니다.

[bushi@rose kernel]$ cat m.c
#include <stdlib.h>
int main(int argc, char **argv)
{
	void *mem1 = malloc(atoi(argv[1]));
	void *mem2 = malloc(atoi(argv[1]));
	return mem1 == NULL || mem2 == NULL;
}
[bushi@rose kernel]$ 
[bushi@rose kernel]$ 
[bushi@rose kernel]$ gcc -o m m.c -Wall -O0 -g
[bushi@rose kernel]$
[bushi@rose kernel]$ 
[bushi@rose kernel]$ 
[bushi@rose kernel]$ ltrace -S ./m 128
...
...
__libc_start_main(0x80483f4, 2, 0xbffea9c4, 0x8048470, 0x8048460 <unfinished ...>
atoi(0xbffeb504, 0, 0x285cc0, 0x284ff4, 0x8048470) = 128
malloc(128 <unfinished ...>
SYS_brk(NULL)                                    = 0x8ef7000
SYS_brk(0x8f18000)                               = 0x8f18000
<... malloc resumed> )                           = 0x8ef7008
atoi(0xbffeb504, 0, 0x285cc0, 0x284ff4, 0x8048470) = 128
malloc(128)                                      = 0x8ef7090
SYS_exit_group(0 <no return ...>
+++ exited (status 0) +++
[bushi@rose kernel]$ 
[bushi@rose kernel]$ 
[bushi@rose kernel]$ 
[bushi@rose kernel]$ ltrace -S ./m 1000000
...
...
__libc_start_main(0x80483f4, 2, 0xbfadda54, 0x8048470, 0x8048460 <unfinished ...>
atoi(0xbfadf500, 0, 0xc7acc0, 0xc79ff4, 0x8048470) = 0xf4240
malloc(1000000 <unfinished ...>
SYS_mmap2(0, 0xf5000, 3, 34, -1)                 = 0xb77a2000
<... malloc resumed> )                           = 0xb77a2008
atoi(0xbfadf500, 0, 0xc7acc0, 0xc79ff4, 0x8048470) = 0xf4240
malloc(1000000 <unfinished ...>
SYS_mmap2(0, 0xf5000, 3, 34, -1)                 = 0xb76ad000
<... malloc resumed> )                           = 0xb76ad008
SYS_exit_group(0 <no return ...>
+++ exited (status 0) +++
[bushi@rose kernel]$ 

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.