What exactly happens in virtual memory when i call a function like printf in Linux?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












I'm asking about functions like printf that a lot of processes might use and also need the help of kernel for stuff like system calls



So can someone tell me step by step in detail that what happens? because I'm a little confused in this area, i have these questions :



  1. is the instructions for printf function inside of kernel part of our user process? and when it tries to execute printf, we do a JMP to that kernel location within the same user process but we go into kernel mode? or there is a context switch and a kernel process executes this?


  2. do all of the processes that execute functions like printf map to the same physical memory location when they call printf in their virtual memory?


  3. overall what are the situations that non kernel processes use the kernel part of the virtual memory?


enter image description here










share|improve this question























  • read the code of glibc you'll see. syscall instruction on x86_64, Intel's manual will tell you about it. Process don't get mapped into physical memory, don't know what you mean. Read Intel's manual.
    – ç¥žç§˜å¾·é‡Œå…‹
    1 hour ago










  • @神秘德里克 what do you mean processes don't get mapped into physical memory? when i said that i obviously meant pages of that virtual memory space belonging to that process get mapped into frames of physical memory.
    – John P
    1 hour ago














up vote
2
down vote

favorite












I'm asking about functions like printf that a lot of processes might use and also need the help of kernel for stuff like system calls



So can someone tell me step by step in detail that what happens? because I'm a little confused in this area, i have these questions :



  1. is the instructions for printf function inside of kernel part of our user process? and when it tries to execute printf, we do a JMP to that kernel location within the same user process but we go into kernel mode? or there is a context switch and a kernel process executes this?


  2. do all of the processes that execute functions like printf map to the same physical memory location when they call printf in their virtual memory?


  3. overall what are the situations that non kernel processes use the kernel part of the virtual memory?


enter image description here










share|improve this question























  • read the code of glibc you'll see. syscall instruction on x86_64, Intel's manual will tell you about it. Process don't get mapped into physical memory, don't know what you mean. Read Intel's manual.
    – ç¥žç§˜å¾·é‡Œå…‹
    1 hour ago










  • @神秘德里克 what do you mean processes don't get mapped into physical memory? when i said that i obviously meant pages of that virtual memory space belonging to that process get mapped into frames of physical memory.
    – John P
    1 hour ago












up vote
2
down vote

favorite









up vote
2
down vote

favorite











I'm asking about functions like printf that a lot of processes might use and also need the help of kernel for stuff like system calls



So can someone tell me step by step in detail that what happens? because I'm a little confused in this area, i have these questions :



  1. is the instructions for printf function inside of kernel part of our user process? and when it tries to execute printf, we do a JMP to that kernel location within the same user process but we go into kernel mode? or there is a context switch and a kernel process executes this?


  2. do all of the processes that execute functions like printf map to the same physical memory location when they call printf in their virtual memory?


  3. overall what are the situations that non kernel processes use the kernel part of the virtual memory?


enter image description here










share|improve this question















I'm asking about functions like printf that a lot of processes might use and also need the help of kernel for stuff like system calls



So can someone tell me step by step in detail that what happens? because I'm a little confused in this area, i have these questions :



  1. is the instructions for printf function inside of kernel part of our user process? and when it tries to execute printf, we do a JMP to that kernel location within the same user process but we go into kernel mode? or there is a context switch and a kernel process executes this?


  2. do all of the processes that execute functions like printf map to the same physical memory location when they call printf in their virtual memory?


  3. overall what are the situations that non kernel processes use the kernel part of the virtual memory?


enter image description here







kernel memory virtual-memory






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago

























asked 3 hours ago









John P

1246




1246











  • read the code of glibc you'll see. syscall instruction on x86_64, Intel's manual will tell you about it. Process don't get mapped into physical memory, don't know what you mean. Read Intel's manual.
    – ç¥žç§˜å¾·é‡Œå…‹
    1 hour ago










  • @神秘德里克 what do you mean processes don't get mapped into physical memory? when i said that i obviously meant pages of that virtual memory space belonging to that process get mapped into frames of physical memory.
    – John P
    1 hour ago
















  • read the code of glibc you'll see. syscall instruction on x86_64, Intel's manual will tell you about it. Process don't get mapped into physical memory, don't know what you mean. Read Intel's manual.
    – ç¥žç§˜å¾·é‡Œå…‹
    1 hour ago










  • @神秘德里克 what do you mean processes don't get mapped into physical memory? when i said that i obviously meant pages of that virtual memory space belonging to that process get mapped into frames of physical memory.
    – John P
    1 hour ago















read the code of glibc you'll see. syscall instruction on x86_64, Intel's manual will tell you about it. Process don't get mapped into physical memory, don't know what you mean. Read Intel's manual.
– ç¥žç§˜å¾·é‡Œå…‹
1 hour ago




read the code of glibc you'll see. syscall instruction on x86_64, Intel's manual will tell you about it. Process don't get mapped into physical memory, don't know what you mean. Read Intel's manual.
– ç¥žç§˜å¾·é‡Œå…‹
1 hour ago












@神秘德里克 what do you mean processes don't get mapped into physical memory? when i said that i obviously meant pages of that virtual memory space belonging to that process get mapped into frames of physical memory.
– John P
1 hour ago




@神秘德里克 what do you mean processes don't get mapped into physical memory? when i said that i obviously meant pages of that virtual memory space belonging to that process get mapped into frames of physical memory.
– John P
1 hour ago










1 Answer
1






active

oldest

votes

















up vote
5
down vote



accepted










  1. printf is implemented by the C library, it’s not part of the kernel. (The kernel does have its own equivalent, more or less, but that’s not available to user processes.) So a user process calling printf doesn’t call into the kernel immediately. printf ends up calling write, which is handled by the kernel (well, there’s a small wrapper in the C library, but it’s minimal); the process invokes the corresponding system call, and control switches to the kernel, but still within the context of the same process.


  2. Code pages from executables or libraries are only loaded once into memory (for the same version of the underlying file), so yes, printf maps to the same physical address, if it’s provided by the same library.


  3. The kernel mappings are only accessible from kernel code.






share|improve this answer






















  • Thanks for answer, So regarding question 1 : at which point we jump into kernel section of our process virtual space? when we call write or only when we do a system call?
    – John P
    1 hour ago










  • Generally, the process calls write, which is a wrapper in the C library; that sets everything up as appropriate, and invokes the system call. The switch to the kernel happens as a result of the system call (that’s why it’s a system call).
    – Stephen Kitt
    1 hour ago











  • thanks, also regarding the thirds answer, i updated the picture the previous one was not complete. ( i also made another thread for follow up questions on this answer, didn't want to spam in comments)
    – John P
    40 mins ago










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f473796%2fwhat-exactly-happens-in-virtual-memory-when-i-call-a-function-like-printf-in-lin%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
5
down vote



accepted










  1. printf is implemented by the C library, it’s not part of the kernel. (The kernel does have its own equivalent, more or less, but that’s not available to user processes.) So a user process calling printf doesn’t call into the kernel immediately. printf ends up calling write, which is handled by the kernel (well, there’s a small wrapper in the C library, but it’s minimal); the process invokes the corresponding system call, and control switches to the kernel, but still within the context of the same process.


  2. Code pages from executables or libraries are only loaded once into memory (for the same version of the underlying file), so yes, printf maps to the same physical address, if it’s provided by the same library.


  3. The kernel mappings are only accessible from kernel code.






share|improve this answer






















  • Thanks for answer, So regarding question 1 : at which point we jump into kernel section of our process virtual space? when we call write or only when we do a system call?
    – John P
    1 hour ago










  • Generally, the process calls write, which is a wrapper in the C library; that sets everything up as appropriate, and invokes the system call. The switch to the kernel happens as a result of the system call (that’s why it’s a system call).
    – Stephen Kitt
    1 hour ago











  • thanks, also regarding the thirds answer, i updated the picture the previous one was not complete. ( i also made another thread for follow up questions on this answer, didn't want to spam in comments)
    – John P
    40 mins ago














up vote
5
down vote



accepted










  1. printf is implemented by the C library, it’s not part of the kernel. (The kernel does have its own equivalent, more or less, but that’s not available to user processes.) So a user process calling printf doesn’t call into the kernel immediately. printf ends up calling write, which is handled by the kernel (well, there’s a small wrapper in the C library, but it’s minimal); the process invokes the corresponding system call, and control switches to the kernel, but still within the context of the same process.


  2. Code pages from executables or libraries are only loaded once into memory (for the same version of the underlying file), so yes, printf maps to the same physical address, if it’s provided by the same library.


  3. The kernel mappings are only accessible from kernel code.






share|improve this answer






















  • Thanks for answer, So regarding question 1 : at which point we jump into kernel section of our process virtual space? when we call write or only when we do a system call?
    – John P
    1 hour ago










  • Generally, the process calls write, which is a wrapper in the C library; that sets everything up as appropriate, and invokes the system call. The switch to the kernel happens as a result of the system call (that’s why it’s a system call).
    – Stephen Kitt
    1 hour ago











  • thanks, also regarding the thirds answer, i updated the picture the previous one was not complete. ( i also made another thread for follow up questions on this answer, didn't want to spam in comments)
    – John P
    40 mins ago












up vote
5
down vote



accepted







up vote
5
down vote



accepted






  1. printf is implemented by the C library, it’s not part of the kernel. (The kernel does have its own equivalent, more or less, but that’s not available to user processes.) So a user process calling printf doesn’t call into the kernel immediately. printf ends up calling write, which is handled by the kernel (well, there’s a small wrapper in the C library, but it’s minimal); the process invokes the corresponding system call, and control switches to the kernel, but still within the context of the same process.


  2. Code pages from executables or libraries are only loaded once into memory (for the same version of the underlying file), so yes, printf maps to the same physical address, if it’s provided by the same library.


  3. The kernel mappings are only accessible from kernel code.






share|improve this answer














  1. printf is implemented by the C library, it’s not part of the kernel. (The kernel does have its own equivalent, more or less, but that’s not available to user processes.) So a user process calling printf doesn’t call into the kernel immediately. printf ends up calling write, which is handled by the kernel (well, there’s a small wrapper in the C library, but it’s minimal); the process invokes the corresponding system call, and control switches to the kernel, but still within the context of the same process.


  2. Code pages from executables or libraries are only loaded once into memory (for the same version of the underlying file), so yes, printf maps to the same physical address, if it’s provided by the same library.


  3. The kernel mappings are only accessible from kernel code.







share|improve this answer














share|improve this answer



share|improve this answer








edited 28 mins ago

























answered 1 hour ago









Stephen Kitt

149k23330396




149k23330396











  • Thanks for answer, So regarding question 1 : at which point we jump into kernel section of our process virtual space? when we call write or only when we do a system call?
    – John P
    1 hour ago










  • Generally, the process calls write, which is a wrapper in the C library; that sets everything up as appropriate, and invokes the system call. The switch to the kernel happens as a result of the system call (that’s why it’s a system call).
    – Stephen Kitt
    1 hour ago











  • thanks, also regarding the thirds answer, i updated the picture the previous one was not complete. ( i also made another thread for follow up questions on this answer, didn't want to spam in comments)
    – John P
    40 mins ago
















  • Thanks for answer, So regarding question 1 : at which point we jump into kernel section of our process virtual space? when we call write or only when we do a system call?
    – John P
    1 hour ago










  • Generally, the process calls write, which is a wrapper in the C library; that sets everything up as appropriate, and invokes the system call. The switch to the kernel happens as a result of the system call (that’s why it’s a system call).
    – Stephen Kitt
    1 hour ago











  • thanks, also regarding the thirds answer, i updated the picture the previous one was not complete. ( i also made another thread for follow up questions on this answer, didn't want to spam in comments)
    – John P
    40 mins ago















Thanks for answer, So regarding question 1 : at which point we jump into kernel section of our process virtual space? when we call write or only when we do a system call?
– John P
1 hour ago




Thanks for answer, So regarding question 1 : at which point we jump into kernel section of our process virtual space? when we call write or only when we do a system call?
– John P
1 hour ago












Generally, the process calls write, which is a wrapper in the C library; that sets everything up as appropriate, and invokes the system call. The switch to the kernel happens as a result of the system call (that’s why it’s a system call).
– Stephen Kitt
1 hour ago





Generally, the process calls write, which is a wrapper in the C library; that sets everything up as appropriate, and invokes the system call. The switch to the kernel happens as a result of the system call (that’s why it’s a system call).
– Stephen Kitt
1 hour ago













thanks, also regarding the thirds answer, i updated the picture the previous one was not complete. ( i also made another thread for follow up questions on this answer, didn't want to spam in comments)
– John P
40 mins ago




thanks, also regarding the thirds answer, i updated the picture the previous one was not complete. ( i also made another thread for follow up questions on this answer, didn't want to spam in comments)
– John P
40 mins ago

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f473796%2fwhat-exactly-happens-in-virtual-memory-when-i-call-a-function-like-printf-in-lin%23new-answer', 'question_page');

);

Post as a guest













































































Comments

Popular posts from this blog

What does second last employer means? [closed]

Installing NextGIS Connect into QGIS 3?

One-line joke