1
$\begingroup$

I had read about the various functionalities of system calls recently in one of my coursebooks and about how they are used to allocate memory for new processes. The book didn't specify whether it was possible to combine multiple memory allocation requests within a single system call. So I asked one of my teachers and they said that it was indeed possible. I feel that this would probably decrease the system's performance in some way but I cannot seem to give a concrete reason for the same.

Could someone explain why the performance drops in this case?

Thank you for your time.

$\endgroup$
4
  • $\begingroup$ Okay, Thank you! $\endgroup$
    – qwertybro
    Jan 28 '20 at 16:24
  • $\begingroup$ Repost from this? cs.stackexchange.com/q/120046/98 Same class? $\endgroup$
    – Raphael
    Jan 28 '20 at 17:26
  • $\begingroup$ @Raphael, yup, the original version was re-posted word-for-word; see my comment there. $\endgroup$
    – D.W.
    Jan 28 '20 at 17:32
  • $\begingroup$ As a matter of fact, we are from the same class. $\endgroup$
    – qwertybro
    Jan 28 '20 at 18:09
1
$\begingroup$

In a standard operating system like Unix, no. You can't directly make multiple requests. But, you can make one big request for a lot of memory. The system call lets you request a certain amount of memory, and you choose how much to request.

In practice your program doesn't make that system call every time you want to allocate memory. Instead, you have a standard library function, such as malloc(), which has a pool of available memory. When you call malloc(), it selects a region of available memory and allocates that to the caller, and keeps track that it has been allocated. When the available memory runs out, then it uses a system call to ask the operating system for a bunch more memory. So, it might only need to invoke the system call infrequently, even if you call malloc() a lot.

See https://en.wikipedia.org/wiki/Memory_management#DYNAMIC, https://en.wikipedia.org/wiki/Sbrk, https://en.wikipedia.org/wiki/C_dynamic_memory_allocation, https://en.wikipedia.org/wiki/C_dynamic_memory_allocation.

$\endgroup$
4
  • $\begingroup$ I see. Could you elaborate a bit more why exactly we cannot have multiple malloc() statements in a single system call? Wouldn't this be a more efficient approach rather than repeatedly passing individual system calls with a single malloc() statement? $\endgroup$
    – qwertybro
    Jan 28 '20 at 18:16
  • $\begingroup$ @qwertybro, of course anything is possible but that doesn't necessarily make it a good idea. At this point I'd encourage you to do more reading to understand how existing systems work. There's lots of information available online on how malloc works, how sbrk works, etc. Be resourceful! I'm sure you can learn a lot! $\endgroup$
    – D.W.
    Jan 28 '20 at 18:37
  • $\begingroup$ I'll make sure to do that. Thank you for the help! $\endgroup$
    – qwertybro
    Jan 28 '20 at 18:40
  • $\begingroup$ @qwertybro, one compelling reason is that asking for several operations in one go complicates the interface enormously (need to state there are 25 requests today, and give them all; have to return 25 results; user has to check them all... but 99.753% of the time only one operation is asked for). $\endgroup$
    – vonbrand
    Feb 19 '20 at 1:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.