(最终版)于ARM7的bootloader移植和设计_第1页
(最终版)于ARM7的bootloader移植和设计_第2页
(最终版)于ARM7的bootloader移植和设计_第3页
(最终版)于ARM7的bootloader移植和设计_第4页
(最终版)于ARM7的bootloader移植和设计_第5页
已阅读5页,还剩33页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、UNIX系统概述UNIX 环境高级编程第二版 1.1 简介所有的操作系统上运行他们的程序提供服务。典型的服务包括执行一个新程序,打开文件,读文件,分配的内存区域,得到了当前时间,等等。本文的重点是描述了各种版本的UNIX操作系统所提供的服务。描述在一个严格的线性方式没有任何UNIX系统的前向引用,但没有说明尚未而言,几乎是不可能的(而且很可能会无聊)。本章提供了一个从程序员的角度对UNIX系统的旋风式访问。我们会提供一些简要的描述术语和概念和实例,在整个文本中出现。我们描述更在后面的章节详细讨论这些功能。本章还提供了对UNIX系统提供的程序员这个新环境,服务的介绍和概述。1.2 UNIX架构从

2、严格意义上,操作系统可以被定义为软件的控制计算机的硬件资源,并提供一种环境下,程序可以运行。一般来说,我们称这种软件的核心,因为它是相对较小,在环境的核心所在。图1.1显示了UNIX系统的体系结构图。到内核接口是一个软件层称为系统调用(图1.1中的阴影部分)。常用函数库是建立在系统调用接口上,但应用程序可以自由使用这两种。 (我们谈论的系统调用和库函数1.11节更多。)外壳是一个特殊的应用程序,提供一个用于运行其他应用程序接口。从广义上讲,是一个操作系统内核和所有其他软件,使计算机有用,并给出了计算机它的个性。这个其他软件包括系统工具,应用程序,壳,常用的功能库,等等。举例来说,Linux是由

3、GNU操作系统所使用的内核。有些人指的GNU / Linux操作系统,不过它更普遍简称为Linux的。虽然这种用法可能不是严格意义上的正确,它是理解的,因为这句话的双重含义的操作系统。 (它还具有更加简洁的有利条件。)1.3 登陆登陆名字当我们到一个UNIX系统日志,我们进入我们的登录名,然后由我们的密码。然后,系统查找在我们的登录名的密码文件,通常是文件/ etc / passwd中。如果我们看看我们在密码文件中,我们看到它的七冒号分隔的字段组成项目:登录名,加密口令,数字用户ID(205),数字组ID(205),注释字段,主目录( /home/sar),和shell程序(/ bin / k

4、sh)。sar:x:205:105:Stephen Rago:/home/sar:/bin/ksh当代所有系统都提出了不同的加密的密码文件。在第6章中,我们将看看这些文件和某些函数来访问它们。Shell命令一旦我们登陆,一些系统信息通常会显示消息,然后我们可以给shell程序类型的命令。 (有些系统启动一个窗口管理程序,当您登录,但您通常最后一个壳的窗口之一运行起来。)shell是一个命令行解释器,它读取用户输入并执行命令。用户输入的shell通常是从终端(一种交互式的Shell)或有时从一个文件(称为一个shell脚本)。在使用普通炮弹在图1.2中概述。图1.2。在UNIX系统中经常使用的s

5、hell系统知道哪个shell来执行,我们从我们在密码文件中的条目最后一个字段。是Bourne shell,由史蒂夫在贝尔实验室开发的伯恩,一直以来第7版的使用,并与几乎所有的UNIX系统中存在提供。在控制流结构是陵68让人想起了Bourne shell。C shell中,由Bill Joy开发伯克利分校,是提供所有的BSD版本。此外,在C shell是由ATT公司提供与系统V/386版本3.2,并在系统V版本4(SVR4的)也。 (我们将有更多谈论的UNIX系统中的这些在下一章不同的版本。)的C shell是外壳上的第6版,而不是在Bourne shell内置。它的控制流看起来像C语言较多,

6、而且支持其他功能,并非由Bourne shell的规定:作业控制,历史的机制,和命令行编辑。在Korn shell是考虑到Bourne shell的继任者,并首次与SVR4的提供。在Korn shell,由贝尔实验室的David Korn开发,运行在大多数UNIX系统,但在此之前的SVR4通常是一种额外的成本附加,所以它不是像其他两发炮弹为普遍。它是与Bourne shell向上兼容,包括那些功能,使得C shell的流行:作业控制,命令行编辑,等等。Bourne shell是GNU Linux系统提供的。它被设计为POSIX的相容的,其余的仍与Bourne shell兼容。它支持了C she

