LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1204|回复: 0

[翻译] 计算机如何同时处理多个任务?

[复制链接]
发表于 2005-10-21 20:57:51 | 显示全部楼层 |阅读模式
计算机如何同时处理多个任务?
此篇文章是 UNIX and Internet Fundamentals HOWTO 的一个选段,我觉得很重要。但是由于这份 HOWTO 也只是简单的介绍一下原理,读者如果想仔细深入下去的话,我建议你参阅一下专业书籍。
事实上并非如此。计算机在同一时刻只能处理一件事物(或者说是进程)。但是计算机 能非常迅速的切换多个任务,这样反应缓慢的人类(fool slow human)就以为计算机 能够同时处理多个事务。这样被称做“时间共享”(timesharing)。

如何去管理“时间共享”是内核的任务之一。有一个称做时间表的东西保存了在你空间里的 其他非内核进程的进程表。每隔 1/60 秒的时间,有一个时间处理机告诉内核创造 一个时间片。进程表停止当前正在运行的进程,将它置入一个合适的空间里,并且着手控制 其他的进程。

1/60 秒听起来不是非常长的时间。但是对于今天的微处理器来说它有足够的时间去处理上 千条的指令,这样就可以处理大量的工作。所以如果你有很多的进程,这样就可以非常完美 地在在自己地时间片上切换运行。

但是在实际中,一个程序并不能获得它全部的时间片。当 I/O 设备上有中断输入时,内核马 上停止处理当前的任务,转向处理当前的输入,然后返回运行当前的任务。一个高优限权的中断 能“挤出”当前运行的普通进程。这样“不礼貌的行为”被称做抢占式多任务,这个特性 在许多 UNIX 系统中存在。

事实上,一个运行着的程序对于它们的时间片只受非常小的限制(但是有一个非常小的例外, 比如一些声音或者 3D 图象程序)。但是尽管如此,一个程序经常因为等待磁盘设备的传输或者 网络链接而经常的延迟。

一个操作系统处理同时发生的进程称做“多任务”。UNIX 类的系统从头一开始就被设计为多任务 的系统,并且非常的优秀:甚至比 Windows 系统和老的 Mac OS 系统要好(即使它们也称做“多 任务”的系统,但是相比之下确实不如 UNIX)。同道理,继承了 UNIX 系统特性的 Linux 在多 任务处理方面占据了大量的分量,使其在网络链接、 Web 服务方面性能优异。


--------------------------------------------------------------------------------

How does my computer do several things at once?
It doesn't, actually. Computers can only do one task (or process) at a time. But a computer can change tasks very rapidly, and fool slow human beings into thinking it's doing several things at once. This is called timesharing.

One of the kernel's jobs is to manage timesharing. It has a part called the scheduler which keeps information inside itself about all the other (non-kernel) processes in your zoo. Every 1/60th of a second, a timer goes off in the kernel, generating a clock interrupt. The scheduler stops whatever process is currently running, suspends it in place, and hands control to another process.

1/60th of a second may not sound like a lot of time. But on today's microprocessors it's enough to run tens of thousands of machine instructions, which can do a great deal of work. So even if you have many processes, each one can accomplish quite a bit in each of its timeslices.

In practice, a program may not get its entire timeslice. If an interrupt comes in from an I/O device, the kernel effectively stops the current task, runs the interrupt handler, and then returns to the current task. A storm of high-priority interrupts can squeeze out normal processing; this misbehavior is called thrashing and is fortunately very hard to induce under modern Unixes.

In fact, the speed of programs is only very seldom limited by the amount of machine time they can get (there are a few exceptions to this rule, such as sound or 3-D graphics generation). Much more often, delays are caused when the program has to wait on data from a disk drive or network connection.


An operating system that can routinely support many simultaneous processes is called "multitasking". The Unix family of operating systems was designed from the ground up for multitasking and is very good at it - much more effective than Windows or the old Mac OS, which had multitasking bolted into them as an afterthought and do it rather poorly. Efficient, reliable multitasking is a large part of what makes Linux superior for networking, communications, and Web service.
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表