Developing a Daemon

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.
_XR_Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

akshayxxCommented:
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
akshayxxCommented:
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
akshayxxCommented:
r u talking about threads ? do u want to have multiple threads in ur program ?
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

akshayxxCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
_XR_Author Commented:
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
_XR_Author Commented:
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
akshayxxCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux OS Dev

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.