企鹅股票网

dma指标:DMA控制器与通道是什么关系,通道到底是什么,感觉不太理解?作者:axiqia

发布时间:2020-02-11 11:57   来源:网络 点击 :

在比较DMA和Channel I/O之前,我们不妨回顾一下IO数据传输的四种方式,循环IO测试、程序中断IO、DMA方式和通道方式。

循环IO测试(Programmed I/O):计算机的IO测试指令通过轮询的方式,检测IO设备的忙/闲标志,决定主存和外设之间是或否传出一个字或者一个字符。在这种情况下,CPU的大量时间在等待输入、输出的循环检测上,使计算机不能充分发挥效率,外设也得不到合理的使用,整个系统效率低下。

举个例子:Consider a user process that wants to print the eight-character string "ABCDEFGH" on the printer.

It first assembles the string in a buffer in user space,as shown in Fig. 5-7(a).

The operating system then (usually) copies the buffer with the string to an array, say, p, in kernel space, where it is more easily accessed (because the kernel may have to change the memory map to get at user space). It then checks to see if the printer is currently available. If not, it waits until it is available. As soon as the printer is available, the operating system copies the first character to the printer's data register, in this example using memory-mapped 110. This action activates the printer. The character may not appear yet because some printers buffer a line or a page before printing anything.In Fig. 5-7(b), however, we see that the first character has been printed and that the system has marked the "B" as the next character to be printed.

As soon as it has copied the first character to the printer, the operating system checks to see if the printer is ready to accept another one. Generally, the printer ,has a second register, which gives its status. The act of writing to the data register causes the status to become not ready. When the printer controller has processed the current character, it indicates its availability by setting some bit in its status register or putting some value in it.

At this point the operating system waits for the printer to become ready again. When that happens, it prints the next character, as shown in Fig. 5-7(c). This loop continues until the entire string has been printed. Then control returns to the user process.

The actions followed by the operating system are summarized in Fig. 5-8.

First the data are copied to the kernel. Then the operating system enters a tight loop outputting the characters one at a time. The essential aspect of programmed I/O, clearly illustrated in this figure, is that after outputting a character, the CPU continuously polls the device to see if it is ready to accept another one. This behavior is often called polling or busy waiting.

程序中断(Interrupt-Driven I/O):IO设备的控制器逐个比特的从设备中读取一块数据放入设备的内部缓冲区中,然后,计算该块数据的校验和,以保证读取的正确性,接着,设备控制器发出中断信号,操作系统开始逐个字节地从缓冲区中数据读入内存。中断机制的引入,使得外围设备有了反映自身状态的能力,仅当IO操作正常或者异常结束时才中断CPU,从而实现了一定程度的并行。但是,IO操作毕竟是由CPU控制的,此时每传输一个字或字符,往往就要中断一次。中断也需要消耗时间,因此这种模式也在一定程度上浪费了CPU时间。当IO设备很多时,CPU可能完全陷入处理IO中断中。

继续上一个栗子:

When the system call to print the string is made, the buffer is copied to kernel space, as we showed earlier, and the first character is copied to the printer as soon as it is willing to accept a character. At that point the CPU calls the scheduler and some other process is run. The process that asked for the string to be printed is blocked until the entire string has printed.The work done on the system call is shown in Fig. 5-9(a).

When the printer has printed the character and is prepared to accept the next one, it generates an interrupt. This interrupt stops the current process and saves its state. Then the printer interrupt service procedure is run. A crude version of this code is shown in Fig. 5-9(b). If there are no more characters to print, the interrupt handler takes some action to unblock the user. Otherwise, it outputs the next character, acknowledges the interrupt, and returns to the process that was running just before the interrupt, which continues from where it left off.

DMA方式:在内存和IO设备之间直接进行数据交换,不需要CPU的干预。当需要IO数据传输时,CPU将DMA初始化,之后DMA接管总线的使用权,将所需要的数据全部读入内存后,IO设备的控制器才会发出中断。本质上讲, DMA也是Programmed I/O,只是DMA controller替代了CPU的工作。

DMA的好处在于将CPU中断从每个字符中断减少为每个缓冲区中断,从而如果输出字符很多时,能带来可观的性能提升。另一方面,DMA controller往往比CPU慢很多,如果DMA controller不能全速驱动输出设备或者当等待DMA时CPU经常无其他任务可做,那么Interrupt-Driven I/O或Programmed I/O可能更好。但是,大多数情况下,DMA都是更好的。

对于以上三种方式:

都需要CPU介入:

  • 循环IO测试:完全介入
  • 程序中断:需要CPU介入,但在数据读入IO设备的缓冲区,发出中断前,CPU可以做其他事务
  • DMA:在传输开始(DMA初始化)和传输结束(中断)时介入

都需要硬件接口支持:

  • 循环IO测试:最简单的硬件
  • 程序中断:增加中断控制器
  • DMA:增加DMA

因此,这三种方式适用于外设不太多的小型机、微型机。对于IO设备数量多,输入输出频繁的系统,占用CPU时间太多,接口硬件太复杂,成本也高。

那么,什么是通道呢?

通道:能执行有限通道指令的IO控制器,代替CPU管理控制外设。通道有自己的指令系统,是一个协处理器,一般用在大型计算机系统中(不是大型机)。通道实质是一台能够执行有限的输入输出指令,并能被多台外设共享的小型DMA专用处理机。广义上讲,DMA也属于通道。



通道解决了两个问题:

  1. 由CPU承担输入输出的工作。虽然DMA无需CPU进行外设与内存的数据交换工作,但是这只是减少了CPU的负担。因而DMA中,输入输出的初始化仍然要由CPU来完成。
  2. 大型计算机系统中高速设备共享DMA接口的问题。大型计算机系统的外设太多以至于不得不共享有限的DMA接口(小型计算机系统比如pc机中每个高速设备分配一个DMA接口)。


最后回到问题本身,通道与DMA相比:

共同点:

都能实现IO设备和内存之间建立数据直传通路;

不同点:

  1. DMA只能实现固定的数据传送控制,而通道有自己的指令和程序,具有更强的独立处理数据输入和输出的能力。
  2. DMA只能控制一台或者少数几台同类设备,而一个通道可以控制多台同类或者不同的设备。

Reference:Modern Operating Systems : Andrew S. Tanenbaum‎, Third Edition

更多回复:
猜你喜欢