本来不应该这样,但事实使我惊讶的是,我们对网络的依靠有多强大。甚至我发现自己除了使用Web浏览器外没有使用其他任何东西。如此强调基于Web的服务,您可能会忘记欣赏不起眼的操作系统。
就是说,我们忽视了我们的操作系统所面临的风险,即根本无法充分利用它使我们的设备成为现实的令人难以置信的工具。 我们大多数人只接触两个操作系统家族中的一个或两个,即“ Windows 房屋”和“几乎所有其他房屋”。后者通常被称为Unix。
Windows在可用性和安全性上已经迈出了很大一步,但对我而言,它永远无法接近Unix及其后代。尽管已有50多年的历史了,但Unix具有简单,优雅和多功能性,这是其他任何OS都无法比拟的。
本专栏是我个人认为最重要的Unix元素的注释。计算机科学的医生将承认封装Unix的独特之处的巨大困难。因此,据我所知较少的,我当然无法接近。不过,我的希望是表达对Unix的钦佩之情可能会激发您的灵感。
家谱的根源
如果您还没有听说过Unix,那是因为它的后代与它的相似之处并不完全相同,而且绝对不会共享一个名称。 MacOS是一个遥不可及的分支机构,尽管可以说它至少像它的前辈一样,但仍体现了足够的基本Unix特性以追溯明确的血统。
BSD的三个主要分支,尤其是FreeBSD,拥有最接近Unix的公式,并继续构成一些世界上最重要的计算系统的骨干。世界上很大一部分服务器,计算机化的军事硬件和PlayStation控制台都是底层的BSD。
最后是Linux。尽管Linux并没有像BSD那样完全继承Unix,但Linux是最丰富和可见的Unix火炬手。世界上有很多(如果不是绝对的话)服务器是Linux。最重要的是,几乎所有嵌入式设备都运行Linux,包括Android移动设备。
这个必不可少的操作系统从何而来?
为了提供尽可能简明的历史课程,Unix是由1970年在贝尔实验室的计算机科学界最精明的人才汇聚而成的。在他们的任务中,他们为自己设定了简单的目标。首先,他们希望OS能够在可以找到的任何硬件上平稳运行,因为具有讽刺意味的是,他们很难在Bell上找到任何可以使用的计算机。他们还希望自己的操作系统允许多个用户同时登录并运行程序,而不会互相碰撞。最后,他们希望操作系统易于管理且直观地组织起来。在从邻近的部门那里获得了多余的设备后,该团队最终创建了Unix。
大学计算机科学系出于研究目的最初并大力采用Unix。伊利诺伊大学香槟分校和加州大学伯克利分校领导了这项工作,后者甚至发展了自己的Unix品牌,称为伯克利软件发行公司(BSD)。
最终,贝尔的继任者AT&T对Unix失去了兴趣,并在90年代初放弃了它。在此之后不久,BSD变得越来越流行,AT&T意识到它犯了一个严重的错误。在可能仍是有史以来最旷日持久,最积极的技术行业法律斗争之后,BSD开发人员获得了Unix事实上主线的唯一监护权。从那时起,BSD一直是Unix的资深政治家,并捍卫着Unix最纯净,广泛存在的迭代之一。
组织结构
我对Unix的概念及其伴随的总体计算方法就是所谓的“ Unix方式”。它是Unix结构与Unix哲学的交集。
首先从等式的结构方面入手,让我们考虑文件系统。设计是一棵树,每个文件都从根开始并从那里分支。只是“树”是倒置的,其根在顶部。每个文件都具有与“ / ”的正确关系(正斜杠表示为“ root”)。整个系统包含在此处的目录中。在每个目录中,您可以拥有几乎无限数量的文件或其他目录,每个目录或文件可以具有无限数量的自己的文件和目录,依此类推。
更重要的是,根目录下的每个目录都有特定的用途。我之前在文件系统层次结构标准的一篇文章中对此进行了介绍,因此在这里我将不再赘述。但是举几个说明性的例子,/ boot目录存储了系统启动所需的一切。 / bin,/ sbin和/ usr目录保留所有系统二进制文件(运行程序的东西)。可以更改/ etc中的系统拥有程序的工作方式的配置文件。您的所有个人文件(例如文档和媒体)都位于/ home(更准确地说,位于/ home中用户帐户的目录中)。始终更改的数据类型(即日志)记录在/ var下。
这样,Unix确实秉承了古老的格言:“一个万物之地,万物万物。”这就是为什么很容易找到想要的东西的原因。在大多数情况下,只需选择一个名称似乎是文件最合适位置的目录,就可以一次跟随一个树到达您所需要的目录。如果那行不通,您可以运行“ find”之类的命令来精确查找所需的内容。这种组织方案还使混乱降至最低。错位的事物会脱颖而出,此时可以移动或删除它们。
一切都是文件
使实用程序更加优雅的另一个约定是,Unix中的所有内容都是文件。 Unix并没有为硬件和过程之类的东西创建另一个独特的数字结构,而是将所有这些都视为文件。它们可能并非全部都是我们通常理解的文件,但它们在计算机科学中是位组的文件。
这种统一性意味着您可以自由使用各种工具来处理系统上任何需要它的东西。文档和媒体文件是文件。听起来很明显,这意味着将它们视为可以被其他程序引用的单个对象,无论是根据其内容格式,元数据还是原始位组成。
设备也是Unix中的文件。无论您将什么硬件连接到系统,都将其分类为块设备或流设备。用户几乎永远不会以文件格式弄乱这些设备,但是计算机需要一种对这些设备进行分类的方法,以便它知道如何与它们交互。在大多数情况下,系统调用某些程序将设备“文件”转换为立即可用的形式。
块设备代表数据块。虽然不能将块设备整体上视为“文件”,但系统可以通过请求块号来读取块设备的段。另一方面,流设备是表示信息流的“文件”,意味着某些过程不断创建或发送的位。键盘是一个很好的例子:键盘被按下时发送数据流。
甚至进程都是文件。您运行的每个程序都将产生一个或多个持续存在的进程,只要该进程可以执行即可。进程会定期启动其他进程,但可以通过它们的唯一进程ID(PID)进行跟踪,并由拥有它们的用户进行分组。通过将进程分类为文件,可以轻松地查找和处理它们。这就是使优先考虑自私的过程或杀死不守规矩的过程的原因。
要深入了解的话,您可以通过运行“ lsof ”命令来见证将所有内容解释为文件的强大功能。 “ lsof ” 是“列出打开的文件”的缩写,它枚举了当前正在使用的符合特定条件的所有文件。示例条件包括文件是否使用系统网络连接,或由哪个进程拥有它们。
一个正在阶段中的思想流派
考虑到编写OS的艰巨任务,大多数OS开发人员通过定义支持它的理念来专注于他们的工作。没有哪一种象Unix哲学那样具有标志性和影响力。它的影响已经超越了Unix,激发了几代计算机科学家和程序员的灵感。
Unix哲学有多种表述,因此,我将概述其核心原则。 在Unix中,每个工具都应该做一件事,但是要做好。这听起来很直观,但是没有(仍旧没有)以这种方式设计的足够的程序。该规范在实践中的含义是,应仅构建每种工具来解决一小部分计算任务,但也应以易于使用且可配置的方式来做到这一点,以适应有关该计算切片的用户偏好。 一旦按照这些理念构建了一些工具,用户就应该能够结合使用它们来完成很多工作(在几秒钟内就可以完成更多工作)。 “经典” Unix命令实际上可以执行基本有用的计算机应具备的所有功能。
只需几十种工具,用户就可以:
-管理流程
-不论文件类型如何,操作文件及其内容
-配置硬件和网络设备
-管理已安装的软件
-将代码编写并编译为有效的二进制文件
Unix哲学的另一个中心思想是,工具不应对用户如何使用其输出或结果承担或施加期望。这个概念似乎很抽象,但是旨在实现确保工具可以链接在一起的非常务实的好处。这只会放大强大的基本Unix工具集的功能。
实际上,这允许一个命令的输出成为另一命令的输入。还记得我说过的一切都是文件吗?程序输出也不例外。因此,通常需要文件的任何命令都可以替换为前一个命令输出的“文件”。
最后,为了突出Unix鲜为人知的方面,它赋予了文本处理和操作特权。原因很简单:文本是人类所理解的。因此,这就是我们希望提供的计算结果。
从根本上讲,所有计算机真正要做的就是将某些文本转换为不同的文本(通过二进制方式,以便可以理解文本)。因此,Unix工具应允许用户毫无问题地编辑,替换,格式化和重定向文本。同时,Unix文本工具决不能拒绝用户粒度控制。
在观察上述教条时,将文本操作划分为单独的工具。这些包括“ awk”,“ sed”,“ grep”,“ sort”,“ tr”,“ uniq”之类的名称,以及许多其他名称。在这里,每个人也都有自己厉害的地方,但在协作方面却更为强大。
真正的力量来自内在
不管您如何发现它们是多么迷人,这些建筑和意识形态上的区别如果看起来很深奥,这是可以理解的。但是,无论您使用的计算机是否符合这些理想,设计计算机的OS和应用程序的人员肯定都认为非常符合。这些开发人员以及之前的先驱使用Unix强大的工具来制作您每天享受的计算体验。
这些工具也不会降级到硅谷的某些数字工作台。所有这些都在那里-随时随地坐在您的系统上-并且使用它们的机会可能比您想象的要多。您可能希望计算机解决的大多数问题都不是新问题,因此通常已经有老工具解决了。如果您发现自己在计算机上执行重复性任务,则可能有一个工具可以为您完成此任务,这可能归功于Unix。
在撰写有关技术的文章时,我介绍了其中一些工具,并且我可能会再介绍更多。在那之前,如果您发现“ Unix之路”像我一样引人注目,那么我鼓励您自己了解它。我向您保证,互联网不乏它。那就是我得到的。
原文:It probably shouldn't, but it routinely astonishes me how much we live on the Web. Even I find myself going entire boots without using anything but the Web browser. With such an emphasis on Web-based services, one can forget to appreciate the humble operating system.
That said, we neglect our OS at the risk of radically underutilizing the incredible tools that it enables our device to be.
Most of us only come into contact with one, or possibly both, of two families of operating systems: "House Windows" and "House Practically Everything Else." The latter is more commonly known as Unix.
Windows has made great strides in usability and security, but to me it can never come close to Unix and its progeny. Though more than 50 years old, Unix has a simplicity, elegance, and versatility that is unrivalled in any other breed of OS.
This column is my exegesis of the Unix elements I personally find most significant. Doctors of computer science will concede the immense difficulty of encapsulating just what makes Unix special. So I, as decidedly less learned, will certainly not be able to come close. My hope, though, is that expressing my admiration for Unix might spark your own.
The Root of the Family Tree
If you haven't heard of Unix, that's only because its descendants don't all have the same resemblance to it -- and definitely don't share a name. MacOS is a distant offshoot which, while arguably the least like its forebears, still embodies enough rudimentary Unix traits to trace a clear lineage.
The three main branches of BSD, notably FreeBSD, have hewn the closest to the Unix formula, and continue to form the backbone of some of the world's most important computing systems. A good chunk of the world's servers, computerized military hardware, and PlayStation consoles are all some type of BSD under the hood.
Finally, there's Linux. While it hasn't preserved its Unix heritage as purely as BSD, Linux is the most prolific and visible Unix torchbearer. A plurality, if not outright majority, of the world's servers are Linux. On top of that, almost all embedded devices run Linux, including Android mobile devices.
Where Did This Indispensable OS Come From?
To give as condensed a history lesson as possible, Unix was created by an assemblage of the finest minds in computer science at Bell Labs in 1970. In their task, they set themselves simple objectives. First, they wanted an OS that could smoothly run on whatever hardware they could find since, ironically, they had a hard time finding any computers to work with at Bell. They also wanted their OS to allow multiple users to log in and run programs concurrently without bumping into each other. Finally, they wanted the OS to be simple to administer and intuitively organized. After acquiring devices from the neighboring department, which had a surplus, the team eventually created Unix.
Unix was adopted initially, and vigorously so, by university computer science departments for research purposes. The University of Illinois at Champaign-Urbana and the University of California Berkeley led the charge, with the latter going so far as to develop its own brand of Unix called the Berkeley Software Distribution, or BSD.
Eventually, AT&T, Bell's successor, lost interest in Unix and jettisoned it in the early 90s. Shortly following this, BSD grew in popularity, and AT&T realized what a grave mistake it had made. After what is probably still the most protracted and aggressive tech industry legal battle of all time, the BSD developers won sole custody of the de facto main line of Unix. BSD has been Unix's elder statesmen ever since, and guards one of the purest living, widely available iterations of Unix.
Organizational Structure
My conception of Unix and its accompanying overall approach to computing is what I call the "Unix Way." It is the intersection of Unix structure and Unix philosophy.
To begin with the structural side of the equation, let's consider the filesystem. The design is a tree, with every file starting at the root and branching from there. It's just that the "tree" is inverted, with the root at the top. Every file has its proper relation to "/" (the forward slash notation called "root"). The whole of the system is contained in the directories found here. Within each directory, you can have a practically unlimited number of files or other directories, each of which can have an unlimited number of files and directories of its own, and so on.
More importantly, every directory under root has a specific purpose. I covered this a while back in a piece on the Filesystem Hierarchy Standard, so I won't rehash it all here. But to give a few illustrative examples, the /boot directory stores everything your system needs to boot up. The /bin, /sbin, and /usr directories retain all your system binaries (the things that run programs). Configuration files that can alter how system-owned programs work live in /etc. All your personal files such as documents and media go in /home (to be more accurate, in your user account's directory in /home). The kind of data that changes all the time, namely logs, gets filed under /var.
In this way, Unix really lives by the old adage "a place for everything, and everything in its place." This is exactly why it's very easy to find whatever you're looking for. Most of the time, you can follow the tree one directory at a time to get to exactly what you need, simply by picking the directory whose name seems like the most appropriate place for your file to be. If that doesn't work, you can run commands like 'find' to dig up exactly what you're looking for. This organizational scheme also keeps clutter to a minimum. Things that are out-of-place stand out, at which point they can be moved or deleted.
Everything Is a File
Another convention which lends utility through elegance is the fact that everything in Unix is a file. Instead of creating another distinct digital structure for things like hardware and processes, Unix thinks of all of these as files. They may not all be files as we commonly understand them, but they are files in the computer science sense of being groups of bits.
This uniformity means that you are free to use a variety of tools for dealing with anything on your system that needs it. Documents and media files are files. Obvious as that sounds, it means they are treated like individual objects that can be referred to by other programs, whether according to their content format, metadata, or raw bit makeup.
Devices are files in Unix, too. No matter what hardware you connect to your system, it gets classified as a block device or a stream device. Users almost never mess with these devices in their file form, but the computer needs a way of classifying these devices so it knows how to interact with them. In most cases, the system invokes some program for converting the device "file" into an immediately usable form.
Block devices represent blocks of data. While block devices aren't treated like "files" in their entirety, the system can read segments of the block device by requesting a block number. Stream devices, on the other hand, are "files" that present streams of information, meaning bits that are being created or sent constantly by some process. A good example is a keyboard: it sends a stream of data as keys are pressed.
Even processes are files. Every program that you run spawns one or more processes that persist as long as the program does. Processes regularly start other processes, but can all be tracked by their unique process ID (PID) and grouped by the user that owns them. By classifying processes as files, locating and manipulating them is straightforward. This is what makes reprioritizing selfish processes or killing unruly ones possible.
To stray a bit into the weeds, you can witness the power of construing everything as a file by running the 'lsof' command. Short for "list open files," 'lsof' enumerates all files currently in use which fit certain criteria. Example criteria include whether or not the files use system network connections, or which process owns them.
Virtues of Openness
The last element I want to point out (though certainly not the last that wins my admiration) is Unix's open computing standard. Most, if not all, of the leading Unix projects are open source, which means they are accessible. This has several key implications.
First, anyone can learn from it. In fact, Linux was born out of a desire to learn and experiment with Unix. Linus Torvalds wanted a copy of Minix to study and modify, but its developers did not want to hand out its source code. In response, Torvalds simply made his own Unix kernel, Linux. He later published the kernel on the Internet for anyone else who also wanted to play with Unix. Suffice it to say that there was some degree of interest in his work.
Second, Unix's openness means anyone can deploy it. If you have a project that requires a computer, Unix can power it; and being highly adaptable due to its architecture, this makes it great for practically any application, from tinkering to running a global business.
Third, anyone can extend it. Again, due to its open-source model, anyone can take a Unix OS and run with it. Users are free to fork their own versions, as happens routinely with Linux distributions. More commonly, users can easily build their own software that runs on any type of Unix system.
This portability is all the more valuable by virtue of Unix and its derivatives running on more hardware than any other OS type. Linux alone can run on essentially all desktop or laptop devices, essentially all embedded devices including mobile devices, all server devices, and even supercomputers.
So, I wouldn't say there's nothing Unix can't do, but you'd be hard-pressed to find it.
A School of Thought, and Class Is in Session
Considering the formidable undertaking that is writing an OS, most OS developers focus their work by defining a philosophy to underpin it. None has become so iconic and influential as the Unix philosophy. Its impact has reached beyond Unix to inspire generations of computer scientists and programmers.
There are multiple formulations of the Unix philosophy, so I will outline what I take as its core tenets.
In Unix, every tool should do one thing, but do that thing well. That sounds intuitive enough, but enough programs weren't (and still aren't) designed that way. What this precept means in practice is that each tool should be built to address only one narrow slice of computing tasks, but that it should also do so in a way that is simple to use and configurable enough to adapt to user preferences regarding that computing slice.
Once a few tools are built along these philosophical lines, users should be able to use them in combination to accomplish a lot (more on that in a sec). The "classic" Unix commands can do practically everything a fundamentally useful computer should be able to do.
With only a few dozen tools, users can:
- Manage processes
- Manipulate files and their contents irrespective of filetype
- Configure hardware and networking devices
- Manage installed software
- Write and compile code into working binaries
Another central teaching of Unix philosophy is that tools should not assume or impose expectations for how users will use their outputs or outcomes. This concept seems abstract, but is intended to achieve the very pragmatic benefit of ensuring that tools can be chained together. This only amplifies what the potent basic Unix toolset is capable of.
In actual practice, this allows the output of one command to be the input of another. Remember that I said that everything is a file? Program outputs are no exception. So, any command that would normally require a file can alternatively take the "file" that is the previous command's output.
Lastly, to highlight a lesser-known aspect of Unix, it privileges text handling and manipulation. The reason for this is simple enough: text is what humans understand. It is therefore what we want computational results delivered in.
Fundamentally, all computers truly do is transform some text into different text (by way of binary so that it can make sense of the text). Unix tools, then, should let users edit, substitute, format, and reorient text with no fuss whatsoever. At the same time, Unix text tools should never deny the user granular control.
In observing the foregoing dogmas, text manipulation is divided into separate tools. These include the likes of 'awk', 'sed', 'grep', 'sort', 'tr', 'uniq', and a host of others. Here, too, each is formidable on its own, but immensely powerful in concert.
True Power Comes From Within
Regardless of how fascinating you may find them, it is understandable if these architectural and ideological distinctions seem abstruse. But whether or not you use your computer in a way that is congruent with these ideals, the people who designed your computer's OS and applications definitely did. These developers, and the pioneers before them, used the mighty tools of Unix to craft the computing experience you enjoy every day.
Nor are these implements relegated to some digital workbench in Silicon Valley. All of them are there -- sitting on your system anytime you want to access them -- and you may have more occasion to use them than you think. The majority of problems you could want your computer to solve aren't new, so there are usually old tools that already solve them. If you find yourself performing a repetitive task on a computer, there is probably a tool that accomplishes this for you, and it probably owes its existence to Unix.
In my time writing about technology, I have covered some of these tools, and I will likely cover yet more in time. Until then, if you have found the "Unix Way" as compelling as I have, I encourage you to seek out knowledge of it for yourself. The Internet has no shortage of this, I assure you. That's where I got it.