7、ll和Korn shell的功能。在TENEX C shell的是C shell的增强版本。它借用,如命令完成从TENEX操作系统(在1972年开发的螺栓Beranek和Newman)等多种功能。在TENEX C shell中增加了许多功能的C壳,往往是作为C shell的替代品。Linux使用了其默认的shell的Bourne -再次外壳。事实上,/ bin / sh的是一个到/ bin / bash的链接。在FreeBSD和Mac OS X的默认用户shell是TENEX C shell的,但他们使用他们的行政Bourne shell的shell脚本,因为C shell的编程语言是非常困难

8、的使用。 Solaris中,有其在这两个BSD和System V的文物,提供了所有的炮弹,如图1.2所示。对大多数的炮弹自由港是在互联网上公布。整个文本,我们将使用像这样的括号注释来描述历史的笔记,并比较了UNIX系统的不同实现。往往是一个特定的实现技术的原因就变得非常明显的历史原因进行了描述。在整个文本,我们将展示交互式shell执行一个例子,我们开发的方案。这些示例使用功能常见的Bourne shell,Korn shell中,和Bourne -再次外壳。1.4 文件和目录1.4 文件和目录文件系统UNIX文件系统是分层排列的目录和文件。一切都开始在目录中称为根,其名称是单个字符/。目录是

9、一个文件,它包含目录条目。从逻辑上讲,我们可以认为每个目录项包含一个文件名作为一种描述文件的属性信息结构沿。一个文件的属性是作为fileregular文件类型这样的事情,directorythe大小的文件,该文件的所有者,对其他用户的权限可能filewhether访问该文件时fileand最后修改。 Stat和Fstat软件的函数返回的信息包含所有文件的属性结构。在第四章中,我们将研究在非常详细的文件中的所有属性。我们之间作出了一个目录项的逻辑视图和它实际上是在磁盘上的存储方式的区别。大多数的UNIX文件系统的实现不存储在目录条目本身的属性,因为它们保持同步,当一个文件有多个硬链接的困难。这将

10、成为我们讨论时明确在第四章硬链接。文件名目录中的名称叫做文件名。只有两个字符不能出现在一个文件名是斜线字符(/)和空字符。斜线分隔的文件名,形成了路径名(详见下)和空字符结束路径名。不过,这是很好的做法,限制字符的文件名是正常打印字符集。 (我们的字符限制,因为如果我们使用的文件名shell的一些特殊字符,我们必须使用shell的引用机制引用文件名,这样就可以很复杂。)两个文件名时会自动创建一个新目录被创建:。 (称为点)及. (称为点点)。点指的是当前目录,点点指的是父目录。在根目录,点点是一样的点。UNIX系统的研究和一些老的UNIX系统V文件系统受到限制的文件名,14个字符。 BSD版本

11、扩大了这一限制为255个字符。今天,几乎所有的商业UNIX文件系统的支持至少255个字符的文件名。路径名一个或多个文件名序列,以斜线和斜线开始选择性分离,形成了一个路径名。一个路径名以斜线开始被称为一个绝对路径,否则,它被称为相对路径。相对路径是指相对于当前文件的目录。对于文件系统(/)根的名字是一个特殊情况的绝对路径名没有文件名的组成部分。范例上市的所有目录中的文件的名称并不困难。图1.3显示了在LS(1)命令最基本的执行情况。ls的表示法(1)是正常的方式来引用在UNIX系统手册特定条目。它指的是,在第1 ls的条目。通常的章节编号1至8,并在每个部分的所有条目按字母顺序排列。在整个文本中

12、,我们假设你有一个为UNIX系统的手册的副本。从历史上看,UNIX系统集中所有八节汇集成是所谓的UNIX程序员手册。随着网页数量增加,这种趋势之间分配改为单独的手册部分:针对用户,一个程序员,系统管理员之一,例如。一些UNIX系统的进一步分裂在给定的一节中的手册页,使用大写字母。例如,所有的标准输入/输出(I / O)在ATT的功能1990e是表示,如第3S技术被作为的FOPEN(3S技术)。其他系统取代字母的,如命令C时,数字部分。今天,大多数手册以电子形式。如果您的在线手册,顺便看看ls命令的手册页会是这样 man 1 lsor man -s1 ls图1.3是一个程序,只是打印每个目录中的

13、文件名,没有别的。如果源文件被命名为myls.c,我们编译成默认的a.out的可执行文件由它cc myls.c从历史上看,cc(1)是C编译器。在使用GNU C编译系统的系统,C编译器是gcc(1)。在这里,西西常常与海湾合作委员会。一些示例输出 $ ./a.out /dev . . console tty mem kmem null mouse stdin stdout stderr zero many more lines that arent shown cdrom $ ./a.out /var/spool/cron cant open /var/spool/cron: Permissi

14、on denied $ ./a.out /dev/tty cant open /dev/tty: Not a directory在整个文本,我们将展示我们运行的命令以及在这种方式产生的输出:我们在此字体类型的显示特征,而从程序的输出结果是这样显示。如果我们需要添加评论这个输出,我们就会显示斜体的意见。美元符号前面我们的投入是由shell是印提示。我们将始终显示命令提示符一个美元符号。请注意,不是目录列表按字母顺序排列。名称前的ls命令打印它们排序。有很多细节要考虑在这20行的程序。首先,我们包含了我们自己的头:apue.h.我们包括在几乎每一个在这个文本程序中,这头。这头包括一些标准的系统头和

15、众多的常量定义和函数原型,我们在整个文本中的例子使用。这头一个附录B上市在函数声明中使用的主要由ISO C标准的支持风格。 (我们将有更多的话要说有关ISO C在下一章标准。)我们从命令行参数,argv的1,作为要列出目录名称。在第7章,我们将看看如何,主要功能是如何调用命令行参数和环境变量都可以访问该程序。由于实际的目录条目格式从一个UNIX系统各有不同,我们使用函数opendir,的READDIR和closedir操纵目录。在opendir函数返回一个指针,指向一个目录结构,并就此向我们传递的READDIR函数的指针。我们不在乎在DIR结构的。然后,我们呼吁在一个循环的READDIR,读每

16、一个目录项。本的READDIR函数返回一个指向dirent结构,或当它与目录,一个空指针结束。所有,我们将在dirent结构是每个目录项(d_name)的名称。使用这个名字,我们便可以调用统计函数(4.2节),以确定所有的文件属性。我们呼吁我们的两个函数来处理自己的错误:err_sys和err_quit。我们可以看到从前面的输出,该err_sys函数打印一个内容丰富的信息说明什么类型的错误,遇到(“权限被拒绝”或“不是目录”)。这两个错误功能列在附录B描述,我们也谈到在1.7节有关错误处理的。当程序完成后,它调用的参数为0的函数退出。退出该函数终止程序。按照惯例,一个参数0手段确定,1到255

17、之间的争论意味着发生了错误。在第8.5节,我们将展示如何外壳等任何一个或一个程序,我们写程序,可以得到一个程序,它执行的退出状态。图1.3。列出所有目录中的文件#include apue.h#include int i;main(int argc, char *argv) DIR *dp; struct dirent *dirp; if (argc != 2) err_quit(usage: ls directory_name); if (dp = opendir(argv1) = NULL) err_sys(cant open %s, argv1); while (dirp = readdi

18、r(dp) != NULL) printf(%sn, dirp-d_name); closedir(dp); exit(0);工作目录每个进程都有一个工作目录,有时也被称为当前工作目录。这是使所有的相对路径名被解释的目录。一个进程可以改变其与ChDir函数的工作目录。例如,文档相对路径/备忘录/乔指的是文件或目录乔,目录中的备忘录,在目录中的文档,它必须是在工作目录。从刚才看这个路径,我们知道,这两个文档和备忘录必须是目录,但我们不能判断乔是一个文件或目录。在路径/ usr / lib中/ lint是一个绝对路径名是指在lib文件的目录或目录在目录中USR的皮棉,这是在根目录中,。家目录当我们

19、登录时,工作目录设置为我们的家目录。我们的家目录是从我们的密码文件中的条目。原文:UNIX System OverviewAdvanced Programming in the UNIX Environment: Second Edition1.1. IntroductionAll operating systems provide services for programs they run. Typical services include executing a new program, opening a file, reading a file, allocating a regio

20、n of memory, getting the current time of day, and so on. The focus of this text is to describe the services provided by various versions of the UNIX operating system.Describing the UNIX System in a strictly linear fashion, without any forward references to terms that havent been described yet, is ne

21、arly impossible (and would probably be boring). This chapter provides a whirlwind tour of the UNIX System from a programmers perspective. Well give some brief descriptions and examples of terms and concepts that appear throughout the text. We describe these features in much more detail in later chap

