1

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.

KMG
  • 1,433
  • 1
  • 8
  • 19
  • 2
    Your first statement is false, so the whole premise of your question is flawed. The OS does not create virtual space for an app approximately equal to the amount of RAM on the system. It typically allocates much less. If the app needs more memory than the OS has allocated for it, it needs to request it - and, for that, it uses system calls. All physical and virtual memory is managed by (components of) the operating system, and effectively loaned to apps for their exclusive use. Which, roughly, is why a modern OS can recover that memory when a program terminates. – Peter Jul 04 '20 at 06:59

1 Answers1

3

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.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • ok but having a simple hello world with a new statment will still call this system call or not i mean this virtual address space isn't fully used yet right ? – KMG Jul 04 '20 at 07:03
  • Maybe, maybe not. It's not specified whether the very first time you call `new` will need to call `sbrk()` or not. And of course it depends how much memory you ask for. – John Zwinck Jul 04 '20 at 07:19