C Programming in Plan 9 from Bell Labs Pietro Gagliardi ABSTRACT 6DEI F=FAH EI =n EnJHo@K?JEon Jo FHoCH=mmEnC MEJD 2l=n ' BHom *All L=>I MEJD JDA + l=nCK=CA. 2l=n ' FHoLE@AI noJ onlO = IECnEBE?=nJlO EmFHoLA@ LAHIEon oB +, >KJ =lIo = nKm>AH oB FHoCH=mmEnC lE>H=HEAI Jo IEmFlEBO ?omFlE?=JA@ J=IkI. 6DEI F=FAH EI mA=nJ Jo >A = IKFFlAmAnJ Jo JDA m=nK=l F=CAI, oJDAH @o?KmAnJI FHoLE@A@ >O JDA IOIJAm En /sys/doc,=n@=FHoCH=mmAHߣIlEJAH=JKHA?ollA?JEon. 1. Introduction 2l=n ' BHom *All L=>I D=I =lM=OI >AAn = IOIJAm =>oLA JDA HAIJ: IEmFlA, FoHJ=>lA, =n@ BA=JKHA-?omFlAJA. 1J EInߣJ 7N1:;® H=JDAH, EJ EmFHoLAI on JDA >=IE?I oB 7N1: >O FHoLE@­ EnC = nKm>AH oB BA=JKHAI =>IAnJ BHom moIJ oJDAH oFAH=JEnC IOIJAmI. OnA oB JDoIA BA=­ JKHAI EI = CHA=J FHoCH=mmEnC AnLEHonmAnJ JD=J HEL=lI 7N1:ߣI. 2l=n ' EI BKllO 7nE?o@A- ?onBoHm=nJ JDHoKCD EJI nA=HlO KnELAHI=l KIA oB JDA 76.-& An?o@EnC, >HoKCDJ Jo KI >O JMo oB JDA FAoFlA JD=J >HoKCDJ KI 2l=n '. 1J noJ onlO kAAFI JDA + l=nCK=CA oB ol@, >KJ JDHoKCD JDA MoHk oB KAn 6DomFIon, EJ FHoLE@AI = + JD=J m=kAI IomA oJDAHMEIA ?omFlE­ ?=JA@ ?onIJHK?JI IJH=ECDJBoHM=H@. *=?kEnC JDEI nAM + KF EI !! FHoCH=mm=>ElEJO lE>H=HEAI JD=J IECnEBE?=nJlO HA@K?A JDA =moKnJ oB ?o@A = FHoCH=mmAH nAA@I Jo MHEJA. )n@ ALAHO IEnClA lEnA oB JDEI ?o@A EI BKllO FoHJ=>lA =monC @EBBAHAnJ 2l=n ' EnIJ=ll=JEonI, ALAn MEJD @EBBAHAnJ =H?DEJA?JKHAI ߞ JDA noJEon oB = configure I?HEFJ D=I >AAn L=nGKEIDA@ =J l=IJ. LA=HnEnC FHoCH=mmEnC MEJD 2l=n ' EI noJ IomAJDEnC JD=J HAGKEHAI ?omFlE?=JA@ JANJ­ >ookI =n@ BoKH OA=HI oB ?ollACA IJK@O Jo m=IJAH. 1n B=?J,MEJDjKIJJDAm=nK=lF=CAI=n@ F=CAI oB IomA @o?KmAnJ=JEon En D=n@, IomAonA ?=n GKE?klO m=IJAH JDA ?oHA ?on?AFJI. 0oMALAH, JDAHA IomAJEmAI EI = nAA@ oB = IJ=HJAHߣI CKE@A oH JKJoHE=l Jo IJ=HJ MEJD oH Jo ?lA=H KF IomA Kn?AHJ=EnJO. 6D=J J=Ik EI MD=J JDEI F=FAH =EmI Jo @o. 6DEI F=FAH EI not = BKll HABAHAn?A Jo 2l=n 'ߣI FHoCH=mmEnC AnLEHonmAnJ ߞ JDA m=nK=l F=CAI @o JD=J. KAAF JDEIEnmEn@MDElAOoKHA=@. ;oK nAA@ Jo knoM DoM Jo KIA 2l=n ' BHom *All L=>I, H?, =n A@EJoH IK?D =I I=m oH =?mA, =n@ JDA + FHoCH=mmEnC l=nCK=CA Jo IJ=HJ. 6DA oBBE?E=l CKE@A Jo + EI 2HoB. *HE=n KAHnECD=n =n@ ,AnnEI 4EJ?DEAߣI The C Programming Language, noM En EJI IA?on@ A@E­ JEon. 4A=@JDHoKCDEJ:OoKߣlllA=HnGKEJA=loJ. 2. Core Concepts 0AHA EI KAHnECD=nߣI DAllo, MoHl@-FHEnJEnC FHoCH=m JD=J D=I >A?omA GKEnJAIIAn­ JE=l, En 5J=n@=H@ + =n@ MEJD = BAM @EBBAHAn?AI BHom JDA onA En KAHnECD=nߣI >ook BoH ANFoIEJEonFKHFoIAI. ­ ­ #include <stdio.h> int main() { printf("hello, world\n"); return 0; } NoMDAHAEJEI=I=2l=n'FHoCH=mmAHMoKl@MHEJAEJ. #include <u.h> #include <libc.h> void main() { print("hello, world\n"); exits(0); } 1mmA@E=JAlO, ANFAHJ + FHoCH=mmAHI MEll I=O JDEnCI lEkA 9DAHA @E@ stdio Co? =n@ IDoKJ =J JDA JoF oB JDAEH lKnCI JDEnCI lEkA ;oK ?=nߣJ @A?l=HA main =I HAJKHnEnC void 1BOoKߣHAonAoBJDAIACKOI,JDAnOoK>AJJAHCAJKIA@JoEJ. 6DA En?lK@A BElA u.h, IJoHA@ En /$objtype/include MDAHA $objtype EI =n AnLEHonmAnJ L=HE=>lA IJoHEnC JDA ?KHHAnJ +27 n=mA, ?onJ=EnI +27-IFA?EBE? @ABEnEJEonI. )ll DA=@AH BElAI En 2l=n ' KIA JDEI, Io EJ mKIJ >A En?lK@A@ BEHIJ. NANJ ?omAI libc.h, IJoHA@ En /sys/include. libc.h ?onJ=EnI JDA @ABEnEJEonI BoH JDA + lE>H=HO, MDE?D EI lEnkA@EnJoALAHO2l=n'FHoCH=m. 6DA+lE>H=HO?onIEIJIoBIALAH=lF=HJI: ࢫ )llJDA2l=n'IOIJAm?=llII=LABoH=BAMJD=JonlOJDAlE>H=HOKIAI ࢫ )IAJoBIK>HoKJEnAIJoB=?ElEJ=JAKIEnCJDAIOIJAm?=llI ࢫ 6DABoHm=JJA@FHEnJHoKJEnAI ࢫ M=JDAm=JE?=lBKn?JEonI ࢫ 6EmABKn?JEonI ࢫ .Kn?JEonIBoHMoHkEnCMEJD7nE?o@A?D=H=?JAHI,oH RuneI libc mKIJ>AIA?on@;EJEIKIA@>OmoIJ,EBnoJ=ll,oJDAHlE>H=HEAI. 6DA print BKn?JEon EI = mAm>AH oB JDA IAJ oB BoHm=JJA@ FHEnJ HoKJEnAI; EJ MoHkI jKIJlEkA printf En+,MEJDIALAH=lmEnoH@EBBAHAn?AI: ࢫ 6DA %u BoHm=J EI ConA; EJ D=I >AAn HAFl=?A@ MEJD JDA u mo@EBEAH Jo oJDAH EnJACAH BoHm=JI. 5oEnIJA=@oBI=OEnC %−3lu,OoKI=O %−3uld. ࢫ 6DA %b BoHm=JEIFHoLE@A@BoHFHEnJEnC>En=HOnKm>AHI. ࢫ 6DA %C =n@ %S BoHm=JI =HA FHoLE@A@ BoH FHEnJEnC 76.-& ?D=H=?JAHI, ?=llA@ RuneI, =n@IJHEnCIoB RuneI,HAIFA?EJLAlO. 6DAO=HA@EI?KIIA@l=JAH. ࢫ 6DA ll mo@EBEAHBl=CJoEnJACH=lBoHm=JIFHEnJI vlongI,MDE?D=HA@AI?HE>A@l=JAH. ࢫ 6DA %r BoHm=JFHEnJIJDAAHHoHIJHEnC,MDE?DEI@AI?HE>A@nANJ. ࢫ ;oK?=n?HA=JAOoKHoMnBoHm=JI;JD=JEI@AI?HE>A@l=JAH. OJDAHMEIA, print >AD=LAIJDAI=mA=I printf. exits =n@ JDA void HAJKHn BHom main HAGKEHA = >EJ oB ANFl=n=JEon. 6DA JH=@E­ JEon=l M=O oB HAFHAIAnJEnC AHHoHI =n@ IJ=JKI HAJKHnI En + EI MEJD nKm>AHI: = HAJKHn BHom main oH JDA =HCKmAnJ Jo exit HAFHAIAnJI = IJ=JKI HAJKHn BHom = FHoCH=m, =n@ errno IJoHAI EnBoHm=JEon =>oKJ AHHoH HAJKHnI BHom BKn?JEonI. 6DA JH=@EJEon=l >AD=LEoH EI Jo D=LA zAHo mA=n no AHHoH =n@ =nO oJDAH L=lKA mA=n AHHoH; )N51 + @ABEnAI EXIT_SUCCESS =n@ EXIT_FAILURE BoHIJ=JKIHAJKHnIBHomFHoCH=mI. ­!­ 6DEI CAJI HAIJHE?JA@ LAHO GKE?klO. )N51 + onlO @ABEnAI JDHAA IJ=n@=H@ L=lKAI BoH errno @om=En AHHoH, H=nCA AHHoH, =n@ EllAC=l mKlJE>OJA IAGKAn?A =n@ JMo L=lKAI BoH IJ=JKI HAJKHn. )n@ IomAJEmAI =n EnJACAH MonߣJ JAll OoK AnoKCD. .oH AN=mFlA, lAJߣI J=kA JDA 7N1: lseek IOIJAm?=ll,MDE?Dm=nEFKl=JAIJDABElAHA=@/MHEJAFoIEJEon: long lseek(int fd, long offset, int from); 1B =nO =HCKmAnJ EI EnL=lE@ BoH AN=mFlA, from noJ , , oH , lseek HAJKHnI MEJD errno IAJ Jo EINVAL IFA?EBE? Jo 7N1:. *KJ JDEI @oAInߣJ JAll OoK which =HCKmAnJ M=I EnL=lE@, oH DoM m=nO; EJ onlO I=OI JD=J IomAJDEnC M=I noJ HECDJ. 9A ?=n =@@ JDA =FFHo­ FHE=JA errno L=lKAI Jo HAIolLA JDEI FHo>lAm. *KJ MD=J =>oKJ = lE>H=HO JD=J @ABEnAI oLAH , L=lKAI BoH errno? On m=?DEnAI MEJD Im=ll int IEzAI, JDEI ?DokAI OoKH FHoCH=m =n@@ABA=JIJDAFKHFoIAIoB>oJDIE@AI. ) >AJJAH E@A= EI Jo CELA JDA FHoCH=mmAH JDA =>ElEJO Jo D=n@lA =nO AHHoH JD=J ?omAI En MEJDoKJ MoHHO oB loIEnC IJ=n@=H@I ?omFlE=n?A oH ?l=HEJO, =n@ Jo CAnAH=JA =nO AHHoH MEJDoKJ B=llEnC EnJo = IKHBAEJ oB FoIIE>ElEJO. 5o JDA @AIECnAHI oB 2l=n ' @A?E@A@ Jo KIA IJHEnCI EnIJA=@ oB nKm>AHI. -=?D FHoCH=m D=I =n error string MDE?D EI IAJ >O HoKJEnAI MDAn =n AHHoH o??KHI. )n@ A=?D FHoCH=m HAJKHnI = IJHEnC Jo JDA DoIJ AnLEHonmAnJ MEJD JDA exits IOIJAm ?=ll. 6DA L=lKA CELAn Jo exits ?=n >A =??AIIA@ BHom H? JDHoKCD JDA AnLEHonmAnJL=HE=>lA $status. 5o MEJD = IJHEnC, DoM @o OoK HAFHAIAnJ = l=?k oB AHHoH? 9DO, MEJD = nKll FoEnJAH oH nKllIJHEnC *A?=KIAJDA?onIJ=nJ 0 JKHnIEnJo=nKllFoEnJAH,JDAIJ=JAmAnJ exits(0); @oAIALAHOJDEnC=lHA=@O. OB?oKHIAOoK?=n=lIoI=O exits(nil); oH exits(""); nil,En u.h,EI2l=n'ߣI NULL. 5o DoM @oAI JDEI ANFl=En MDO main D=I Jo HAJKHn void? ;oK ?=nߣJ HAJKHn = IJHEnC Fl=?A@En=KJom=JE?IJoH=CABHom=BKn?JEon: char * f(void) { auto char s[] = "hello"; return s; /* WRONG */ } *KJ=FHoCH=mmAHm=OIJoHAJDAANEJIJ=JKIoB=FHoCH=mEnJDEIM=O. An Aside on Style 2l=n ' FHoCH=mI =HA KIK=llO MHEJJAn Jo ?onBoHm Jo = FHA@ABEnA@ IAJ oB IJOlA CKE@A­ lEnAI,@AI?HE>A@EnJDAm=nK=lF=CA style$,BoHJDAI=kAoBKnEBoHmEJO. 0AHAEI=J=IJA: ­"­ static int func(int f, char *g[]) { int i, j; j = 5; acquirelock(); for(i = 0; i < j; i++){ process(i, &j); if((j = g(&i)) == 0 ? h() : i()) /* g() affects h()/i() */ if(strcmp(s, t) == 0) something(); } return j − i; } OB ?oKHIA JDEI FEA?A oB ?o@A @oAInߣJ @o =nOJDEnC IAnIE>lA >O EJIAlB. 1J M=I MHEJJAn Jo IDoM JDA >=IE?I oB JDEI IJOlA. 1B OoK M=nJ Jo ?onJHE>KJA Jo 2l=n ', >A IKHA Jo KIA JDEI IJOlA. OB?oKHIA,OoK?=nIJEllKIAOoKHB=LoHEJAIJOlAAlIAMDAHA. 3. Compiling Programs 7N1: ?omFElAHICELAOoKJDAoFJEonoB?omFElEnC=FHoCH=mEnonAIDoJ: $ cc a.c b.c # compile and link; creates a.out $ a.out # run oHEnFEA?AI: $ cc −c a.c # compile; creates a.o $ cc −c b.c # compile $ cc a.o b.o −lS # link; creates a.out. you can also use ld and omit −lS $ a.out # run 2l=n ' CELAI OoK no ?DoE?A >KJ Jo @o JDA l=JJAH, >KJ MEJD ld EnIJA=@ oB cc BoH JDA BEn=l IJ=CA. On JoF oB JD=J, JDAHA EI no IEnClA + ?omFElAH =n@ no IEnClA lEnkAH ߞ JDAHA EI onA oBA=?DBoHA=?DIKFFoHJA@FHo?AIIoH=H?DEJA?JKHA. 9D=J =HA JDA >AnABEJI Jo JDEI HAGKEHAmAnJ? .EHIJ, l=HCA FHojA?JI ?=n >A >KElJ MEJD A=IA, jKIJ lEkA make. 2l=n ' FHoLE@AI =n EmFHoLA@ L=HE=nJ, ?=llA@ mk, JD=J 1 @AI?HE>A l=JAH.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages20 Page
-
File Size-