




已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
ACE的进程管理 2007年08月09日 星期四 21:03The ACE Process Management Class RelationshipsACE_Process使用ACE_Process类派生进程有两个步骤:1)创建一个新的ACE_Process_Options对象,为新的子进程指定合乎你需要的属性;2)用ACE_Process:spawn()方法派生新进程。#include #include #include int ACE_TMAIN(int argc, ACE_TCHAR* argv) if (argc 1) ACE_DEBUG(LM_DEBUG, ACE_TEXT(%P): I am a child process.n); return 0; else ACE_Process_Options options; mand_line(%s %d, argv0, 1); ACE_Process child; pid_t pid = child.spawn(options); if (pid = -1) ACE_ERROR_RETURN(LM_ERROR, ACE_TEXT(%pn), ACE_TEXT(spawn), -1); ACE_DEBUG(LM_DEBUG, ACE_TEXT(%P): Spawn a child process (%d).n), pid); ACE_exitcode status; child.wait(&status); ACE_DEBUG(LM_DEBUG, ACE_TEXT(%P): Child process (%d) terminated with exit code %d.n), pid, status); return 0;ACE_Process_OptionsThe ACE_Process_Options对象负责携带新进程的各个选项。ACE_Process_ManagerACE_Process_Manager允许用户通过单次调用派生多个进程,并等待它们终止:#include #include static const int NCHILDREN = 5;int ACE_TMAIN(int argc, ACE_TCHAR *argv) if (argc 1) / Running as a child. ACE_OS:sleep(10); else / Running as a parent. / Get the processwide process manager. ACE_Process_Manager* pm = ACE_Process_Manager:instance(); / Specify the options for the new processes / to be spawned. ACE_Process_Options options; mand_line(ACE_TEXT(%s a), argv0); / Spawn two child processes. pid_t pidsNCHILDREN; pm-spawn_n(NCHILDREN, options, pids); / Destroy the first child. pm-terminate(pids0); / Wait for the child we just terminated. ACE_exitcode status; pm-wait(pids0, &status); / Get the results of the termination.#if !defined(ACE_WIN32) if (WIFSIGNALED(status) != 0) ACE_DEBUG(LM_DEBUG, ACE_TEXT(%d died because of a signal ) ACE_TEXT(of type %dn), pids0, WTERMSIG(status);#else ACE_DEBUG (LM_DEBUG, ACE_TEXT(The process terminated with exit code %dn), status);#endif /*ACE_WIN32*/ / Wait for all (only NCHILDREN - 1 left) of the / children to exit. pm-wait(0); return 0;你还可以登记事件处理器,让它们在子进程终止时被回调:#include #include #include static const int NCHILDREN = 5;class DeathHandler: public ACE_Event_Handlerpublic: DeathHandler () : count_(0) ACE_TRACE(ACE_TEXT(DeathHandler:DeathHandler); virtual int handle_exit(ACE_Process* process) ACE_TRACE(ACE_TEXT(DeathHandler:handle_exit); ACE_DEBUG (LM_DEBUG, ACE_TEXT(Process %d exited with exit code %dn), process-getpid (), process-return_value(); if (+count_ = NCHILDREN) ACE_Reactor:instance()-end_reactor_event_loop(); return 0; private: int count_;int ACE_TMAIN(int argc, ACE_TCHAR *argv) if (argc 1) / Running as a child. return 0; / Instantiate a process manager with space for / 10 processes. ACE_Process_Manager pm(ACE_Process_Manager:DEFAULT_SIZE, ACE_Reactor:instance (); / Create a process termination handler. DeathHandler handler; / Specify the options for the new processes to be spawned. ACE_Process_Options options; mand_line(ACE_TEXT (%s a), argv0); / Spawn two child processes. pid_t pidsNCHILDREN; pm.spawn_n(NCHILDREN, options, pids); / Register handler to be called when these processes exit. for (int i = 0; i run_reactor_event_loop(); return 0;ACE_Process_MutexACE_Process_Mutex:acquire();/获取互斥体ACE_Process_Mutex:release();/释放互斥体#include #include #include #include class GResourceUserpublic: GResourceUser(ACE_Process_Mutex &mutex) : gmutex_(mutex) ACE_TRACE(ACE_TEXT(GResourceUser:GResourceUser); void run (void) ACE_TRACE(ACE_TEXT(GResourceUser:run); int count = 0; while (count+ gmutex_.acquire(); ACE_ASSERT(result = 0); ACE_DEBUG(LM_DEBUG, ACE_TEXT(%P| %t) has the mutexn); / Access Global resource ACE_OS:sleep(1); result = this-gmutex_.release(); ACE_ASSERT(result = 0); ACE_OS:sleep(1); / Give other process a chance. private: ACE_Process_Mutex &gmutex_;int ACE_TMAIN (int argc, ACE_TCHAR *argv) if (argc 1) / Run as the child. / Create or get the global mutex. ACE_Process_Mutex mutex(GlobalMutex); GResourceUser acquirer(mutex); acquirer.run(); else / Run as the parent. ACE_Process_Options options; mand_line(%s a, argv0); ACE_Process processa, processb; pid_t pida = processa.spawn(options); pid_t pidb = processb.spawn(options); ACE_DEBUG (LM_DEBUG, ACE_TEXT(Spawned processes; pids %d:%dn), pid
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 你的能量超乎你想象课件
- 邮政外包网点管理办法
- 郑州摆地摊管理办法
- 非正常动物管理办法
- 交通道路秩序管理办法
- 企业生产安全培训app课件
- 淘宝新闻发布管理办法
- 社会公益资金管理办法
- 瑞安遣返人员管理办法
- 矿山合法枪支管理办法
- 专职安全生产管理人员综合类C证习题(含答案)
- 铝单板幕墙专项方案
- 和政秧歌的发展历史、表演形式及音乐特点,舞蹈论文
- GB/T 42062-2022医疗器械风险管理对医疗器械的应用
- WB/T 1066-2017货架安装及验收技术条件
- 第六节-食品新资源的开发-课件
- 复变函数-西安交大版课件
- 南方医大护理学导论教案
- 隧道断面施工放样测量记录表
- 小说写作的基础知识短篇小说的写作
- (中职中专)《电控发动机维修》全套教学设计全书电子教案整本书教案合集1-22章全
评论
0/150
提交评论