3

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.

What is the step-by-step description in detail for what happens? Because I'm a little confused in this area, I have these questions:

  1. Are the instructions for the printf function inside of the 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 is there 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

closed as too broad by user88036, RalfFriedl, Isaac, Thomas, Shadur Oct 8 '18 at 12:47

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the 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. – 炸鱼薯条德里克 Oct 7 '18 at 16:13
  • 2
    @神秘德里克 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 Oct 7 '18 at 16:41
  • Process has tones of Pages, some may map to same physical address as other process(like the same file mmap), some may not, not so sure your question until I read the answer. – 炸鱼薯条德里克 Oct 8 '18 at 1:16
16
  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. If printf’s output gets written¹, that happens by 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 part of virtual memory is only accessible from kernel code.

¹ Strictly speaking, printf writes its output to a buffer, which might not be written anywhere.

  • 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 Oct 7 '18 at 16:46
  • 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 Oct 7 '18 at 17:03
  • 2
    printf may end-up calling write(). It adds some text to a stdio buffer which may eventually be written. – Stéphane Chazelas Oct 7 '18 at 18:54
  • 1
    @John I was referring to the part of memory reserved for the kernel. – Stephen Kitt Oct 8 '18 at 7:23
  • 1
    @John a system call doesn’t involve a normal process jumping into the kernel. A system call involves a normal process asking the CPU to switch to the kernel and run a call, identified by number, on its behalf. You don’t jump to an address in the kernel’s end of the address space (at least, not in Linux on x86); you invoke some other mechanism (on x86, INT 0x80, SYSENTER, or SYSCALL, depending on the specific ABI being used). See this article and its follow-up for details. – Stephen Kitt Oct 8 '18 at 7:41

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