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

📄 u4

📁 UNIX v6源代码 这几乎是最经典的unix版本 unix操作系统设计和莱昂氏unix源代码分析都是用的该版
💻
字号:
.SHIV.  PROGRAMMING.PP.UC UNIXis a marvelously pleasant and productive system forwriting programs;productivity seems to be an order of magnitude higherthan on other interactive systems..PPThere will be no attempt made to teach any ofthe programming languages available on.UC UNIX ,but a few words of advice are in order.First,.UC UNIXis written in C,as is most of the applications code.If you are undertaking anything substantial,C is the only reasonable choice.More on that in a moment.But remember that there are quite a few programs already written,some of which have substantial power..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, say a book,you could laboriously type.B1ede chap1.11p$pe chap1.21p$p etc..B2But instead you can do the job once and for all.Type.B1ls chap* >temp.B2to get the list of filenames into a file.Then edit this file to make the necessaryseries of editing commands(using the global commands of.C ed ),and write it into ``script''.Now the command.B1ed <script.B2will producethe same output as the laborious hand typing..PPThe pipe mechanism lets you fabricate quite complicated operationsout of spare parts already built.For example, the first draft of the .C spellprogram was (roughly).B1.ne 7cat ... 	(collect the files)| tr ...	(put each word on a new line,	  delete punctuation, etc.)| sort	(into dictionary order)| uniq	(strip out duplicates)| comm	(list words found in text but	  not in dictionary).B2.SHProgramming the Shell.PPAn option often overlooked by newcomersis that the shell is itself a programming language,and since.UC UNIXalready has a host of building-block programs,you can sometimes avoid writing a special purpose programmerely by piecing together some of the building blockswith shell command files..PPAs an unlikely example,suppose you want to countthe number of users on the machine every hour.You could type.B1.ne 2datewho | wc -l.B2every hour, and write down the numbers, but that is rather primitive.The next step is probably to say.B1(date; who | wc -l) >>users.B2which uses ``>>'' to .ulappendto the end of the file ``users''.(We haven't mentioned ``>>'' before _it's another service of the shell.)Now all you have to do is to put a loop around this,and ensure that it's done every hour.Thus, place the following commands into a file, say ``count'':.B1.ne 4: loop(date; who | wc -l) >>userssleep  3600goto loop.B2The command .C :is followed by a space and a label,which you can then .C goto .Notice that it's quite legal to branch backwards.Now if you issue the command.B1sh  count  &.B2the users will be counted every hour,and you can go on with other things.(You will have to use.C killto stop counting.).PPIf you would like ``every hour'' to be a parameter,you can arrange for that too:.B1.ne 4: loop(date; who | wc - l) >>userssleep  $1goto loop.B2``$1'' means the first argument when this procedure is invoked.If you say.B1sh count 60.B2it will count every minute.A shell program can have up to nine arguments,``$1'' through ``$9''..PPThe other aspect of programming is conditional testing.The.C ifcommandcan test conditions and execute commands accordingly.As a simple example, suppose you want to add to your login sequencesomething to print your mail if you have some.Thus, knowing that mail is stored in a file called`mailbox', you could say.B1if -r mailbox  mail.B2This says``if the file `mailbox' is readable, execute the.C mailcommand.''.PPAs another example, you could arrange that the``count''procedure count every hour by default,but allow an optional argument to specify a different time.Simply replace the ``sleep $1'' line by.B1.ne 2if $1x = x sleep 3600if $1x != x sleep $1.B2The construction.B1if $1x = x.B2tests whether ``$1'', the first argument, was present or absent..PPMore complicated conditions can be tested:you can find out the status of an executed command,and you can combine conditions with `and', `or',`not' and parentheses _see.SE if (I).You should also read.SE shift (I)which describes how to manipulate arguments to shell command files..SHProgramming in C.PPAs we said, C is the language of choice:everything in.UC UNIXis tuned to it.It is also a remarkably easy language to useonce you get started.Sections II and III of the manualdescribe the system interfaces, that is,how you do I/Oand similar functions..PPYou can write quite significant C programswith the level of I/O and system interface described in.ulProgramming in C: A Tutorial,if you use existing programs and pipes to help.For example, rather than learning how to open and close filesyou can (at least temporarily) write a program that readsfrom its standard input,and use.C catto concatentate several files into it.This may not be adequate for the long run,but for the early stages it's just right..PPThere are a number of supporting programs that go with C.The C debugger,.C cdb ,is marginally useful for digging through the dead bodiesof C programs..C db ,the assembly language debugger, is actually more useful mostof the time,but you have to know more about the machineand system to use it well.The most effective debugging tool is stillcareful thought, coupled with judiciously placedprint statements..PPYou can instrument C programs and thus find outwhere they spend their time and what parts are worth optimising.Compile the routines with the ``-p'' option;after the test run use.C profto print an execution profile.The command.C timewill give you the gross run-time statisticsof a program, but it's not super accurate or reproducible..PPC programs that don't depend too much on special features of .UC UNIXcan be moved to the Honeywell 6070and.UC IBM370 systemswith modest effort.Read .ul 3The.UC GCOSC Libraryby M. E. Lesk and B. A. Barres for details..SHMiscellany.PPIf you .ulhaveto use Fortran,you might consider.C ratfor ,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.C db ,.C prof ,etc., are all virtually useless with Fortran programs..PPIf you want to use assembly language(all heavens forfend!),try the implementation language.UC LIL,which gives you many of the advantages of a high-level language,like decent control flow structures,but still lets you get close to the machineif you really want to..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.C yacccompiler-compiler, which helps you develop a compiler quickly.

⌨️ 快捷键说明

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