用c++创建一个Linux上的Daemon程序,含SHELL管理脚本(一)
来源:互联网 发布:编程儿童产业 编辑:程序博客网 时间:2024/06/06 03:20
用c++创建一个Linux上的Daemon程序,含SHELL管理脚本(一)
本文提供了一个Daemon程序的简单实例,更多高级应用,请参考:
用c++创建一个Linux上的Daemon程序,含SHELL管理脚本(二)
用c++创建一个Linux上的Daemon程序,含SHELL管理脚本(三)
1、编写代码,命名为simple.c
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include <syslog.h>
#include <singnal.h>
#include <fcntl.h>
#define MAXFD 64
int main(void)
{
pid_t pid=fork();
if (pid > 0)
{
exit(0);
}
else if (pid < 0)
{
exit(1);
}
setsid();
pid_t pid2 = fork();
if (pid2 > 0)
{
exit(0);
}
else if (pid2 < 0)
{
exit(1);
}
umask(0);
chdir("/");
for (int i = 0; i < MAXFD; ++i)
{
close(i);
}
close(STDIN_FILENO);
if (open("/dev/null", O_RDWR) != STDIN_FILENO)
{
exit(2);
}
if (dup2(STDIN_FILENO, STDOUT_FILENO) != STDOUT_FILENO)
{
exit(2);
}
if (dup2(STDIN_FILENO, STDERR_FILENO) != STDERR_FILENO)
{
exit(2);
}
signal(SIGHUP, SIG_IGN);
signal(SIGCHLD, SID_IGN);
while(1)
{
sleep(1);
}
return 0;
}
2、编译
gcc -Wall -std=c99 - o D_test simple.c
或者
g++ -o D_test simple.c
3、运行
./D_test
4、查看运行状态
ps -A
- 用c++创建一个Linux上的Daemon程序,含SHELL管理脚本(一)
- 用c++创建一个Linux上的Daemon程序,含SHELL管理脚本(二)
- 用c++创建一个Linux上的Daemon程序,含SHELL管理脚本(三)
- linux Daemon程序设计(含步骤、源码、管理)
- 一个Linux shell脚本,关于CI上打包的
- Tomcat创建实例的一个脚本(shell 脚本)
- linux下c程序 daemon、fork与创建pthread的顺序问题
- linux的Shell脚本程序
- Shell脚本实例(一) 监控一个程序的运行情况
- 用php写的一个daemon程序
- Linux shell 脚本(一)
- 一个创建工程目录的shell脚本
- 一个用shell写的执行java程序的脚本
- 一个简单的shell脚本程序(运用expr命令)
- 一个启动Java程序的shell脚本
- linux shell脚本中读取文件内容的方法(含效率测试)
- linux的C程序 调用 shell脚本,获取shell的执行结果
- 管理Tomcat服务的Daemon脚本
- Cocoa内存管理
- 做人难
- iPhone开发者路线图
- NYOJ38 布线问题 prim算法 kruscal算法
- 1到n的正数中1出现的次数
- 用c++创建一个Linux上的Daemon程序,含SHELL管理脚本(一)
- OS: 生产者消费者问题(多线程+互斥量+条件变量)
- 黑马程序员_面向对象,类,方法,属性
- [www.infoshare.cc]使用SWT插件制作计算器
- 引用传参--面向对象的魅力
- jsp常用的九个内置对象总结
- 机器猫500集下载
- UITableView可编辑状态常用操作
- java包装类