I know for malloc sbrk is the system call invoked ,Similarly What is the system cal invoked when i write to a malloed memory(heap memory)
/* 10 byte of heap memory allocated */
char *ptr = malloc(5);
ptr = 10; // **What is the system call invoked for
writing into this heap memory** ?????
There are no system call involved in this case. Ask you compiler to generate assembly so that you can see that there is only some MOV instructions there. Or you can use a debugger to see the assembly
Accessing memory does not require a system call. On the contrary, accessing memory is what most of your code does most of the time! On a modern OS, you have a flat view of a contiguous range of virtual memory, and you typically only need a system call to mark a particular region (a “page”) of that memory as valid; other times, contiguously growing memory ranges such as the call stack don’t even require any action on your program’s part. It’s solely the job of your operating system’s memory manager to intercept accesses to memory that isn’t mapped to physical memory (via a page fault), do some kernel magic to bring the desired memory into physical space and return control to your program.
The only reason
malloc occasionally needs to perform a system call is because it asks the operating system for a random piece of virtual memory somewhere in the middle. If your program were to only function with global and local variables (but no dynamic allocation), you wouldn’t need any system calls for memory management.
“operating system doesn’t see every write that occurs: a write to memory corresponds simply to a STORE assembly instruction, not a system call. It is the hardware that takes care of the STORE and the necessary address translation. The only time the OS will see a memory write is when the address translation in the page tables fails, causing a trap to the OS. “
Please read the below link for details