资源描述:
《Ch4 Linux系统程序设计 进程控制和进程间通信》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、Ch4LinuxSystemProgramming–Process&IPCJianjianSONGSoftwareInstitute,NanjingUniversityOct,2004ContentProcess&processenvironmentProcessControlProcessidentifier,fork,exec…ProcessrelationshipSignalInter-processcommunication(IPC)Pipe,FIFOsemaphore,sharedmemory,messagequeueDaemo
2、nThread1.Process&ProcessEnvironmentWhatisaprocess?ProgramandprocessProcess:anaddressspacewithoneormorethreadsexecutingwithinthataddressspace,andtherequiredsystemresourcesforthosethreads.(SUSv3)ThestartupofaprocessSystemcall“fork”Processresourcesstructtask_structSystemspac
3、estack…Systemcall“exec”TheentryofCprogramsSystemstackTheentryofCprogramscrt0.occ/ldmainfunctionfunctionprototype:intmain(intargc,char*argv[]);TheterminationofaprocessFivewaysofterminatingaprocessNormalterminationReturnfrom“main”functionCall“exit”functionCall“_exit”functio
4、nAbnormalterminationCall“abort”functionTerminatedbyasignalexit&_exitfunctionsFunctionprototype:#includevoidexit(intstatus);#includevoid_exit(intstatus);ExitstatusDifference_exitiscorrespondingtoasystemcall,while“exit”isalibraryfunction._exitterminateap
5、rocessimmediately.ExithandleratexitfunctionRegisterafunctiontobecalledatnormalprogramtermination.Prototype:#includeintatexit(void(*function)(void));on_exitfunctionExampleThememorymapofaCprogramTextsegment(codesegment)DatasegmentInitializeddataUninitializeddataHe
6、apStackCommandlineargumentsmainfunctionintmain(intargc,char*argv[]);Example:Theimplementationofecho(1)CommandlineoptionStandardusagegetoptfunctiongetoptfunctionPrototype:intgetopt(intargc,char*constargv[],constchar*optstring);externchar*optarg;externintoptind,opterr,optop
7、t;Question:getopt(argc,argv,“if:lr”);Programexample(P104,inBLP)EnvironmentvariablesEnvironmenttableEnvironmentpointerexternchar**environ;putenv&getenvfunctionsGet,setorunsetanenvironmentvariable#includechar*getenv(constchar*name);intputenv(char*string);intsetenv
8、(constchar*name,constchar*value,intoverwirte);voidunsetenv(constchar*name);SharedobjectSharedobj