0

Concerning malloc and systemcall - I would like to know what's happening when the systemcall is made. Malloc is just a library-function right?

So - when calling malloc and there are unmapped memory left on the heap - no system call is made, I guess. But, let say the allocated heap becomes full - the heap has to grow. Here, I do not know how a trap is triggered?

A trap is needed to make the system call, so a kernel function such as brk() or mmap() can be made - but how is it made? Is it through some kind of exception internally?

I am interested to know!!!

Edit: concerning the other question - I have looked at it and cannot se (by the moment) anything about systemcall functions, traps in the cpu - instead its much about why the program crashes

Jolta
  • 2,620
  • 1
  • 29
  • 42
java
  • 1,165
  • 1
  • 25
  • 50
  • 3
    possible duplicate of [How do malloc() and free() work?](http://stackoverflow.com/questions/1119134/how-do-malloc-and-free-work) – Joe Jun 26 '15 at 10:51
  • @joe - where can I read about trap and systemcall in the other question? – java Jun 26 '15 at 11:08
  • 1
    The hardware memory-management system generates an interrupt when your software tries to access a page that is not loaded. The page requested gets loaded and mapped, the CPU instruction that triggered the interrupt is then restarted. – Martin James Jun 26 '15 at 11:14
  • 1
    On operating systems with memory protection (Unix-like - e.g. Linux, Windows NT and successors) you need a special mechanism to switch execution of the code from user to kernel mode durin a system call. This mechanism differs between HW architectures and OS. ---- For example Linux on x86 invokes interrupt by a special instruction `int` (or newer `sysenter` and on x86_64 `syscall`). The interrupt handler runs in kernel space and takes care of calling the requested code (the system call routine). ---- See for example: http://www3.cs.stonybrook.edu/~porter/courses/cse506/f11/slides/interrupts.pdf – pabouk - Ukraine stay strong Jun 26 '15 at 11:51
  • @java: I don't know how to link the specific entry, but see the answer from mgalgs on Sep 16 2013 at 21:07. – Steve Summit Jun 26 '15 at 12:12

2 Answers2

1

No special traps or exceptions are needed. Pseudocode for malloc is:

void *malloc(size_t size)
{
    search for 'size' free space in available blocks;
    if(no block found}
        {
        request additional memory from OS;
        construct new block;
        }
    mark 'size' bytes used in block;
    return pointer into block;
}

The step you're wondering about is request additional memory from OS; and it is, as you speculated, typically a straightforward function call to sbrk.

(It's true, when you call sbrk, somewhere in its implementation there's going to be a special mechanism, such as a trap, to perform the context switch into the operating system so that it can do its work for you, but this will be the same sort of mechanism as is used for all system calls. It's not something you generally need to worry about: you just call sbrk() like any other function.)

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
  • The `sbrk` call is a system call which needs the "special trap" to work on systems with memory protection. See my comment http://stackoverflow.com/questions/31071204/malloc-systemcall-how-its-made#comment50163589_31071204 – pabouk - Ukraine stay strong Jun 26 '15 at 11:55
  • @pabouk: sure, but I didn't think that's what the OP was asking. Whatever the syscall mechanism is, it applies to all system calls (there's nothing special about `brk` and `sbrk`), and in any case, it's invisible to the normal C programmer: you just call `sbrk()` in your code like any other function, and the code in the library -- the code implementing `sbrk`, that is -- takes care of the magic syscall mechanism for you. – Steve Summit Jun 26 '15 at 11:59
  • According to Jon Masters, Richard Blum (Professional Linux programming) many system C library functions are simply wrappers around a system call that actually carry out the requested task. I think this could be applied to malloc. So @SteveSummit - you may be correct about this. – java Jun 26 '15 at 12:07
  • But at the same time - a systemcall provides a trap so the caller looses control over whats happening in the protected kernel mode. http://www.bottomupcs.com/system_calls.html – java Jun 26 '15 at 12:12
  • 1
    But at one level, this is no different from the way you always "lose control" when you call some other function to have it do a job for you. – Steve Summit Jun 26 '15 at 12:17
1

In order to have an idea how malloc is made, you can write a program in which you call malloc and then use this command : strace yourprogram.

With this command, you can see in depth the trace system calls and signals when you are calling malloc and have an idea how it's made.

alifirat
  • 2,899
  • 1
  • 17
  • 33