LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-12-2001, 10:14 AM   #1
kobilevi
LQ Newbie
 
Registered: Feb 2001
Posts: 10

Rep: Reputation: 0
Question linux daemons


Hi,

I want to write a daemon which will run automatically when the system is loaded. Is there any specifications I should follow?

Thanks,
Kobi.
 
Old 08-12-2001, 10:46 PM   #2
SensFan
Member
 
Registered: May 2001
Location: Toronto, Canada
Distribution: Slackware
Posts: 67

Rep: Reputation: 15
Heres code I've been using.

Code:
#include <stdio.h>

int init_daemon();

int main()
{
    int success_flag;
    printf("Starting  Daemon\n");
     init_daemon();
    /*code goes here*/
     return (0);
}
int init_daemon()
{
     pid_t pid;

     if( ( pid=fork()) <  0)
        return (-1);  /*fork failed*/

   else if(pid !=0)
       exit(0);   /*parent is destroyed at this point*/

   setsid(); /*becomes the session leader*/

    chdir("/");  /*changes current dir, to avoid blocking a device from being umounted*/

    umask(0);  /*clear out the file mode creation mask*/

    return (0);
}
That code just makes your process a daemon, if will die right away cause it returns right after that. I got that from Advanced Programming in UNIX environment vy Stevens. Good book. Other implementations call for another fork, info on the net and in Stevens book as well. Maybe place it in init.d to get it launched on startup.
 
Old 08-13-2001, 07:14 AM   #3
kobilevi
LQ Newbie
 
Registered: Feb 2001
Posts: 10

Original Poster
Rep: Reputation: 0
What about c++?

Is there any different?

Thanks,
Kobi.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
managing daemons under Ubuntu linux retiem Debian 1 03-04-2005 12:59 PM
daemons LinuxLala Linux - General 4 11-28-2003 01:37 PM
Daemons mariua Linux - General 0 10-11-2001 05:03 AM
Daemons mariua Linux - Newbie 0 10-11-2001 05:00 AM
daemons acren Linux - Security 3 07-06-2001 04:28 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration