What exactly happens in virtual memory when i call a function like printf in Linux?
Clash 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 :
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?
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?
overall what are the situations that non kernel processes use the kernel part of the virtual memory?
kernel memory virtual-memory
add a comment |Â
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 :
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?
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?
overall what are the situations that non kernel processes use the kernel part of the virtual memory?
kernel memory virtual-memory
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
add a comment |Â
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 :
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?
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?
overall what are the situations that non kernel processes use the kernel part of the virtual memory?
kernel memory virtual-memory
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 :
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?
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?
overall what are the situations that non kernel processes use the kernel part of the virtual memory?
kernel memory virtual-memory
kernel memory virtual-memory
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
add a comment |Â
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
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
5
down vote
accepted
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 callingprintf
doesnâÂÂt call into the kernel immediately.printf
ends up callingwrite
, 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.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.The kernel mappings are only accessible from kernel code.
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 callswrite
, 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
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
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 callingprintf
doesnâÂÂt call into the kernel immediately.printf
ends up callingwrite
, 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.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.The kernel mappings are only accessible from kernel code.
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 callswrite
, 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
add a comment |Â
up vote
5
down vote
accepted
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 callingprintf
doesnâÂÂt call into the kernel immediately.printf
ends up callingwrite
, 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.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.The kernel mappings are only accessible from kernel code.
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 callswrite
, 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
add a comment |Â
up vote
5
down vote
accepted
up vote
5
down vote
accepted
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 callingprintf
doesnâÂÂt call into the kernel immediately.printf
ends up callingwrite
, 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.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.The kernel mappings are only accessible from kernel code.
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 callingprintf
doesnâÂÂt call into the kernel immediately.printf
ends up callingwrite
, 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.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.The kernel mappings are only accessible from kernel code.
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 callswrite
, 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
add a comment |Â
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 callswrite
, 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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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