Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 01-10-2013, 08:32 AM   #1
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, 01:12 AM   #2
Senior Member
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,289

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
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=]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, 03:47 AM   #3
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,996

Rep: Reputation: 1443Reputation: 1443Reputation: 1443Reputation: 1443Reputation: 1443Reputation: 1443Reputation: 1443Reputation: 1443Reputation: 1443Reputation: 1443
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, 06:37 AM   #4
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, 10: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)
$$ 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, 01:57 PM   #6
Senior Member
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,844

Rep: Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478
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:

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 02:02 PM.


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

All times are GMT -5. The time now is 03:38 PM.

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