RE: daemon

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hemath wrote:
> I need some help in writing daemon , I googled it but unable to find
> any good links , can some body please give me good links on writing
> daemons .

Dear Hemanth,

It is not a kernel related question. But it is purely on application
development question. The 14th Chapter of the Advanced Unix Programming
by Richard Stevens explains about the Daemon Process creation and its
related things. Anyway here are the simple steps for the creation of the
daemon process.

/*
 * Steps to create a Daemon Process are
 *
 * 1. call fork() and have the parent exit.
 * 2. call setsid() to create a new session.
 * 3. again call fork() and have the parent exit.
 * 4. change the current working directory to root directory.
 * 5. set the file mode creation mask to 0
 * 6. unneeded file descriptors should be closed
 *
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

/* function prototypes */
void daemon_code();	


int daemon_init(void)
{
	pid_t pid;	/* to hold the PID of process */
	int status, fileDesc = -1;
	
	/* creating a new process that is first child */
	if ( (pid = fork()) < 0)
	{	
		printf("Error in fork() function\n");
		return -1;
	}
	else if (pid != 0)	/* if it is a parent process, exit form
it */
		exit(0);	
	
	/* first child contiues */
	
	/* setting session id - becomes session leader */
	if ( (status = setsid()) < 0) 	
	{
		printf("Error in assinging session id\n");
		return -1;
	}

	/* creating a new process that is second child */
	if ( (pid = fork()) < 0)
	{	
		printf("Error in fork() function\n");
		return -1;
	}
	else if (pid != 0)	/* if it is a parent process, exit form
it */
		exit(0);	/* here parent is a first child */
	
	/* second child continues here */
	
	/* change the current working directory to root directory */
	chdir("/");	
	
	/* clearing the file mode creation mask to 0 */
	umask(0);	

	/* undefined file descriptor should be closed */
	fileDesc=open("/dev/null",O_RDWR);	/* stdin */
	
	(void) dup(fileDesc);			/* stdout */
	
	(void) dup(fileDesc);			/* stderr */
	
	/* calling the daemon code from here */
 	daemon_code();
	
	return 0;
}

/* main program starts here */
int main()
{
	daemon_init();
	return 0;
}

/* daemon code start here */
void daemon_code()
{	
    
   for(;;)
   {	   
	/* implementing the required methods for handling your
requirements */
   }
}

Thanks and Regards,
Srinivas G

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
Please read the FAQ at http://kernelnewbies.org/FAQ



[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux