LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 01-10-2013, 07:32 AM   #1
linkaran
LQ Newbie
 
Registered: Aug 2012
Posts: 17

Rep: Reputation: Disabled
Difference in task structure of a process and of a daemon


I am trying to differentiate a daemon from a normal process(user or kernel). I have tried to check if the task structure of a daemon has some special field. I also noticed that daemonizing a process means there is no controlling terminal associated with a daemon but not sure if it helps while programming. Please suggest if there is any way to differentiate a daemon from a normal process.

Thank you in advance!
 
Old 01-11-2013, 12:12 AM   #2
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,082

Rep: Reputation: 299Reputation: 299Reputation: 299
Daemons are processes and as far as I know, there's nothing magical that separates a daemon from a normal process. The main thing, as you note, is that there's no controlling terminal. You might find [UTL=http://www.enderunix.org/docs/eng/daemon.php]this page[/URL] helpful, as it describes how to set up a daemon and how to use setsid to detach from a controlling terminal. One thing that this page doesn't mention, though, that I learned some time ago, is that a daemon should fork a second time after the setsid(), with the parent exiting and the child continuing to run. The reason for this is that the child will not be the process group leader (which means it cannot regain a controlling terminal since only a process group leader may do that -- not sure if there is any other use to that; it's been awhile since I looked at this seriously).

In short, I don't think there's any binary flag in task_stuct to tell whether a process is a daemon. It's more a combination of events, i.e. a background process that does not have a controlling terminal (i.e. runs totally in the background) with no stdin or stdout. Otherwise, AFAIK a daemon is just like any other process.
 
Old 01-11-2013, 02:47 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,727

Rep: Reputation: 488Reputation: 488Reputation: 488Reputation: 488Reputation: 488
A possible (if superficial) definition:

A process is considered to be a daemon if it doesn't have a controlling terminal and its parent is init (ppid==1).
 
Old 01-11-2013, 05:37 AM   #4
linkaran
LQ Newbie
 
Registered: Aug 2012
Posts: 17

Original Poster
Rep: Reputation: Disabled
@btmiller,from the document you referred to, it is clear that a daemon is just like any other process with probably two distinct properties of its own which are no controlling terminal and parented to init process. Though these might be the necessary conditions for a daemon but not sufficient, using these to identify daemons.

Please do let know any points I have missed,if any.
 
Old 01-11-2013, 09:58 AM   #5
Yogesh Walke
LQ Newbie
 
Registered: Dec 2012
Location: Pune, India
Distribution: fedora
Posts: 1

Rep: Reputation: Disabled
A daemon can be detected using combination of following properties, i.e., -
* Daemons are reparented to init process : (task->parent->pid == 1)
* There is no controlling terminal associated with a daemon : (task->signal->tty == NULL)
* Setsid is called while daemonizing : (task->group_leader->signal->leader == 1)
So,
$$ for_each_process(task)
if (((task->signal->tty)==NULL) && (task->parent->pid == 1)) && (task->group_leader->signal->leader == 1))

$$
code can be used in a kernel module to list all daemons.
 
1 members found this post helpful.
Old 01-11-2013, 12:57 PM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 2,136

Rep: Reputation: 549Reputation: 549Reputation: 549Reputation: 549Reputation: 549Reputation: 549
Ummm... not exactly.

Any process put into the background is reparented to init when the parent process tree terminates - the classic way to do that is using the "nohup" command. This isn't necessary for bash though.

When the parent process tree terminates, the process automatically becomes the task group leader for any processes it starts, and its signal leader is init (pid 1, due to reparenting). No need for a setsid system call.

All that is necessary is to redirect stdin/stdout/stderr, put it in the background and log out (the redirection is needed to disconnect from the controlling terminal - otherwise the process gets an I/O error on these file descriptors).

The definition is: http://en.wikipedia.org/wiki/Daemon_%28computing%29

Code:
In a Unix environment, the parent process of a daemon is often, but not always, the
init process. A daemon is usually created by a process forking a child process and
then immediately exiting, thus causing init to adopt the child process. In addition,
a daemon or the operating system typically must perform other operations, such as
dissociating the process from any controlling terminal (tty). Such procedures are
often implemented in various convenience routines such as daemon(3) in Unix.
There is no special field that identifies a daemon. It is just how a process is used.

Trivia side note: The name "daemon" comes from "Dump And Examine MONitor" from pre-UNIX history (TOPS 10, I believe). It was the name for a system service that provided for core dumps of user memory and for debugging the system monitor (now called a "kernel").

Last edited by jpollard; 01-11-2013 at 01:02 PM.
 
  


Reply

Tags
daemon, kernel, programming


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
Process vs task vs threads vishnu anand Linux - Newbie 5 01-07-2012 07:25 AM
How to find which process started a daemon process in autostart? indiajoe Linux - Desktop 4 10-25-2011 02:19 AM
difference between init.rc process and normal process manohar Programming 1 03-31-2011 11:17 PM
Where is the PCB Task Structure located NuNn Linux - General 3 04-15-2009 11:31 AM
what is the difference between a daemon a client and a server? alekoos Slackware 4 04-27-2004 02:14 AM


All times are GMT -5. The time now is 11:06 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration