环境:
主机:Fedora12
目标板:SC6410
目标板LINUX内核版本:2.6.36
实现功能:
做的一个嵌入式板子开机会自启动一个程序,但发现它工作数天后会退出。检查内存使用并没有泄漏,于是编写了一个守护进程来不断检查程序是否运行,没运行则运行它,这是一个折衷的办法。
说明:
需要运行的程序是AlarmInterface,位于目录/rf/下面。我做了一个脚本DuiJiang来启动这个AlarmInterface,并在脚本中添加了触摸屏支持。也就是说启动DuiJiang就可以启动AlarmInterface。检测程序是否运行的方法是通过ps -w|grep AlarmInterface指令获得AlarmInterface的进程,然后保存在一个文件中.检查AlarmInterface进程是否运行即可判断程序是否运行.
驱动源代码:
daemon_service.c:
-   
 -   
 - www.linuxidc.com
 -   
 - #include <stdio.h>   
 - #include <unistd.h>   
 - #include <sys/types.h>   
 - #include <sys/stat.h>   
 - #include <fcntl.h>   
 - #include <syslog.h>   
 -   
 -   
 - #define NAME "AlarmInterface -qws"   
 -   
 - #define NAME_FIND "AlarmInterface"   
 -   
 - #define DIR_OUT_FILE "/rf/out"   
 -   
 - #define RUN_NAME "DuiJiang &"   
 -   
 -   
 -   
 -   
 -   
 -   
 - int daemon(int nochdir,int noclose)  
 - {  
 -     pid_t pid;  
 -   
 -       
 -     pid = fork();  
 -       
 -     if (pid < 0)  
 -     {  
 -         perror("fork");  
 -         return -1;  
 -     }  
 -       
 -     if (pid != 0)  
 -     {  
 -         exit(0);  
 -     }  
 -       
 -     pid = setsid();  
 -     if (pid < -1)  
 -     {  
 -         perror("set sid");  
 -         return -1;  
 -     }  
 -       
 -     if (!nochdir)  
 -     {  
 -         chdir("/");  
 -     }  
 -       
 -       
 -     if (!noclose)  
 -     {  
 -         int fd;  
 -         fd = open("/dev/null",O_RDWR,0);  
 -         if (fd != -1)  
 -         {  
 -             dup2(fd,STDIN_FILENO);  
 -             dup2(fd,STDOUT_FILENO);  
 -             dup2(fd,STDERR_FILENO);  
 -             if (fd > 2)  
 -             {  
 -                 close(fd);  
 -             }  
 -         }  
 -     }  
 -       
 -     umask(0027);  
 -   
 -     return 0;  
 - }  
 -   
 -   
 -   
 -   
 -   
 - int match(char *src,char *dst,int len)  
 - {  
 -     int i = 0;  
 -     int j = 0;  
 -     int size_dst = 0;  
 -   
 -       
 -     size_dst = strlen(dst);  
 -       
 -     if (size_dst > len)  
 -     {  
 -         return 0;  
 -     }     
 -       
 -     for (i = 0;i < len;i++)  
 -     {  
 -         for (j = 0;j < size_dst;j++)  
 -         {  
 -             if (src[i + j] != dst[j])  
 -             {  
 -                 break;  
 -             }  
 -         }  
 -         if (j == size_dst)  
 -         {  
 -             return 1;  
 -         }  
 -     }  
 -   
 -     return 0;  
 - }  
 -   
 - int main(int argc,char *argv[])  
 - {  
 -     int fd = 0;  
 -     char buf[100];  
 -   
 -       
 -     daemon(0,0);  
 -   
 -     while (1)  
 -     {  
 -           
 -         openlog(argv[0],LOG_CONS|LOG_PID,LOG_USER);  
 -           
 -           
 -           
 -         system("touch "DIR_OUT_FILE);  
 -           
 -         system("ps -w|grep "NAME_FIND" >> "DIR_OUT_FILE);  
 -           
 -         fd = open(DIR_OUT_FILE,O_CREAT|O_RDONLY,0777);  
 -           
 -         memset(buf,0,100);  
 -           
 -         read(fd,buf,100);  
 -           
 -         if (match(buf,NAME,90))  
 -         {  
 -             syslog(LOG_INFO,"jdh success!!!!!!!!!!");  
 -         }  
 -         else  
 -         {  
 -             syslog(LOG_INFO,"jdh fail!!!!!!!!!!");  
 -               
 -             system(RUN_NAME);  
 -         }  
 -   
 -           
 -         sleep(5);  
 -           
 -         system("rm "DIR_OUT_FILE);  
 -           
 -           
 -         sleep(55);  
 -     }  
 -   
 -       
 -     closelog();  
 -   
 -     return 0;  
 - }  
 
守护进程每分钟检测一次,用tail -f /var/log/messages可以看到守护进程输出的信息。