Link to home
Start Free TrialLog in
Avatar of _XR_
_XR_

asked on

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.
Avatar of akshayxx
akshayxx
Flag of United States of America image

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);
}
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
r u talking about threads ? do u want to have multiple threads in ur program ?
ASKER CERTIFIED SOLUTION
Avatar of akshayxx
akshayxx
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of _XR_
_XR_

ASKER

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
Avatar of _XR_

ASKER

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.
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