Unix下的守护进程
来源:互联网 发布:淘宝充值可以代付吗 编辑:程序博客网 时间:2024/05/22 01:41
此程序在log文件中不停的输出当前系统时间。
#include <stdio.h> #include <signal.h> #include <sys/file.h> #include <sys/types.h>#include <unistd.h>#include <stdio.h>#include <time.h>void main(int argc,char **argv){FILE *fileLog;char strTMP[512];time_t sysTime;struct tm *localTime;int childpid,childpid2,fd,fdtablesize;/* 忽略终端 I/O信号,STOP信号 */signal(SIGTTOU,SIG_IGN);signal(SIGTTIN,SIG_IGN);signal(SIGTSTP,SIG_IGN); signal(SIGHUP ,SIG_IGN);/* 父进程退出,程序进入后台运行 */childpid = fork();if(childpid<0){printf("创建子进程失败!");exit(1);}else if(childpid == 0){/* 创建一个新的会议组 */ if(setsid()<0){exit(1);}/* 子进程退出,第二子进程没有控制终端了 */ childpid2=fork();if(childpid2<0){exit(1);}else if(childpid2==0){if(chdir("./tmp")==-1){exit(1);/* 改变当前工作目录 */ }/* 关闭打开的文件描述符,包括标准输入、标准输出和标准错误输出 */ for (fd = 0, fdtablesize = getdtablesize(); fd < fdtablesize; fd++) close(fd);umask(0);/*重设文件创建掩模 */ signal(SIGCHLD,SIG_IGN);/* 忽略SIGCHLD信号(子进程退出时会发送此信号给父进程) */ while(1==1){fileLog = fopen("./example.log","a");if(fileLog == NULL){exit(1);}time(&sysTime);localTime = localtime(&sysTime);fprintf(fileLog,"%s log.........\n",asctime(localTime));fclose(fileLog);sleep(1);}fclose(fileLog);}else{exit(0);}}else{printf("父进程结束,子进程id:[%d]\n",childpid);exit(0);}}
编译makefile:
example:example.o xlc_r -o example example.oexample.o:example.c xlc_r -c example.c
- Unix下的守护进程
- Unix下的守护进程
- unix下的守护进程
- UNIX下守护进程(Daemon)的编写
- 详解unix下的C守护进程
- UNIX的守护进程(服务器)
- Unix守护进程列表
- unix 守护进程
- unix 守护进程
- Linux下的守护进程
- Linux下的守护进程
- linux下的守护进程
- Linux下的守护进程
- Linux下的守护进程
- Linux下的守护进程
- Linux下的守护进程
- Linux下的守护进程
- linux下的守护进程
- !!n的用法
- Google: 'We'll track EVERY task on EVERY data center server
- CREATE SEQUENCE 官方文档
- 内联函数的定义及说明
- 获取 RRD 文件的信息
- Unix下的守护进程
- 涉及sdu数据同步的代码
- 中文编码方式总结
- 最长重复子串
- linux 环境变量 路径
- 设计模式——观察者模式
- J2EE (一) 轻松入门
- jsp九大隐式对象——out隐式对象
- 支持中文的zip压缩和解压,修改自带jdk ZipInputStream、ZipOutputStream、ZipEntry代码