22、ters. This chapter also provides an introduction and overview of the services provided by the UNIX System, for programmers new to this environment.1.2. UNIX ArchitectureIn a strict sense, an operating system can be defined as the software that controls the hardware resources of the computer and prov

23、ides an environment under which programs can run. Generally, we call this software the kernel, since it is relatively small and resides at the core of the environment. Figure 1.1 shows a diagram of the UNIX System architecture.The interface to the kernel is a layer of software called the system call

24、s (the shaded portion in Figure 1.1). Libraries of common functions are built on top of the system call interface, but applications are free to use both. (We talk more about system calls and library functions in Section 1.11.) The shell is a special application that provides an interface for running

25、 other applications.In a broad sense, an operating system is the kernel and all the other software that makes a computer useful and gives the computer its personality. This other software includes system utilities, applications, shells, libraries of common functions, and so on.For example, Linux is

26、the kernel used by the GNU operating system. Some people refer to this as the GNU/Linux operating system, but it is more commonly referred to as simply Linux. Although this usage may not be correct in a strict sense, it is understandable, given the dual meaning of the phrase operating system. (It al

27、so has the advantage of being more succinct.)1.3. Logging InLogin NameWhen we log in to a UNIX system, we enter our login name, followed by our password. The system then looks up our login name in its password file, usually the file /etc/passwd. If we look at our entry in the password file we see th

28、at its composed of seven colon-separated fields: the login name, encrypted password, numeric user ID (205), numeric group ID (105), a comment field, home directory (/home/sar), and shell program (/bin/ksh). sar:x:205:105:Stephen Rago:/home/sar:/bin/kshAll contemporary systems have moved the encrypte

29、d password to a different file. In Chapter 6, well look at these files and some functions to access them.ShellsOnce we log in, some system information messages are typically displayed, and then we can type commands to the shell program. (Some systems start a window management program when you log in

30、, but you generally end up with a shell running in one of the windows.) A shell is a command-line interpreter that reads user input and executes commands. The user input to a shell is normally from the terminal (an interactive shell) or sometimes from a file (called a shell script). The common shell

31、s in use are summarized in Figure 1.2.The system knows which shell to execute for us from the final field in our entry in the password file.The Bourne shell, developed by Steve Bourne at Bell Labs, has been in use since Version 7 and is provided with almost every UNIX system in existence. The contro

32、l-flow constructs of the Bourne shell are reminiscent of Algol 68.The C shell, developed by Bill Joy at Berkeley, is provided with all the BSD releases. Additionally, the C shell was provided by AT&T with System V/386 Release 3.2 and is also in System V Release 4 (SVR4). (Well have more to say about

33、 these different versions of the UNIX System in the next chapter.) The C shell was built on the 6th Edition shell, not the Bourne shell. Its control flow looks more like the C language, and it supports additional features that werent provided by the Bourne shell: job control, a history mechanism, an

34、d command line editing.The Korn shell is considered a successor to the Bourne shell and was first provided with SVR4. The Korn shell, developed by David Korn at Bell Labs, runs on most UNIX systems, but before SVR4 was usually an extra-cost add-on, so it is not as widespread as the other two shells.

35、 It is upward compatible with the Bourne shell and includes those features that made the C shell popular: job control, command line editing, and so on.The Bourne-again shell is the GNU shell provided with all Linux systems. It was designed to be POSIX-conformant, while still remaining compatible wit

36、h the Bourne shell. It supports features from both the C shell and the Korn shell.The TENEX C shell is an enhanced version of the C shell. It borrows several features, such as command completion, from the TENEX operating system (developed in 1972 at Bolt Beranek and Newman). The TENEX C shell adds m

37、any features to the C shell and is often used as a replacement for the C shell.Linux uses the Bourne-again shell for its default shell. In fact, /bin/sh is a link to /bin/bash. The default user shell in FreeBSD and Mac OS X is the TENEX C shell, but they use the Bourne shell for their administrative

38、 shell scripts because the C shells programming language is notoriously difficult to use. Solaris, having its heritage in both BSD and System V, provides all the shells shown in Figure 1.2. Free ports of most of the shells are available on the Internet.Throughout the text, we will use parenthetical

39、notes such as this to describe historical notes and to compare different implementations of the UNIX System. Often the reason for a particular implementation technique becomes clear when the historical reasons are described.Throughout this text, well show interactive shell examples to execute a prog

