LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Difference in task structure of a process and of a daemon (https://www.linuxquestions.org/questions/linux-newbie-8/difference-in-task-structure-of-a-process-and-of-a-daemon-4175444982/)

linkaran 01-10-2013 07:32 AM

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!

btmiller 01-11-2013 12:12 AM

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.

NevemTeve 01-11-2013 02:47 AM

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).

linkaran 01-11-2013 05:37 AM

@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.

Yogesh Walke 01-11-2013 09:58 AM

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.

jpollard 01-11-2013 12:57 PM

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").


All times are GMT -5. The time now is 06:54 PM.