Daemon进程示例

15 篇文章 0 订阅
4 篇文章 0 订阅

Linux下Daemon进程示例。

将进程ID写入到文件中,并对文件加锁,确保只有一个daemon进程在运行。此外,有信号处理函数示例。

(备注:参考大名鼎鼎的 APUE中示例,整理而出 :))

#include <iostream>

using namespace std;

//TO BE ADDED
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>

#define PIDFILE	"main.pid"

int Lockfile(const int iFd)
{
	struct flock    stLock;

	stLock.l_type = F_WRLCK;        /* F_RDLCK, F_WRLCK, F_UNLCK */
	stLock.l_start = 0;    /* byte offset, relative to l_whence */
	stLock.l_whence = SEEK_SET;    /* SEEK_SET, SEEK_CUR, SEEK_END */
	stLock.l_len = 0;        /* #bytes (0 means to EOF) */

	return (fcntl(iFd, F_SETLK, &stLock));
}

int IsRunning()
{
	const int iPidFile = open(PIDFILE, O_RDWR | O_CREAT, (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
	if (iPidFile < 0)
	{
		cout << "Can not open pid file:" << PIDFILE << endl;
		return -1;
	}

	//lock file
	if (Lockfile(iPidFile) < 0)
	{
		if ((EACCES == errno) || (EAGAIN == errno))
		{
			close(iPidFile);
			cout << "Is already running" << endl;
			return 1; //already running
		}

		//fail to lock
		cout << "Cant lock pid file:" << PIDFILE << endl;
		return -1;
	}

	//write pid
	ftruncate(iPidFile, 0);
	char buf[16];
	sprintf(buf, "%ld", (long)getpid());
	write(iPidFile, buf, strlen(buf) + 1);

	return 0;

}

int SetDaemon()
{
	//umask(0);
	int iPid = 0;
	if ((iPid = fork()) < 0)
	{
		//fork error
		return -1;
	}
	else if (0 != iPid) //parent
	{
		exit(0);
	}

	//setsid();

	return 0;
}

void SigTerm(int iSig)
{
	cout << "Recv single term," << iSig << endl;
}

void SigHup(int iSig)
{
	cout << "Recv single hup," << iSig << endl;
}

void SigUsr1(int iSig)
{
	cout << "Recv single Usr1," << iSig << endl;
}

int HandleSingle()
{
	struct sigaction stSigAct;

	stSigAct.sa_handler = SigTerm;
	sigemptyset(&stSigAct.sa_mask);
	sigaddset(&stSigAct.sa_mask, SIGTERM);
	stSigAct.sa_flags = 0;
	if (sigaction(SIGTERM, &stSigAct, NULL) < 0)
	{
		cout << "Can not catch SIGTERM," << strerror(errno) << endl;
		return -1;
	}

	stSigAct.sa_handler = SigHup;
	sigemptyset(&stSigAct.sa_mask);
	sigaddset(&stSigAct.sa_mask, SIGHUP);
	stSigAct.sa_flags = 0;
	if (sigaction(SIGHUP, &stSigAct, NULL) < 0)
	{
		cout << "Can not catch SIGHUP," << strerror(errno) << endl;
		return -1;
	}

	stSigAct.sa_handler = SigUsr1;
	sigemptyset(&stSigAct.sa_mask);
	sigaddset(&stSigAct.sa_mask, SIGUSR1);
	stSigAct.sa_flags = 0;
	if (sigaction(SIGUSR1, &stSigAct, NULL) < 0)
	{
		cout << "Can not catch SIGUSR1," << strerror(errno) << endl;
		return -1;
	}
	return 0;
}

int main()
{
	int iRet = 0;
	iRet = SetDaemon();
	if (0 != iRet)
	{
		cout << "Fail to set daemon, now exit." << endl;
		exit(0);
	}

	iRet = IsRunning();
	if (0 != iRet)
	{
		cout << "Is running!" << endl;
		exit(1);
	}
	cout << "\nmain daemon running..." << endl;

	iRet = HandleSingle();
	if (iRet < 0)
	{
		cout << "HandleSingle error" << endl;
		exit(1);
	}

	while (1)
	{
		pause(); //wait for signal
	}

	//sleep(60); //sleeping 60s

	//pause() causes the calling process (or thread) to sleep until a signal is delivered 
	// that either terminates the process or causes the invocation of
	// a signal-catching function.

	//sleep() makes the calling thread sleep until seconds seconds have elapsed or a
	//signal arrives which is not ignored.

	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Avahi是一个开源的实现了Zeroconf协议的软件,它可以让你的Linux系统自动发现和连接到其他设备和服务。avahi-daemon是Avahi的守护进程,它运行在后台并提供了Zeroconf服务的功能。 以下是一个简单的Avahi Daemon教程示例,演示了如何使用avahi-daemon来发布和发现服务: 1. 安装Avahi 在Ubuntu和Debian系统上,你可以使用以下命令来安装Avahi: ``` sudo apt-get install avahi-daemon ``` 2. 发布服务 使用Avahi Daemon,你可以轻松地发布你的服务,让其他设备和服务能够发现它。以下是一个发布Web服务器服务的示例: ``` sudo nano /etc/avahi/services/http.service ``` 将以下内容粘贴到文件中: ``` <?xml version="1.0" standalone='no'?> <!DOCTYPE service-group SYSTEM "avahi-service.dtd"> <service-group> <name replace-wildcards="yes">%h</name> <service> <type>_http._tcp</type> <port>80</port> </service> </service-group> ``` 保存并关闭文件。然后,重新启动avahi-daemon服务: ``` sudo service avahi-daemon restart ``` 现在,你的Web服务器服务已经发布了,其他设备和服务可以通过Zeroconf协议来发现它。 3. 发现服务 使用Avahi Daemon,你可以轻松地发现其他设备和服务,以便连接到它们。以下是一个发现Web服务器服务的示例: ``` avahi-browse -a ``` 这将列出所有可用的服务和设备,包括你刚才发布的Web服务器服务。 以上是一个简单的Avahi Daemon教程示例,演示了如何使用avahi-daemon来发布和发现服务。你可以使用类似的方法来发布和发现其他类型的服务,例如打印机和文件共享服务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值