简体   繁体   English

系统调用:Read 函数是否改变过程?

[英]System call: does Read function change process?

enter image description here在此处输入图像描述

I learned that when a system call function is called, the process changes.我了解到,当调用系统调用函数时,流程会发生变化。 But what is process B if I call the read function without the fork() function?但是,如果我在没有 fork() 函数的情况下调用 read 函数,那么进程 B 是什么? isn't there is only one process?不是只有一个过程吗?

On x86-64, there is one specific instruction to do system calls: syscall ( https://www.felixcloutier.com/x86/syscall.html ).在 x86-64 上,有一个执行系统调用的特定指令: syscall ( https://www.felixcloutier.com/x86/syscall.html )。 When you call read() in C, it is compiled to placing the proper syscall number in a register along with the arguments you provide and to one syscall instruction.当您在 C 中调用read()时,它会被编译为将正确的系统调用号连同您提供的参数和一个syscall指令一起放入寄存器中。 When syscall is executed, it jumps to the address stored in the IA32_LSTAR register.执行syscall时,它会跳转到存储在 IA32_LSTAR 寄存器中的地址。 After that, it is in kernel mode executing the kernel's syscall handler.之后,它在内核模式下执行内核的系统调用处理程序。

At that point, it is still in the context of process A. Within its handler, the kernel realizes that you want to read from disk.此时,它仍在进程 A 的上下文中。在其处理程序中,内核意识到您要从磁盘读取。 It will thus start a DMA operation by writing some registers of the hard-disk controller.因此,它将通过写入硬盘控制器的一些寄存器来启动 DMA 操作。 From there, process A is waiting for IO.从那里,进程 A 正在等待 IO。 There is no point in leaving the core idle so the kernel calls the scheduler and it will probably decide to switch the context of the core to another process B.让核心空闲没有意义,因此内核调用调度程序,它可能会决定将核心的上下文切换到另一个进程 B。

When the DMA IO operation is done, the hard-disk controller triggers an interrupt.当 DMA IO 操作完成时,硬盘控制器触发中断。 The kernel thus puts process A back into the ready queue and calls the scheduler which will probably have the effect of switching the context of the core back to process A.内核因此将进程 A 放回就绪队列并调用调度程序,这可能会将内核的上下文切换回进程 A。

The image you provide isn't very clear so I can understand the confusion.您提供的图像不是很清楚,所以我可以理解混乱。 Overall, on most architectures it will work similarly to what is stated above.总的来说,在大多数架构上,它的工作方式与上述类似。

The image is somewhat misleading.该图像有些误导。 What actually happens is, the read system call needs to wait for IO.实际发生的是, read系统调用需要等待 IO。 There is nothing else that can be done in the context of process (or thread) A.在进程(或线程)A 的上下文中没有其他可以做的事情。

So kernel needs to find something else for the CPU to do.所以内核需要为 CPU 寻找其他的东西来做。 Usually there is some other process or processes which do have something to do (not waiting for a system call to return).通常有一些其他进程或进程确实有事情要做(不等待系统调用返回)。 It could also be another thread of process A that is given time to execute (from kernel point of view, thread and process aren't really much different, actually).它也可能是进程 A 的另一个线程,它有时间执行(从内核的角度来看,线程和进程实际上并没有太大的不同)。 There may be several processes which get to execute while process A waits for system call to complete, too.当进程 A 也等待系统调用完成时,可能有几个进程要执行。

And if there is nothing else for any other process and thread to do, then kernel will just be idle, let the CPU sleep for a bit, basically save power (especially important on a laptop).如果没有其他任何进程和线程可以做,那么内核就会处于空闲状态,让 CPU 休眠一会儿,基本上可以省电(在笔记本电脑上尤其重要)。

So the image in the question shows just one possible situation.因此,问题中的图像仅显示了一种可能的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM