博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux - 守护进程-2
阅读量:4042 次
发布时间:2019-05-24

本文共 1134 字,大约阅读时间需要 3 分钟。

利用daemon()函数创建守护进程

1. 函数简介

#include <unistd.h>

int daemon(int nochdir, int noclose);

DESCRIPTION
       The daemon() function is for programs wishing to detach themselves from
       the controlling terminal and run in the background as system daemons.

       If nochdir is zero, daemon()  changes  the  process’s  current  working

       directory to the root directory ("/"); otherwise,

       If  noclose is zero, daemon() redirects standard input, standard output

       and standard error to /dev/null; otherwise,  no  changes  are  made  to
       these file descriptors.

功能:创建一个守护进程

参数:

nochdir:=0将当前目录更改至“/”

noclose:=0将标准输入、标准输出、标准错误重定向至“/dev/null”

返回值:

成功:0

失败:-1

 

2. demo

#include 
#include
#include
#include
#include
#include
#include
#include
#define ERR_EXIT(m) \do \{\ perror(m);\ exit(EXIT_FAILURE);\}\while(0);int main(void){ time_t t; int fd; if(daemon(0, 1) < 0) { ERR_EXIT("daemon error"); } while(1) { fd = open("daemon.log", O_WRONLY|O_CREAT|O_APPEND, 0644); if(fd == -1) { ERR_EXIT("open error"); } t = time(0); char *buf = asctime(localtime(&t)); write(fd, buf, strlen(buf)); close(fd); sleep(60); } return 0;}

 

转载地址:http://glldi.baihongyu.com/

你可能感兴趣的文章
[关注大学生]李开复给中国计算机系大学生的7点建议
查看>>
[关注大学生]大学毕业生择业:是当"鸡头"还是"凤尾"?
查看>>
[茶余饭后]10大毕业生必听得歌曲
查看>>
gdb调试命令的三种调试方式和简单命令介绍
查看>>
C++程序员的几种境界
查看>>
VC++ MFC SQL ADO数据库访问技术使用的基本步骤及方法
查看>>
VUE-Vue.js之$refs,父组件访问、修改子组件中 的数据
查看>>
Vue-子组件改变父级组件的信息
查看>>
Python自动化之pytest常用插件
查看>>
Python自动化之pytest框架使用详解
查看>>
【正则表达式】以个人的理解帮助大家认识正则表达式
查看>>
性能调优之iostat命令详解
查看>>
性能调优之iftop命令详解
查看>>
非关系型数据库(nosql)介绍
查看>>
移动端自动化测试-Windows-Android-Appium环境搭建
查看>>
Xpath使用方法
查看>>
移动端自动化测试-Mac-IOS-Appium环境搭建
查看>>
Selenium之前世今生
查看>>
Selenium-WebDriverApi接口详解
查看>>
Selenium-ActionChains Api接口详解
查看>>