?
Solved

Developing a Daemon

Posted on 2003-03-05
7
Medium Priority
?
352 Views
Last Modified: 2010-04-21
How could i do a daemon ??
Iknow tha eveybody uses fork (i almost know using fork), but i want to know if it possible to do using pthread_daemon_create, so using POSIX Thread.
0
Comment
Question by:_XR_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 8

Expert Comment

by:akshayxx
ID: 8076323
just using fork .. wont create a daemon.. the way is to create the child process with fork and then kill the parent ..
i hope u already know this .. if not try it ..

call this function from the program that wants itself to be made a daemon
//taken from advanced unix programmin by stevens

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int daemon_init(void){
 pid_t pid;
 if((pid=fork())<0)
    return(-1);
 else if(pid!=0)
     exit(0);  //parent goes bye-bye
   //child continues
   setsid();   //become session leader
   chdir("/"); //change working directory
  umask(0);// clear our file mode creation mask
  return(0);
}
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8076333
and i am afraid to say that ,, there is no function named
pthread_daemon_create .. at least i have not heard of it yet ..  i would be glad to know if u can tell me where did u come across such a function
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8076347
r u talking about threads ? do u want to have multiple threads in ur program ?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 8

Accepted Solution

by:
akshayxx earned 400 total points
ID: 8076356
4.3 How can I make my server a daemon?
There are two approaches you can take here. The first is to use inetd to do all the hard work for you. The second is to do all the hard work yourself.

If you use inetd, you simply use stdin, stdout, or stderr for your socket. (These three are all created with dup() from the real socket) You can use these as you would a socket in your code. The inetd process will even close the socket for you when you are done.

If you wish to write your own server, there is a detailed explanation in "Unix Network Programming" by Richard Stevens (see 1.5 Where can I get source code for the book [book title]?). I also picked up this posting from comp.unix.programmer, by Nikhil Nair ( nn201@cus.cam.ac.uk). You may want to add code to ignore SIGPIPE, because if this signal is not dealt with, it will cause your application to exit. (Thanks to ingo@milan2.snafu.de for pointing this out).


I worked all this lot out from the
GNU C Library Manual (on-line
documentation). Here's some code I wrote -
you can adapt it as necessary:


#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/wait.h>

/* Global variables */

volatile sig_atomic_t keep_going = 1;
/* controls program termination */


/* Function prototypes: */

void termination_handler (int signum);
/* clean up before termination */


int
main (void)
{
...

if (chdir (HOME_DIR))
/* change to directory containing data files */
{
fprintf (stderr, "`%s': ", HOME_DIR);
perror (NULL);
exit (1);
}

/* Become a daemon: */
switch (fork ())
{
case -1:    /* can't fork */
perror ("fork()");
exit (3);
case 0:
/* child, process becomes a daemon: */
close (STDIN_FILENO);
close (STDOUT_FILENO);
close (STDERR_FILENO);
if (setsid () == -1)
/* request a new session (job control) */
{
exit (4);
}
break;
default:
/* parent returns to calling process: */
return 0;
}

/* Establish signal handler to clean up
before termination: */
if (signal (SIGTERM, termination_handler) == SIG_IGN)
signal (SIGTERM, SIG_IGN);
signal (SIGINT, SIG_IGN);
signal (SIGHUP, SIG_IGN);

/* Main program loop */
while (keep_going)
{
...
}
return 0;
}

void
termination_handler (int signum)
{
  keep_going = 0;
  signal (signum, termination_handler);
}

0
 

Author Comment

by:_XR_
ID: 8080019
thanks by your replies akshayxx, the function isn't pthread_daemon_create it's pthread_atfork, but isn't necessary nothing more about. I'll use the fork like i imagined before and know so let's work.
Thanks && Regards to All
0
 

Author Comment

by:_XR_
ID: 8080030
Nice Job i have this book, but how i said before i doesn't want use fork but what can i do i'll use them.
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8084682
you might have seen it by now .. just in case u havent .. see this
http://www.unidata.ucar.edu/cgi-bin/man-cgi?pthread_atfork+3

so the calls that are required for the parent to be done and the calls that are requred to be done by child .. for the process to be made daemon..
pid=fork()
parentcode=if( pid> 0 ) ....
childcode= if(pid==0)........
put both in separate functions ..and register them using pthread_atfork()..
and whenever u call fork from the parent process .. parent and child codes will be executed at appropriate places and u'll have the DAEMON at your disposal
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question