Link to home
Start Free TrialLog in
Avatar of mrcool4444
mrcool4444

asked on

C++ deamon creation

I have a program that I want run as a deamon.  I believe I would use the fork() function, but I have no idea as to how I would do this.
Avatar of akshayxx
akshayxx
Flag of United States of America image

do u have access to advance unix programming by stevens? . ..
there is a good example in that book that shows how to make a process 'die'  and live like a  'daemon'.

its a simple C function that u can call even from CPP code and make  ur any program as a daemon..

in short .. fork the process.. and kill the parent process..
i'll get u the code when i get back to home..

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
SOLUTION
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 mrcool4444
mrcool4444

ASKER

Well, I'll make it a little more detailed what I want.  I want an inet.d file in which i can start and stop the service, such as proftpd or apache.

So, in a file /etc/init.d/band:
start() {
        ebegin "Starting bandwith"
        start-stop-daemon --start --quiet --exec /etc/init.d/bandwith -- -d -p -d -p /var/run/bandwith.pid
        eend $?
}

With this, I can then stop the process by using a stop() function.

My problem though is I cannot get my program (bandwith) to run as a deamon.  When I execute /etc/init.d/band start, it starts bandwith but it doesn't deamonize it.

I need to beable to get bandwith deamonized and then have it return the daemon'ed process pid so init.d can stop it.
which OS are you on UNIX/LINUX ( version/distribution?)
Gentoo linux
Hi mrc00444,
What you you mean by saying your program doens't get daemonized. Does it hang (not returning), or does it die? Remember that stdin, stdout, and stderr will not be available to your program. If you to output something then you have to open the file explicitly.

Assuming that you 'daemon' is just a simple and single process (ie no forking) that loops infinitely then:
  start-stop-daemon -m -p /var/run/bandwidth_pid --start --background --exec /whatever/bandwidth
should start it.

There are a few problem in your example:
1) The --background or -b option is needed if your program doesn't fork() and exits.

2) You have the empty -- option in the wrong place. That would pass the options to your process and not the start-stop-daemon program as required.

3) The -m option is needed if you need a pid file to be created, as well as the -p option to specify the name of the PID file.

4) /etc/init.d/ directory is not a good place to store you daemon executable/binary. It's for the start/stop script only. But that not your problem here.

Good luck
And to stop it:
  start-stop-daemon -p /var/run/bandwidth_pid --stop

Hey,
 The following will work as a Deamon perfectly:
Have a Nice Time.
 Source Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/wait.h>

int main(void)
{
          uid=getuid();
     if(getuid()!=0)
     {
          printf("Error : User is Not in root");
          exit(0);
     }
     pid=fork();
     if(pid>0)
     {
          printf("I'am the parent,child has pid %d",pid);
          exit(0);
     }
     else if(pid<0)
     {
          printf("Fork returned Error, Child is not created \n");
          exit(0);
     }
     else
     {
          printf("\n Child Process \n");
          setsid();
          umask(0);
          DaemonCode();
     }    
     
}

void DaemonCode()
{
     setuid(0);
     while(1)
     {
          usleep(10);
     }
     
}
Hi,
 I had posted the answer for the question, I request to accpet the answer or if some thing is wrong you can make comments.so that the points can be awarde.
Thanks,
Nataraj.
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Split: akshayxx {http:#8223489} & AnhLePhuoc {http:#8235475}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer