基本信息
源码名称:android 双线程守护
源码大小:1.26M
文件格式:.zip
开发语言:C/C++
更新时间:2016-09-04
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
安卓 双线程守护
安卓 双线程守护
| int init_daemon(void) { | |
| pid_t pid; | |
| int i; | |
| pid = fork(); | |
| if (pid > 0) { //第一步,结束父进程,使得子进程成为后台 | |
| exit(0); | |
| } else if (pid < 0) { | |
| return -1; | |
| } | |
| // 第二步建立一个新的进程组,在这个新的进程组中,子进程成为这个进程组的首进程,以使该进程脱离所用终端 | |
| setsid(); | |
| // 再次新建一个子进程,退出父进程,保证该进程不是进程组长,同时让该进程无法再打开一个新的终端 | |
| pid = fork(); | |
| if (pid > 0) { | |
| exit(0); | |
| } else if (pid < 0) { | |
| return -1; | |
| } | |
| //第三步:关闭所有从父进程继承的不再需要的文件描述符 | |
| // for (i = 0; i < NOFILE; close(i )); | |
| //第四步:改变工作目录,使得进程不与任何文件系统联系 | |
| chdir("/"); | |
| //第五步:将文件屏蔽字设置为0 | |
| umask(0); | |
| //第六步:忽略SIGCHLD信号 | |
| signal(SIGCHLD, SIG_IGN); | |
| return 0; | |
| } | |
| int main(int sdk, char *arg[]) /* 无参数形式 */ | |
| { | |
| LOGE("pid=%d", getpid()); | |
| LOGE("arg1=%s", arg[1]); | |
| sdkInt = atoi(arg[1]); | |
| string tmp1(startServiceName); | |
| string tmp2(startServiceNameWithSDK); | |
| string tmp4(className); | |
| dest1 = tmp1 tmp4; | |
| dest2 = tmp2 tmp4; | |
| init_daemon(); | |
| time_t now; | |
| while (1) { | |
| sleep(15); | |
| startService(); | |
| time(&now); | |
| LOGE("Snow is running, pid=%d %s/t/n", getpid() , ctime(&now)); | |
| } | |
| } |