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

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

/********************************************************************
filename: daemon.c
created: 2013-07-17
author: firehood
purpose: daemon implement
*********************************************************************/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h> 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<nAppCount;i++)
{
usleep(APP_INFO_Array[i].start_interval*1000);
if(APP_INFO_Array[i].cmdline)
{
char* argv[3] ={0};
argv[0] = strrchr(APP_INFO_Array[i].app_path,'/')+1;
argv[1] = APP_INFO_Array[i].cmdline;
argv[2] = NULL;
APP_pid[APP_INFO_Array[i].id] = ExecuteApp(APP_INFO_Array[i].app_path,argv);
}
else
{
APP_pid[APP_INFO_Array[i].id] = ExecuteApp(APP_INFO_Array[i].app_path,NULL);
}
printf("Daemon: %s is running pid = [%d]\n",APP_INFO_Array[i].app_path,APP_pid[APP_INFO_Array[i].id]);
} int status;
while(1)
{
pid_t ret_pid = wait(&status);
if(ret_pid == -1)
{
printf("Daemon: all app are exited!\n");
break;
}
printf("Daemon: app [%d] returns\n",ret_pid);
if(!WIFEXITED(status))
{
printf("Daemon: app [%d] terminated abnormally\n",ret_pid);
}
else
{
printf("Daemon: app [%d] exited with code[%d]\n",ret_pid,WEXITSTATUS(status));
}
int app_id = GetAppIdByPid(ret_pid);
if(app_id>=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<APP_ID_MAX;i++)
{
if(pid == APP_pid[i] & i != APP_ID_RTMP)
{
return i;
}
}
return -1;
} int ExecuteApp(const char *app_path,const char *argv[])
{
if(app_path == NULL)
{
return 0;
} pid_t pid = fork(); int ret;
switch(pid)
{
case -1:
perror("fork failed");
exit(-1);
case 0 :
ret = execv(app_path,argv);
printf("start app[%s] failed, ret= [%d]\n",app_path,ret);
break;
default:
break;
} return pid;
}

linux创建进程和等待进程退出的更多相关文章

  1. Linux系统编程——特殊进程之僵尸进程

    僵尸进程(Zombie Process) 进程已执行结束,但进程的占用的资源未被回收.这种进程称为僵尸进程. 在每一个进程退出的时候,内核释放该进程全部的资源.包含打开的文件.占用的内存等. 可是仍然 ...

  2. python开发进程:共享数据&进程池

    一,共享数据 展望未来,基于消息传递的并发编程是大势所趋 即便是使用线程,推荐做法也是将程序设计为大量独立的线程集合 通过消息队列交换数据.这样极大地减少了对使用锁定和其他同步手段的需求, 还可以扩展 ...

  3. Linux系统编程之进程控制(进程创建、终止、等待及替换)

    进程创建 在上一节讲解进程概念时,我们提到fork函数是从已经存在的进程中创建一个新进程.那么,系统是如何创建一个新进程的呢?这就需要我们更深入的剖析fork函数. 1.1 fork函数的返回值 调用 ...

  4. LINUX编程学习笔记(十四) 创建进程与 父子进程内存空间

    1什么是进程:进程是一个执行中的程序 执行的程序: 代码->资源->CPU 进程有很多数据维护:进程状态/进程属性 所有进程属性采用的一个树形结构体维护 ps  -a//所有进程 ps - ...

  5. linux进程管理之进程创建

    所谓进程就是程序执行时的一个实例. 它是现代操作系统中一个很重要的抽象,我们从进程的生命周期:创建,执行,消亡来分析一下Linux上的进程管理实现. 一:前言 进程管理结构; 在内核中,每一个进程对应 ...

  6. Linux系统编程之--守护进程的创建和详解【转】

    本文转载自:http://www.cnblogs.com/mickole/p/3188321.html 一,守护进程概述 Linux Daemon(守护进程)是运行在后台的一种特殊进程.它独立于控制终 ...

  7. linux系统编程:守护进程详解及创建,daemon()使用

    一,守护进程概述 Linux Daemon(守护进程)是运行在后台的一种特殊进程.它独立于控制终端并且周期性地执行某种任务或等待处理某些发生的事件.它不需要用户输入就能运行而且提供某种服务,不是对整个 ...

  8. Linux之进程的等待与其内核实现解析

    进程通过fork产生子进程,进程也会死亡,进程退出的时候将会进行内核清理,释放所有进程的资源,资源包括:内存资源,文件资源,信号量资源,共享内存资源,或者引用计数减一,或者彻底释放.     不过进程 ...

  9. linux进程管理之进程创建(三)

    在linux系统中,许多进程在诞生之初都与其父进程共同用一个存储空间.但是子进程又可以建立自己的存储空间,并与父进程“分道扬镳”,成为与父进程一样真正意义上的进程. linux系统运行的第一个进程是在 ...

随机推荐

  1. 全网最全ASP.NET MVC 教程汇总

    全网最全ASP.NET MVC 教程汇总 MVC架构已深得人心,微软也不甘落后,推出了Asp.net MVC.小编特意整理博客园乃至整个网络最具价值的MVC技术原创文章,为想要学习ASP.NET MV ...

  2. SQL Server 有关EXCEPT和INTERSECT使用

    熟练使用SQL Server各种使用会带来多大的方便查询.今天介绍EXCEPT和INTERSECT.请注意,这只是语法SQL Server 2005和以上版本支持. EXCEPT它指的是存在于所述第一 ...

  3. Instruments-Automation: 通过命令行执行测试用例

    为了实现该脚本的自己主动定时执行.我们需要开始在命令行和脚本,详细代码如下所示的: instruments -t /Applications/Xcode.app/Contents/Applicatio ...

  4. EF中的EntityState几个状态的说明

    之前使用EF,我们都是通过调用SaveChanges方法把增加/修改/删除的数据提交到数据库,但是上下文是如何知道实体对象是增加.修改还是删除呢?答案是通过EntityState枚举来判断的,我们看一 ...

  5. javascript中对字符串的操作总结

    原文:javascript中对字符串的操作总结 没听过一句话吗?程序员的世界,不处理字符串就是处理数组.这是群里的一位前辈和我说的,显然这和我之前理解的DOM是javascript的核心的不同的,看了 ...

  6. [转载]cookie

    cookie概述 在上一节,曾经利用一个不变的框架来存储购物栏数据,而商品显示页面是不断变化的, 尽管这样能达到一个模拟 全局变量的功能,但并不严谨.例如在导航框架页面内右击,单击快捷菜单中的[刷新] ...

  7. iOS开发分析&quot;秘密&quot;App内容页面的效果(两)

    @我前几天写的"秘密"的Cell制品的效果,想要的效果还是有差距,并且扩展性非常不好,于是重写封装,把总体视图都放到scrollView中,基本是和secret app 一模一样的 ...

  8. Codeforces 328B-Sheldon and Ice Pieces(馋)

    B. Sheldon and Ice Pieces time limit per test 1 second memory limit per test 256 megabytes input sta ...

  9. jQuery实现发送验证码倒计时60秒

    前端HMTL: <div class="form_box"> <div class="line mb40"> <div class ...

  10. MVC验证02-自定义验证规则、邮件验证

    原文:MVC验证02-自定义验证规则.邮件验证 本文体验MVC自定义验证特性,来实现对邮件的验证.对于刚写完的自定义验证特性,起初只能支持后端验证.如果要让前端jquery支持,还必须对jquery的 ...