![作業(yè)系統(tǒng)導(dǎo)論一課件_第1頁](http://file4.renrendoc.com/view/e9800b7c912630ff38cc583a40451893/e9800b7c912630ff38cc583a404518931.gif)
![作業(yè)系統(tǒng)導(dǎo)論一課件_第2頁](http://file4.renrendoc.com/view/e9800b7c912630ff38cc583a40451893/e9800b7c912630ff38cc583a404518932.gif)
![作業(yè)系統(tǒng)導(dǎo)論一課件_第3頁](http://file4.renrendoc.com/view/e9800b7c912630ff38cc583a40451893/e9800b7c912630ff38cc583a404518933.gif)
![作業(yè)系統(tǒng)導(dǎo)論一課件_第4頁](http://file4.renrendoc.com/view/e9800b7c912630ff38cc583a40451893/e9800b7c912630ff38cc583a404518934.gif)
![作業(yè)系統(tǒng)導(dǎo)論一課件_第5頁](http://file4.renrendoc.com/view/e9800b7c912630ff38cc583a40451893/e9800b7c912630ff38cc583a404518935.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
system(constchar*string)Executesystemcommand
1system(constchar*string)Exec#include<stdlib.h>#include<stdio.h>intmain(){printf("Runningpswithsystem\n");system("ps-ax");printf("Done.\n");exit(0);}2#include<stdlib.h>2#include<stdlib.h>#include<stdio.h>intmain(){printf("Runningpswithsystem\n");system("ps-ax&");printf("Done.\n");exit(0);}3#include<stdlib.h>3Execfamilyintexecl(constchar*path,constchar*arg,...);
intexeclp(constchar*file,constchar*arg,...);
intexecle(constchar*path,constchar*arg
,...,char*const
envp[]);
intexecv(constchar*path,char*const
argv[]);
intexecvp(constchar*file,char*const
argv[]);Theexecfamilyoffunctionsreplacesthecurrentprocessimagewithanewprocessimage.4Execfamilyintexecl(constcMeaningofdifferentletters:l:needsalistofarguments.v:needsanargv[]vector(landvaremutuallyexclusive).e:needsanenvp[]array.p:needsthePATHvariabletofindtheexecutablefile5Meaningofdifferentletters:5#include<stdio.h>#include<unistd.h>intmain(){printf("Callingexecl...nn");execl("/bin/cat","cat","./example-1.c",NULL);printf("Uselesscalltoprintf");}6#include<stdio.h>6行程管理已進(jìn)入執(zhí)行階段的程式稱之為『行程』(Process,或程序)。簡單的說,程序即是執(zhí)行中的程式。行程在執(zhí)行當(dāng)中會隨著需要產(chǎn)生(fork)其他子行程(ChildProcess),並給於子行程執(zhí)行生命及執(zhí)行工作項(xiàng)目。既然『行程』是執(zhí)行中的獨(dú)立程式,當(dāng)他被執(zhí)行時,所有主機(jī)系統(tǒng)的資源都可被它所存取,至於控制主機(jī)資源的程度,則在於該行程的權(quán)限等級如何,而與系統(tǒng)中多少行程無關(guān)。7行程管理已進(jìn)入執(zhí)行階段的程式稱之為『行程』(Process行程命令建立(fork):行程可以執(zhí)行fork系統(tǒng)呼叫,來產(chǎn)生另一個子行程(ChildProcess),而產(chǎn)生行程者則稱為『父行程』(ParentProcess)。行程必定是由另一個行程所產(chǎn)生,而無法自行產(chǎn)生。產(chǎn)生子行程的原因可以接發(fā)生特殊原因或接受命令。8行程命令建立(fork):行程可以執(zhí)行fork系統(tǒng)呼叫fork()isdefinedat<unistd.h>#include<stdio.h>#include<unistd.h>intmain(){printf("HelloWorld\n");fork();printf("GoodbyeCruelWorld\n");}9fork()isdefinedat<unistd.h『行程』的特性
EachhasauniquePID.SomespecialPIDs:0:scheduler1:init2:pagedaemon10『行程』的特性10getpid()willreturncallingprocess’spid.#include<stdio.h>#include<unistd.h>intmain(){fork();printf("\nHello,IamtheprocesswithID=%d\n",getpid());return0;}11getpid()willreturncallingpParent/childprocess當(dāng)使用者以fork系統(tǒng)呼叫產(chǎn)生一個新的處理程序(process),這個處理程序會承襲其親代(parent)的特性(code,stack…)。此時叫用fork系統(tǒng)呼叫的那個處理程序稱為父處理程序,而衍生(fork)出的那個處理程序即為子處理程序(childprocess)。
12Parent/childprocess當(dāng)使用者以fork#include<stdio.h>#include<unistd.h>intmain(){intpid,num=2;printf("HelloWorld\n");pid=fork();if(pid!=0)printf("I'mtheFatherandmyson'sPIDis%d,num=%d\n",pid,num);elseprintf("I'mtheSon,num=%d\n",num);printf("GoodbyeCruelWorld\n");}fatherreturnchild'spid,childprocessreturn0,failreturn-113#include<stdio.h>13getppid()willreturnfather’spidofcallingprocess.#include<unistd.h>#include<stdlib.h>#include<stdio.h>intmain(void){
pid_tpid;if((pid=fork())>0)printf("Iamtheparent,mypid=%u,mychild’spid=%u\n",getpid(),pid);elseif(pid==0)printf("Iamthechild,mypid=%u,myparent’spid=%u\n",getpid(),getppid());else{perror("fork");return1;}return0;}14getppid()willreturnfather’s行程的基本生命週期(圖)15行程的基本生命週期(圖)15行程的生命週期(1)執(zhí)行狀態(tài)(RunningState):行程已佔(zhàn)有CPU,CPU正在執(zhí)行該行程。(2)預(yù)備狀態(tài)(ReadyState):行程正準(zhǔn)備給CPU執(zhí)行,並已取得執(zhí)行中所需的資源。(3)等待狀態(tài)(WaitState):行程在CPU執(zhí)行當(dāng)中可能欠缺某些資源或逾時而退出,並處於等待索取其他資源狀態(tài);當(dāng)行程取得所需資源或時間到達(dá),則可進(jìn)入預(yù)備狀態(tài),隨時接受CPU處理。16行程的生命週期(1)執(zhí)行狀態(tài)(RunningState)行程的生命週期(4)停止?fàn)顟B(tài)(StopState):行程已完成執(zhí)行,並等待被撤銷。(5)死結(jié)狀態(tài)(DeadlockState):行程等待可能一個永遠(yuǎn)無法得到的資源,而繼續(xù)無止境的等待。17行程的生命週期(4)停止?fàn)顟B(tài)(StopState):行程生命週期歸納如下一個父行程呼叫fork()系統(tǒng)呼叫產(chǎn)生了一個子行程,並給予該行程的生命(譬如,植入vi執(zhí)行程式),子行程進(jìn)入『預(yù)備』佇列,等待被CPU執(zhí)行。當(dāng)該行程取得CPU執(zhí)行之後,如果在『時間片段』中未能執(zhí)行完畢的話,則可能被踢出到預(yù)備狀態(tài),等待下一次被執(zhí)行的機(jī)會;也有可能在執(zhí)行當(dāng)中欠缺某些資源而轉(zhuǎn)換到『等待』狀態(tài),等待取得其他資源;也有可能在時間片段裡執(zhí)行完畢,而變遷到『停止』狀態(tài)。18行程生命週期歸納如下一個父行程呼叫fork()系統(tǒng)呼叫產(chǎn)行程的生命週期同一時間也許會有多個行程進(jìn)入同一狀態(tài)底下,等待改變進(jìn)入另一個狀態(tài)或者被選擇執(zhí)行。因此,系統(tǒng)針對每一種狀態(tài)建立一個管理制度,才能由眾多行程中選擇一個再改變其狀態(tài);一般簡單的做法,即是建立一個佇列(Queue),已先進(jìn)先出的方法,讓先進(jìn)入該狀態(tài)的行程,能先進(jìn)入下一個狀態(tài)。19行程的生命週期同一時間也許會有多個行程進(jìn)入同一狀態(tài)底下,等待改變行程狀態(tài)的因素可區(qū)分為行程命令與系統(tǒng)環(huán)境兩大因素。前者為使用者(或系統(tǒng))所下達(dá)的命令;後者為行程因系統(tǒng)環(huán)境的改變,而變遷其狀態(tài)。20改變行程狀態(tài)的因素可區(qū)分為行程命令與系統(tǒng)環(huán)境兩大因素。20行程命令撤銷(Destroy):無論行程是正常停止或非正常停止,都須經(jīng)過撤銷命令來取回行程所占用的記憶體空間,或其他所持有的資源。阻斷(Block):行程從執(zhí)行狀態(tài)轉(zhuǎn)變到等待狀態(tài)。停止(Stop):在正常情況下,行程執(zhí)行完畢之後,會進(jìn)入停止?fàn)顟B(tài)並等待系統(tǒng)撤銷它。但某些情況,系統(tǒng)也可以執(zhí)行停止命令,來停止某一個執(zhí)行當(dāng)中的行程,並使其進(jìn)入停止?fàn)顟B(tài)。喚醒(Wakeup):行程由等待狀態(tài)轉(zhuǎn)換到執(zhí)行狀態(tài)。21行程命令撤銷(Destroy):無論行程是正常停止或非正常停Processterminationexitfunction:Toterminateaprocesswithanexitcode.Noticethatthisisalibrary,and_exitisasystemcall.Normaltermination:Themainprogramreturns.Theprogramcallsexit.Theprogramcalls_exit.Abnormaltermination:Theprogramcallsabort.Theprogramcatchesasignal.22Processterminationexitfunct#include<unistd.h>#include<stdlib.h>#include<stdio.h>intmain(void){pid_tpid;pid=fork();if(pid>0){printf("daemononduty!\n");exit(0);}elseif(pid<0){printf("Can'tfork!\n");exit(-1);}for(;;){printf("Iamthedaemon!\n");sleep(3);/*dosomethingyourownhere*/}}23#include<unistd.h>23#include<sys/types.h>#include<unistd.h>#include<stdio.h>intmain(){pid_tpid;char*message;intn;printf("forkprogramstarting\n");pid=fork();switch(pid){case-1:perror("forkfailed");exit(1);case0:message="Thisisthechild";n=5;break;default:message="Thisistheparent";n=3;break;}for(;n>0;n--){puts(message);sleep(1);}exit(0);}24#include<sys/types.h>24ProcessterminationWhentheparentterminatesbeforethechild,theinitprocessbecomestheparentofthisorphanprocess.Whenthechildterminatesbeforetheparent,thechildbecomesazombie(defunct).Azombieisadeadentity,butnotcompletelydead,.:Thechildmustleavesufficientinformationintheprocesstablesothatlaterwhenitsparentwantstofetchitsstatus,itisabletodoso,Theinformationazombiekeepsintheprocesstableincludesprocessid,terminationstatus,andaccountinginformation.25ProcessterminationWhenthepa#include<unistd.h>#include<stdlib.h>#include<stdio.h>intmain(void){pid_tpid;inti=0;pid=fork();if(pid>0){printf("thisisparent,mypid=%d\n",getpid());printf("parentexitsfirst\n");exit(0);}sleep(1);for(i=0;i<=10000;i++);printf("thisisson,myparent’spid=%d\n",getppid());}26#include<unistd.h>26#include<unistd.h>#include<stdlib.h>#include<stdio.h>intmain(void){pid_tpid;inti=0,j=0;pid=fork();if(pid==0){printf("thisisson,mypid=%d\n",getpid());printf("Sonexitsfirst\n");exit(0);}sleep(1);for(i=0;i<=10000;i++)for(j=0;j<=10000;j++);printf("parentexitsnow\n");}27#include<unistd.h>27waitfamilieswaitAprocesscancallwaittowaitforthechildprocesstocomplete.Thewaitfunctionprovidesaintegerbufferforreceivingtheterminationstatus.Thewaitfunctionwillblockifnochildisavailable.Thereturnvalueistheprocessidofthechildprocess.waitpidAprocesscancallwaittowaitforaparticularchildprocesstocomplete.Thewaitpidcanbenon-blocking.28waitfamilieswaitAprocessAvoidzombieprocesspid_twait(int*stat_loc)Thewait()callisusedtotellaprocesstowaitforoneofhischildstoendbeforegoingonwithit'sowntask.wait()takestheadressofanint,inwichitputstheexitstatusofthechilditwaitedfor(toknowwhatyoucandowiththatstatus,lookat'manwait')29Avoidzombieprocesspid_twait#include<stdio.h>#include<unistd.h>intmain(){fork();printf("\nHello,IamtheprocesswithID=%d\n",getpid());wait();return0;}30#include<stdio.h>30#include<stdio.h>#include<stdlib.h>#include<sys/types.h>#include<unistd.h>#include<sys/wait.h>intmain(intargc,char*argv[],char*env[]){pid_tmy_pid,parent_pid,child_pid;intstatus;/*getandprintmypidandmyparent'spid.*/my_pid=getpid();parent_pid=getppid();printf("Parent:mypidis%d\n",my_pid);printf("Parent:myparent'spidis%d\n",parent_pid);/*printerrormessageiffork()fails*/if((child_pid=fork())<0){perror("forkfailure");exit(1);}/*fork()==0forchildprocess*/if(child_pid==0){printf("\nChild:Iamanew-bornprocess!\n");my_pid=getpid();parent_pid=getppid();printf("Child:mypidis:%d\n",my_pid);printf("Child:myparent'spidis:%d\n",parent_pid);printf("Child:Iamgoingtoexecute-date-command\n");31#include<stdio.h>31execl("/bin/date","date",0,0);perror("execl()failure!\n");printf("Thisprintisafterexecl()andshouldnotgetexecuted\n");_exit(1);}/**parentprocess*/else{printf("\nParent:Icreatedachildprocess.\n");printf("Parent:mychild'spidis:%d\n",child_pid);system("ps-aclf|grepercal;exit");wait(&status);/*canusewait(NULL)sinceexitstatusfromchildisnotused.*/printf("\nParent:mychildisdead.Iamgoingtoleave.\n");}return0;}32execl("/bin/date","date",0,雖然在一部主機(jī)系統(tǒng)內(nèi)有多個行程『同時』執(zhí)行中,但『中央處理機(jī)』(CPU)在某一時間內(nèi)僅能處理一個行程的工作。Racecondition:Multipleprocessesrunningsimultaneouslycouldresultverystrangeerrors.Ifthecorrectnessofaprogramdependsontheexecutionsequenceofconsistingprocesses,thenwehavearacecondition.33雖然在一部主機(jī)系統(tǒng)內(nèi)有多個行程『同時』執(zhí)行中,但『中央處理機(jī)#include<stdio.h>#include<unistd.h>intmain(){inta=2;printf("a=%d\n",a);if(!fork()){a=a+3;printf("a=%d\n",a);a=a+3;printf("a=%d\n",a);}else{a=a*3;printf("a=%d\n",a);a=a*3;printf("a=%d\n",a);}printf("a=%d\n",a);return0;}34#include<stdio.h>34行程的同步系統(tǒng)中同時存在著許多行程在執(zhí)行當(dāng)中,為了達(dá)成某些任務(wù),必須多個行程互相合作才能完成,尤其是共用資源的存取。如欲達(dá)成多個行程相互合作完成任務(wù),最起碼必須使這些行程的工作時序必須一致才行。雖然系統(tǒng)上有多個行程執(zhí)行當(dāng)中,但同一時間CPU僅能選擇執(zhí)行其中一個行程;也就是說,先天上的因素這些行程並不可能達(dá)到時序的同步。這就是我們必須將此先天上不同步的因素,採取某些措施使其達(dá)成類似時序同步的功能,這就所謂『同步化』(Synchronization)的問題。35行程的同步系統(tǒng)中同時存在著許多行程在執(zhí)行當(dāng)中,為了達(dá)成某些system(constchar*string)Executesystemcommand
36system(constchar*string)Exec#include<stdlib.h>#include<stdio.h>intmain(){printf("Runningpswithsystem\n");system("ps-ax");printf("Done.\n");exit(0);}37#include<stdlib.h>2#include<stdlib.h>#include<stdio.h>intmain(){printf("Runningpswithsystem\n");system("ps-ax&");printf("Done.\n");exit(0);}38#include<stdlib.h>3Execfamilyintexecl(constchar*path,constchar*arg,...);
intexeclp(constchar*file,constchar*arg,...);
intexecle(constchar*path,constchar*arg
,...,char*const
envp[]);
intexecv(constchar*path,char*const
argv[]);
intexecvp(constchar*file,char*const
argv[]);Theexecfamilyoffunctionsreplacesthecurrentprocessimagewithanewprocessimage.39Execfamilyintexecl(constcMeaningofdifferentletters:l:needsalistofarguments.v:needsanargv[]vector(landvaremutuallyexclusive).e:needsanenvp[]array.p:needsthePATHvariabletofindtheexecutablefile40Meaningofdifferentletters:5#include<stdio.h>#include<unistd.h>intmain(){printf("Callingexecl...nn");execl("/bin/cat","cat","./example-1.c",NULL);printf("Uselesscalltoprintf");}41#include<stdio.h>6行程管理已進(jìn)入執(zhí)行階段的程式稱之為『行程』(Process,或程序)。簡單的說,程序即是執(zhí)行中的程式。行程在執(zhí)行當(dāng)中會隨著需要產(chǎn)生(fork)其他子行程(ChildProcess),並給於子行程執(zhí)行生命及執(zhí)行工作項(xiàng)目。既然『行程』是執(zhí)行中的獨(dú)立程式,當(dāng)他被執(zhí)行時,所有主機(jī)系統(tǒng)的資源都可被它所存取,至於控制主機(jī)資源的程度,則在於該行程的權(quán)限等級如何,而與系統(tǒng)中多少行程無關(guān)。42行程管理已進(jìn)入執(zhí)行階段的程式稱之為『行程』(Process行程命令建立(fork):行程可以執(zhí)行fork系統(tǒng)呼叫,來產(chǎn)生另一個子行程(ChildProcess),而產(chǎn)生行程者則稱為『父行程』(ParentProcess)。行程必定是由另一個行程所產(chǎn)生,而無法自行產(chǎn)生。產(chǎn)生子行程的原因可以接發(fā)生特殊原因或接受命令。43行程命令建立(fork):行程可以執(zhí)行fork系統(tǒng)呼叫fork()isdefinedat<unistd.h>#include<stdio.h>#include<unistd.h>intmain(){printf("HelloWorld\n");fork();printf("GoodbyeCruelWorld\n");}44fork()isdefinedat<unistd.h『行程』的特性
EachhasauniquePID.SomespecialPIDs:0:scheduler1:init2:pagedaemon45『行程』的特性10getpid()willreturncallingprocess’spid.#include<stdio.h>#include<unistd.h>intmain(){fork();printf("\nHello,IamtheprocesswithID=%d\n",getpid());return0;}46getpid()willreturncallingpParent/childprocess當(dāng)使用者以fork系統(tǒng)呼叫產(chǎn)生一個新的處理程序(process),這個處理程序會承襲其親代(parent)的特性(code,stack…)。此時叫用fork系統(tǒng)呼叫的那個處理程序稱為父處理程序,而衍生(fork)出的那個處理程序即為子處理程序(childprocess)。
47Parent/childprocess當(dāng)使用者以fork#include<stdio.h>#include<unistd.h>intmain(){intpid,num=2;printf("HelloWorld\n");pid=fork();if(pid!=0)printf("I'mtheFatherandmyson'sPIDis%d,num=%d\n",pid,num);elseprintf("I'mtheSon,num=%d\n",num);printf("GoodbyeCruelWorld\n");}fatherreturnchild'spid,childprocessreturn0,failreturn-148#include<stdio.h>13getppid()willreturnfather’spidofcallingprocess.#include<unistd.h>#include<stdlib.h>#include<stdio.h>intmain(void){
pid_tpid;if((pid=fork())>0)printf("Iamtheparent,mypid=%u,mychild’spid=%u\n",getpid(),pid);elseif(pid==0)printf("Iamthechild,mypid=%u,myparent’spid=%u\n",getpid(),getppid());else{perror("fork");return1;}return0;}49getppid()willreturnfather’s行程的基本生命週期(圖)50行程的基本生命週期(圖)15行程的生命週期(1)執(zhí)行狀態(tài)(RunningState):行程已佔(zhàn)有CPU,CPU正在執(zhí)行該行程。(2)預(yù)備狀態(tài)(ReadyState):行程正準(zhǔn)備給CPU執(zhí)行,並已取得執(zhí)行中所需的資源。(3)等待狀態(tài)(WaitState):行程在CPU執(zhí)行當(dāng)中可能欠缺某些資源或逾時而退出,並處於等待索取其他資源狀態(tài);當(dāng)行程取得所需資源或時間到達(dá),則可進(jìn)入預(yù)備狀態(tài),隨時接受CPU處理。51行程的生命週期(1)執(zhí)行狀態(tài)(RunningState)行程的生命週期(4)停止?fàn)顟B(tài)(StopState):行程已完成執(zhí)行,並等待被撤銷。(5)死結(jié)狀態(tài)(DeadlockState):行程等待可能一個永遠(yuǎn)無法得到的資源,而繼續(xù)無止境的等待。52行程的生命週期(4)停止?fàn)顟B(tài)(StopState):行程生命週期歸納如下一個父行程呼叫fork()系統(tǒng)呼叫產(chǎn)生了一個子行程,並給予該行程的生命(譬如,植入vi執(zhí)行程式),子行程進(jìn)入『預(yù)備』佇列,等待被CPU執(zhí)行。當(dāng)該行程取得CPU執(zhí)行之後,如果在『時間片段』中未能執(zhí)行完畢的話,則可能被踢出到預(yù)備狀態(tài),等待下一次被執(zhí)行的機(jī)會;也有可能在執(zhí)行當(dāng)中欠缺某些資源而轉(zhuǎn)換到『等待』狀態(tài),等待取得其他資源;也有可能在時間片段裡執(zhí)行完畢,而變遷到『停止』狀態(tài)。53行程生命週期歸納如下一個父行程呼叫fork()系統(tǒng)呼叫產(chǎn)行程的生命週期同一時間也許會有多個行程進(jìn)入同一狀態(tài)底下,等待改變進(jìn)入另一個狀態(tài)或者被選擇執(zhí)行。因此,系統(tǒng)針對每一種狀態(tài)建立一個管理制度,才能由眾多行程中選擇一個再改變其狀態(tài);一般簡單的做法,即是建立一個佇列(Queue),已先進(jìn)先出的方法,讓先進(jìn)入該狀態(tài)的行程,能先進(jìn)入下一個狀態(tài)。54行程的生命週期同一時間也許會有多個行程進(jìn)入同一狀態(tài)底下,等待改變行程狀態(tài)的因素可區(qū)分為行程命令與系統(tǒng)環(huán)境兩大因素。前者為使用者(或系統(tǒng))所下達(dá)的命令;後者為行程因系統(tǒng)環(huán)境的改變,而變遷其狀態(tài)。55改變行程狀態(tài)的因素可區(qū)分為行程命令與系統(tǒng)環(huán)境兩大因素。20行程命令撤銷(Destroy):無論行程是正常停止或非正常停止,都須經(jīng)過撤銷命令來取回行程所占用的記憶體空間,或其他所持有的資源。阻斷(Block):行程從執(zhí)行狀態(tài)轉(zhuǎn)變到等待狀態(tài)。停止(Stop):在正常情況下,行程執(zhí)行完畢之後,會進(jìn)入停止?fàn)顟B(tài)並等待系統(tǒng)撤銷它。但某些情況,系統(tǒng)也可以執(zhí)行停止命令,來停止某一個執(zhí)行當(dāng)中的行程,並使其進(jìn)入停止?fàn)顟B(tài)。喚醒(Wakeup):行程由等待狀態(tài)轉(zhuǎn)換到執(zhí)行狀態(tài)。56行程命令撤銷(Destroy):無論行程是正常停止或非正常停Processterminationexitfunction:Toterminateaprocesswithanexitcode.Noticethatthisisalibrary,and_exitisasystemcall.Normaltermination:Themainprogramreturns.Theprogramcallsexit.Theprogramcalls_exit.Abnormaltermination:Theprogramcallsabort.Theprogramcatchesasignal.57Processterminationexitfunct#include<unistd.h>#include<stdlib.h>#include<stdio.h>intmain(void){pid_tpid;pid=fork();if(pid>0){printf("daemononduty!\n");exit(0);}elseif(pid<0){printf("Can'tfork!\n");exit(-1);}for(;;){printf("Iamthedaemon!\n");sleep(3);/*dosomethingyourownhere*/}}58#include<unistd.h>23#include<sys/types.h>#include<unistd.h>#include<stdio.h>intmain(){pid_tpid;char*message;intn;printf("forkprogramstarting\n");pid=fork();switch(pid){case-1:perror("forkfailed");exit(1);case0:message="Thisisthechild";n=5;break;default:message="Thisistheparent";n=3;break;}for(;n>0;n--){puts(message);sleep(1);}exit(0);}59#include<sys/types.h>24ProcessterminationWhentheparentterminatesbeforethechild,theinitprocessbecomestheparentofthisorphanprocess.Whenthechildterminatesbeforetheparent,thechildbecomesazombie(defunct).Azombieisadeadentity,butnotcompletelydead,.:Thechildmustleavesufficientinformationintheprocesstablesothatlaterwhenitsparentwantstofetchitsstatus,itisabletodoso,Theinformationazombiekeepsintheprocesstableincludesprocessid,terminationstatus,andaccountinginformation.60ProcessterminationWhenthepa#include<unistd.h>#include<stdlib.h>#include<stdio.h>intmain(void){pid_tpid;inti=0;pid=fork();if(pid>0){printf("thisisparent,mypid=%d\n",getpid());printf("parentexitsfirst\n");exit(0);}sleep(1);for(i=0;i<=10000;i++);printf("thisisson,myparent’spid=%d\n",getppid());}61#include<unistd.h>26#include<unistd.h>#include<stdlib.h>#include<stdio.h>intmain(void){pid_tpid;inti=0,j=0;pid=fork();if(pid==0){printf("thisisson,mypid=%d\n",getpid());printf("Sonexitsfirst\n");exit(0);}sleep(1);for(i=0;i<=10000;i++)for(j=0;j<=10000;j++);printf("parentexitsnow\n");}62#include<unistd.h>27waitfamilieswaitAprocesscancallwaittowaitforthechildprocesstocomplete.Thewaitfunctionprovidesaintegerbufferforreceivingtheterminationstatus.Thewaitfunctionwillblockifnochildisavailable.Thereturnvalueistheprocessidofthechildprocess.waitpidAprocesscancallwaittowaitforaparticularchildprocesstocomplete.Thewaitpidcanbenon-blocking.63waitfamilieswaitAprocessAvoidzombieprocesspid_twait(int*stat_loc)Thewait()callisusedtotellaprocesstowaitforoneofhischildstoendbeforegoingonwithit'sowntask.wait()takestheadressofanint,inwichitputstheexitstatusofthechilditwaitedfor(toknowwhatyoucandowiththatstatus,lookat'manwait')64Avoidzombieprocesspid_twait#include<stdio.h>#include<unistd.h>intmain(){fork();printf("\nHello,IamtheprocesswithID=%d\n",getpid());wait();return0;}65#include<stdio.h>30#include<stdio.h>#include<stdlib.h>#include<sys/types.h>#include<unistd.h>#include<sys/wait.h>intmain(intargc,char*argv[],char*env[]){pid_tmy_pid,parent_pid,child_pid;intstatus;/*getandprintmypidandmyparent'spid.*/my_pid=getpid();parent_pid=getppid();printf("Parent:mypidis%d\n",my_pid);printf("Parent:myparent'spi
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 《100 以內(nèi)的加法和減法(二)-不進(jìn)位加》(說課稿)-2024-2025學(xué)年二年級上冊數(shù)學(xué)人教版
- 13《人物描寫一組》第二課時《巧用多種方法寫“活”身邊人物》說課稿-2023-2024學(xué)年五年級語文下冊統(tǒng)編版
- Revision Being a good guest Period 2(說課稿)-2024-2025學(xué)年人教PEP版(2024)英語三年級上冊
- 2024秋九年級語文上冊 第五單元 18《懷疑與學(xué)問》說課稿 新人教版
- Unit5 What will you do this weekend?Lesson25(說課稿)-2023-2024學(xué)年人教精通版英語四年級下冊
- 5 國家機(jī)構(gòu)有哪些 第三課時 《國家機(jī)關(guān)的產(chǎn)生》 說課稿-2024-2025學(xué)年道德與法治六年級上冊統(tǒng)編版
- 《 關(guān)注新詞新語讓語言鮮活生動》說課稿 2024-2025學(xué)年統(tǒng)編版高中語文必修上冊
- 1~5的認(rèn)識和加減法《第幾》(說課稿)-2024-2025學(xué)年一年級上冊數(shù)學(xué)人教版
- Module 9 Unit 1 It's winter.(說課稿)-2024-2025學(xué)年外研版(一起)英語二年級上冊
- 1《水到哪里去了》說課稿-2023-2024學(xué)年科學(xué)五年級下冊冀人版
- 西安經(jīng)濟(jì)技術(shù)開發(fā)區(qū)管委會招聘筆試真題2024
- 2025屆浙江省高三歷史選考總復(fù)習(xí)模擬測試(八)歷史試題(含答案)
- 六年級2025寒假特色作業(yè)
- 2025年江蘇轄區(qū)農(nóng)村商業(yè)銀行招聘筆試參考題庫含答案解析
- 人教版六年級數(shù)學(xué)下冊完整版教案及反思
- 少兒財商教育講座課件
- (八省聯(lián)考)云南省2025年普通高校招生適應(yīng)性測試 物理試卷(含答案解析)
- 2025藥劑科工作人員工作計劃
- 春節(jié)節(jié)后安全教育培訓(xùn)
- 2025年新高考數(shù)學(xué)一輪復(fù)習(xí)第5章重難點(diǎn)突破02向量中的隱圓問題(五大題型)(學(xué)生版+解析)
- 水土保持方案投標(biāo)文件技術(shù)部分
評論
0/150
提交評論