LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 03-18-2011, 04:00 PM   #1
Keozon
LQ Newbie
 
Registered: Oct 2009
Posts: 4

Rep: Reputation: 0
Advantage to creating a daemon vs just setsid


So I have a hobby web server running. It works fine, and I have fun making cgi scripts and thinking I can take down facebook.

Anyway, my latest project has been to make my own database program to accept queries for a forum. I have no real need to do this, its mostly just for educational purposes. And one thing I've been unable to figure out, is what the advantage is to actually forking a daemon (somewhat complicated and I have no real idea as to what is going on when it all happens), and just using setsid in the program to keep it running in the background. I know there has to be a reason, but its a tough thing to search for, and I've been able to find nothing really helpful.

So I figured I'd ask. If anyone can tell me what a daemon process does for me, that'd be great. And if anyone could tell me what the forking procedure actually does, conceptually (in either C or python terms, if possible. I could probably make sense of java), that'd be an added bonus.

Thanks a ton in advance!
 
Old 03-19-2011, 10:07 AM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
How about a definition (from http://www.linfo.org/daemon.html -- worth reading the entire document)?
Quote:
A daemon is a type of program on Unix-like operating systems that runs unobtrusively in the background, rather than under the direct control of a user, waiting to be activated by the occurrence of a specific event or condition.
OK, so what the heck does that mean? Basically, a daemon process is forked off a parent process and will continue until the system is shut down, there is a fatal error in the daemon programming or somebody or something kills it.

By the way, if you're going to roll your own data base, you're probably going to want to look into the inter process communications facilities (IPC); i.e., shared memory, semaphores, and message queues. Those are how you manage multiple simultaneous access so you don't have collisions and other bad things happening. A quick overview is found at http://web.cs.wpi.edu/~cs3013/b00/we...6-unixipc.html (and Google "unix ipc" will turn up a whole bevy of stuff that's interesting).

The best I can do is point you to Linux Daemon Writing HOWTO (http://www.netzmafia.de/skripten/uni...mon-howto.html) that is pretty much everything you need to know about the what's, why's, how's and wherefore's. I'm not being lazy -- it's a large subject that really can't be adequately addressed here.

Hope this helps some.
 
Old 03-20-2011, 01:19 PM   #3
Keozon
LQ Newbie
 
Registered: Oct 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks for the answer! I read through everything you posted (and a little more) and I certainly learned a lot. I'm still not entirely sure, however, why just doing setsid in the program to make it run independently in the background is not adequate. I do however, now understand why the procedure for making a daemon is so complicated, and why it does what it does.

Is just running a program in the background with setsid just bad practice, or does it actually restrict me somehow? Does it have to do with memory management?
 
Old 03-21-2011, 08:45 AM   #4
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Quote:
Is just running a program in the background with setsid just bad practice, or does it actually restrict me somehow? Does it have to do with memory management?
When you use the fork() system call and exit (the parent), the child process is orphaned and then becomes a child of the init process (PID 1).

In Unix and Linux systems,
Quote:
A process receives signals from the terminal that it is connected to, and each process inherits its parent's controlling tty. A server should not receive signals from the process that started it, so it must detach itself from its controlling tty.

In Unix systems, processes operates within a process group, so that all processes within a group is treated as a single entity. Process group or session is also inherited. A server should operate independently from other processes.
http://www.enderunix.org/docs/eng/daemon.php That is, a setsid() process will not become a child of init.

Is one better than the other? Well, I confess that I don't really know; I suspect that there are subtle differences (and, probably butt-byters) either way. I suppose the best thing would be... well, try it both ways and see if it makes any difference. I've always done it the old-fashioned way (which is what MySQL and PostgreSQL and other applications do) but that's pretty much because that's the way I learned to doit toit.

There is an example setsid() program at http://www.enderunix.org/docs/eng/exampled.c that might be worth a look-see.

Hope this helps some.
 
Old 03-21-2011, 08:20 PM   #5
Keozon
LQ Newbie
 
Registered: Oct 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Alright. Thanks to the links you posted, and the further searching they inspired, I've managed to make sense of what I found earlier at an activestate tutorial on daemonization.

So, to answer my own question, setsid() is technically enough to make a process into a daemon, but it has a few holes. One, if the process calling setsid() is a session leader, UNIX won't allow setsid() to function, because allowing such would cause any child processes to be orphaned and estranged. So, in order to make sure setsid() will always execute, a child process is forked using fork(). Since a child is never a session leader, setsid() will work always.

Running setsid() on the forked process will then make it its own session and process group leader. But, this means it can be attached to a controlling terminal, which daemons don't want/need (possible security risk?), but in UNIX, only a session leader can be attached to a controlling tty, so a child of that process could not have a controlling tty. Another fork creates a child process.

At this point killing off the two parent processes closes all connections to the daemonized process, making it isolated (and, again, more secure? still not sure what isolation does for it, but oh well). Once its parents are killed off, it will be adopted by init, and officially be a daemon in Unix.
 
Old 03-22-2011, 05:58 AM   #6
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Yup, the really basic thing you (must) do is fork-and-exec so the child can operate without a controlling terminal (it'll just sit there all by its lonesome doing what it does). Best of luck with your data base application.
 
  


Reply



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
Is it necessary to use setsid in order to create a daemon? mitsulas Programming 1 04-21-2010 11:52 AM
Creating a daemon offworld21 Programming 10 09-08-2006 12:25 PM
creating simple sh daemon bobbens Programming 6 06-01-2005 11:10 AM
Creating socket programs as daemon cranium2004 Programming 2 03-04-2005 04:42 AM
setsid() question: child has terminal after all? registering Programming 1 06-16-2003 06:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 07:50 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