⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 u4

📁 unix v7是最后一个广泛发布的研究型UNIX版本
💻
字号:
.SHIV.  PROGRAMMING.PPThere will be no attempt made to teach any ofthe programming languages availablebut a few words of advice are in order.One of the reasons why the.UC UNIXsystem is a productive programming environmentis that there is already a rich set of tools available,and facilities like pipes, I/O redirection,and the capabilities of the shelloften make it possible to do a jobby pasting together programs that already existinstead of writing from scratch..SHThe Shell.PPThe pipe mechanism lets you fabricate quite complicated operationsout of spare parts that already exist.For example,the first draft of the.UL  spell program was (roughly).P1.ta .6i 1.2icat ...	\f2collect the files\f3| tr ...	\f2put each word on a new line\f3| tr ...	\f2delete punctuation, etc.\f3| sort	\f2into dictionary order\f3| uniq	\f2discard duplicates\f3| comm	\f2print words in text\f3	\f2  but not in dictionary\f3.P2More pieces have been added subsequently,but this goes a long wayfor such a small effort..PPThe editor can be made to do things that would normallyrequire special programs on other systems.For example, to list the first and last lines of each of aset of files, such as a book,you could laboriously type.P1ede chap1.11p$pe chap1.21p$p.ft Retc..P2But you can do the job much more easily.One way is to type.P1ls chap* >temp.P2to get the list of filenames into a file.Then edit this file to make the necessaryseries of editing commands(using the global commands of.UL ed ),and write it into.UL script .Now the command.P1ed <script.P2will producethe same output as the laborious hand typing.Alternately(and more easily),you can use the fact that the shell will perform loops,repeating a set of commands over and over againfor a set of arguments:.P1for i in chap*do	ed $i <scriptdone.P2This sets the shell variable.UL ito each file name in turn,then does the command.You can type this command at the terminal,or put it in a file for later execution..SHProgramming the Shell.PPAn option often overlooked by newcomersis that the shell is itself a programming language,with variables,control flow.UL if-else , (.UL while ,.UL for ,.UL case ),subroutines,and interrupt handling.Sincethere aremany building-block programs,you can sometimes avoid writing a new programmerely by piecing together some of the building blockswith shell command files..PPWe will not go into any details here;examples and rules can be found in.ulAn Introduction to the.ul.UC UNIX.IT Shell ,by S. R. Bourne..SHProgramming in C.PPIf you are undertaking anything substantial,C is the only reasonable choice of programming language:everything inthe.UC UNIXsystemis tuned to it.Thesystemitselfis written in C,as are most of the programs that run on it.It is also a easy language to useonce you get started.C is introduced and fully described in.ulThe C Programming LanguagebyB. W. Kernighan and D. M. Ritchie(Prentice-Hall, 1978).Several sections of the manualdescribe the system interfaces, that is,how you do I/Oand similar functions.Read.ulUNIX Programmingfor more complicated things..PPMost input and output in C is best handled with the standard I/O library,which provides a set of I/O functionsthat exist in compatible form on most machinesthat have C compilers.In general, it's wisest to confine the system interactionsin a program to the facilities provided by this library..PPC programs that don't depend too much on special features of .UC UNIX(such as pipes)can be moved to other computers that have C compilers.The list of such machines grows daily;in addition to the original.UC PDP -11,it currently includesat leastHoneywell 6000,IBM 370,Interdata 8/32,Data General Nova and Eclipse,HP 2100,Harris /7,VAX 11/780,SEL 86,andZilog Z80.Calls to the standard I/O library will work on all of these machines..PPThere are a number of supporting programs that go with C..UL lintchecks C programs for potential portability problems,and detects errors such as mismatched argument typesand uninitialized variables..PPFor larger programs(anything whose source is on more than one file).UL makeallows you to specify the dependencies among the source filesand the processing steps needed to make a new version;it then checks the times that the pieces were last changedand does the minimal amount of recompilingto create a consistent updated version..PPThe debugger.UL adbis useful for digging through the dead bodiesof C programs,but is rather hard to learn to use effectively.The most effective debugging tool is stillcareful thought, coupled with judiciously placedprint statements..PPThe C compiler provides a limited instrumentation service,so you can find outwhere programs spend their time and what parts are worth optimizing.Compile the routines with the.UL \-poption;after the test run, use.UL profto print an execution profile.The command.UL timewill give you the gross run-time statisticsof a program, but they are not super accurate or reproducible..SHOther Languages.PPIf you .ulhaveto use Fortran,there are two possibilities.You might considerRatfor,which gives you the decent control structuresand free-form input that characterize C,yet lets you write code thatis still portable to other environments.Bear in mind that.UC UNIXFortrantends to produce large and relatively slow-runningprograms.Furthermore, supporting software like.UL adb ,.UL prof ,etc., are all virtually useless with Fortran programs.There may also be a Fortran 77 compiler on your system.If so,this is a viable alternative to Ratfor,and has the non-trivial advantage that it is compatible with Cand related programs.(The Ratfor processorand C toolscan be used with Fortran 77 too.).PPIf your application requires you to translatea language into a set of actions or another language,you are in effect building a compiler,though probably a small one.In that case,you should be usingthe.UL yacccompiler-compiler, which helps you develop a compiler quickly.The.UL lexlexical analyzer generator does the same jobfor the simpler languages that can be expressed as regular expressions.It can be used by itself,or as a front end to recognize inputs for a.UL yacc -basedprogram.Both.UL yaccand.UL lexrequire some sophistication to use,but the initial effort of learning themcan be repaid many times over in programsthat are easy to change later on..PPMost.UC UNIXsystems also make available other languages,such asAlgol 68, APL, Basic, Lisp, Pascal, and Snobol.Whether these are useful depends largely on the local environment:if someone cares about the language and has worked on it,it may be in good shape.If not, the odds are strong that itwill be more trouble than it's worth.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -