博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux创建进程和等待进程退出
阅读量:7120 次
发布时间:2019-06-28

本文共 2354 字,大约阅读时间需要 7 分钟。

         在WIN32下,在一个进程里我们可以使用CreateProcess()创建一个进程,然后通过调用WaitForSingleObect(), WaitForMultipleObject()等待进程退出。那么在linux下该如何实现呢?

          以下的代码实现了一个daemon程序, daemon程序负责系统启动其它所有App,当其它应用出现异常退出的时候,daemon程序会重新启动它们。

/******************************************************************** filename:   daemon.ccreated:    2013-07-17author:     firehood purpose:    daemon implement*********************************************************************/ #include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
typedef enum APP_ID_{ APP_ID_MIN, APP_ID_RTMP = APP_ID_MIN, APP_ID_VIDTRANSFILT, APP_ID_SOS, // APP_ID_MAX,}APP_ID;typedef struct APP_INFO_{ APP_ID id; // app id const char* app_path; // app file path const char* cmdline; // app cmdline int start_interval; // waitting time before starting the app. (unit:ms)}APP_INFO;static APP_INFO APP_INFO_Array[] = { {APP_ID_RTMP, "/opt/exe/crtmpserver/sbin/crtmpserver", "/opt/exe/crtmpserver/etc/crtmpserver.lua" ,0}, {APP_ID_VIDTRANSFILT, "./VidTransFilt", NULL, 2000}, {APP_ID_SOS, "/opt/exe/sos", NULL, 5000},};pid_t APP_pid[APP_ID_MAX];int ExecuteApp(const char *app_path,const char *argv[]);int GetAppIdByPid(pid_t pid);int main(void){ /* Our process ID and Session ID */ pid_t pid, sid; /* Fork off the parent process */ pid = fork(); if (pid < 0) { exit(EXIT_FAILURE); } /* If we got a good PID, then we can exit the parent process. */ if (pid > 0) { exit(EXIT_SUCCESS); }#if 0 /* Change the file mode mask */ umask(0); /* Open any logs here */ /* Create a new SID for the child process */ sid = setsid(); if (sid < 0) { /* Log the failure */ exit(EXIT_FAILURE); } /* Change the current working directory */ if ((chdir("/")) < 0) { /* Log the failure */ exit(EXIT_FAILURE); } /* Close out the standard file descriptors */ close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO);#endif /* Daemon-specific initialization goes here */ printf("Daemon is running..\n"); int nAppCount = sizeof(APP_INFO_Array)/sizeof(APP_INFO); printf("Daemon: AppCount is [%d]\n",nAppCount); int i = 0; for(i =0; i
=0 && app_id!=APP_ID_RTMP) { // restart the app APP_pid[app_id] = ExecuteApp(APP_INFO_Array[app_id].app_path,APP_INFO_Array[app_id].cmdline); } } exit(EXIT_SUCCESS);}int GetAppIdByPid(pid_t pid){ int i = 0; for(i = 0; i

 

 

 

转载地址:http://toiel.baihongyu.com/

你可能感兴趣的文章
SoapUI Pro Project Solution Collection-DataSource(jdbc,excel)
查看>>
浅谈嵌入式软件的未来发展
查看>>
Dockerfile最佳实践(二)
查看>>
数学之美:两点之间最快的路径
查看>>
Ansible 详细用法部署安装
查看>>
CPU
查看>>
Spark的这些事&lt;二&gt;——几个概念
查看>>
Retrofit2.0使用
查看>>
网络01:双无线路由器无缝对接设置
查看>>
实现Android和PC之间的蓝牙通信
查看>>
rails将类常量重构到数据库对应的表中之二
查看>>
微软面试题:写程序找出二叉树的深度
查看>>
[Google Guava] 1.2-前置条件
查看>>
OEA框架 2.9 Pre-Alpha 源码公布
查看>>
我的MYSQL学习心得(十三) 权限管理
查看>>
Spring Data —— 完全统一的API?
查看>>
[翻译] JTBorderDotAnimation
查看>>
浏览器兼容性小记-DOM篇(二)
查看>>
091023 T GIX4 项目中的 智能部署 和 智能客户端
查看>>
Mondrian and OLAP
查看>>