[리눅스] System call - brk / mmap

brk(2) - Linux manual page

brk / sbrk

#include <unistd.h>

int brk(void *addr);
void *sbrk(intptr_t increment);
int main()
{
	void *first = sbrk(0);
	void *second = sbrk(4096);
	void *third = sbrk(0); //calling sbrk(0) essentially returns the current
												 //position of the program break
	printf("First: %p ...
}
//First: 0xfde000
//First: 0xfde000
//First: 0xfdf000

mmap

#include <sys/mman.h>
void *mmap(void addr[.length], size_t length, int prot, 
						int flags,intfd, off_t offset);
int munmap(void addr[.length], size_t length);
#define PAGESIZE 4096

int main()
{
	//addr of null means we don't care where memory is allocated
	//PAGESIZE will be multiples of 4kb even if you don't request a size that's
	//multiple of 4kB
	
	uint8_t* first = mmap(NULL, PAGESIZE, PROT_READ | PROT_WRITE,
															MAP_PRIVATE } MAP_ANONYMOUS, -1, 0);
	uint8_t* second = mmap(NULL, PAGESIZE, PROT_READ | PROT_WRITE,
															MAP_PRIVATE } MAP_ANONYMOUS, -1, 0);
	printf("First: %p\\n", first);
	printf("Second: %p\\n", second);
}