40、ram that weve developed. These examples use features common to the Bourne shell, the Korn shell, and the Bourne-again shell.1.4. Files and DirectoriesFile SystemThe UNIX file system is a hierarchical arrangement of directories and files. Everything starts in the directory called root whose name is t

41、he single character /.A directory is a file that contains directory entries. Logically, we can think of each directory entry as containing a filename along with a structure of information describing the attributes of the file. The attributes of a file are such things as type of fileregular file, dir

42、ectorythe size of the file, the owner of the file, permissions for the filewhether other users may access this fileand when the file was last modified. The stat and fstat functions return a structure of information containing all the attributes of a file. In Chapter 4, well examine all the attribute

43、s of a file in great detail.We make a distinction between the logical view of a directory entry and the way it is actually stored on disk. Most implementations of UNIX file systems dont store attributes in the directory entries themselves, because of the difficulty of keeping them in synch when a fi

44、le has multiple hard links. This will become clear when we discuss hard links in Chapter 4.FilenameThe names in a directory are called filenames. The only two characters that cannot appear in a filename are the slash character (/) and the null character. The slash separates the filenames that form a

45、 pathname (described next) and the null character terminates a pathname. Nevertheless, its good practice to restrict the characters in a filename to a subset of the normal printing characters. (We restrict the characters because if we use some of the shells special characters in the filename, we hav

46、e to use the shells quoting mechanism to reference the filename, and this can get complicated.)Two filenames are automatically created whenever a new directory is created: . (called dot) and . (called dot-dot). Dot refers to the current directory, and dot-dot refers to the parent directory. In the r

47、oot directory, dot-dot is the same as dot.The Research UNIX System and some older UNIX System V file systems restricted a filename to 14 characters. BSD versions extended this limit to 255 characters. Today, almost all commercial UNIX file systems support at least 255-character filenames.PathnameA s

48、equence of one or more filenames, separated by slashes and optionally starting with a slash, forms a pathname. A pathname that begins with a slash is called an absolute pathname; otherwise, its called a relative pathname. Relative pathnames refer to files relative to the current directory. The name

49、for the root of the file system (/) is a special-case absolute pathname that has no filename component.ExampleListing the names of all the files in a directory is not difficult. Figure 1.3 shows a bare-bones implementation of the ls(1) command.The notation ls(1) is the normal way to reference a part

50、icular entry in the UNIX system manuals. It refers to the entry for ls in Section 1. The sections are normally numbered 1 through 8, and all the entries within each section are arranged alphabetically. Throughout this text, we assume that you have a copy of the manuals for your UNIX system.Historica

51、lly, UNIX systems lumped all eight sections together into what was called the UNIX Programmers Manual. As the page count increased, the trend changed to distributing the sections among separate manuals: one for users, one for programmers, and one for system administrators, for example.Some UNIX syst

52、ems further divide the manual pages within a given section, using an uppercase letter. For example, all the standard input/output (I/O) functions in AT&T 1990e are indicated as being in Section 3S, as in fopen(3S). Other systems have replaced the numeric sections with alphabetic ones, such as C for

53、commands.Today, most manuals are distributed in electronic form. If your manuals are online, the way to see the manual pages for the ls command would be something like man 1 lsor man -s1 lsFigure 1.3 is a program that just prints the name of every file in a directory, and nothing else. If the source

54、 file is named myls.c, we compile it into the default a.out executable file by cc myls.cHistorically, cc(1) is the C compiler. On systems with the GNU C compilation system, the C compiler is gcc(1). Here, cc is often linked to gcc.Some sample output is $ ./a.out /dev . . console tty mem kmem null mo

55、use stdin stdout stderr zero many more lines that arent shown cdrom $ ./a.out /var/spool/cron cant open /var/spool/cron: Permission denied $ ./a.out /dev/tty cant open /dev/tty: Not a directoryThroughout this text, well show commands that we run and the resulting output in this fashion: Characters t

56、hat we type are shown in this font, whereas output from programs is shown like this. If we need to add comments to this output, well show the comments in italics. The dollar sign that precedes our input is the prompt that is printed by the shell. Well always show the shell prompt as a dollar sign.No

57、te that the directory listing is not in alphabetical order. The ls command sorts the names before printing them.There are many details to consider in this 20-line program. First, we include a header of our own: apue.h. We include this header in almost every program in this text. This header includes some standard system headers and defines numerous constants and function prototypes that we use throughout the examples in the text. A listing of this header is in Appendix B. The declaration of the main func

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论