Search code examples
c++unixsystem

Why does new make system calls if whole app is virtually maped?


what i understand is that writing a user space app will result in OS creating virtual space for this app which will be approx = whole amount of ram on the system, this way the app will think it's the owner of the computer and some of this space will only be mapped to physical hardware.

Accordingly, I know that new call system call I think posix_memalign, to allocate memory dynamically so why does it need system calls when it can just return a memory chunk from allocated virtual space instead it calls an OS function to do this.

Last thing, Is this memory is a user space memory or it belong to the OS (I mean OS allocate his own heap memory and just let app use it) This is maybe be a silly question but only because of the confusion.


Solution

  • what i understand is that writing a user space app will result in OS creating virtual space for this app which will be approx = whole amount of ram on the system

    No. Typical mainstream operating systems will create virtual memory to hold the program itself and the memory it requests as it requests more. There is no relation between the amount of virtual memory and the amount of physical memory, any more than there is a relation between the largest address number on a street and the count of houses on that street.

    why does it need system calls

    Increasing the amount of virtual memory in the process is often done by sbrk() or mmap(). These result in system calls, as they need to update kernel data structures to "map" more virtual memory addresses.

    Is this memory is a user space memory or it belong to the OS

    Virtual memory is something which both user-space and the kernel know about. I don't know what "belong" means in this context.