PoiNtEr->: Creating a Daemon Process in C Language On Linux

                             Difference between a dream and an aim. A dream requires soundless sleep, whereas an aim requires sleepless efforts.

Search This Blog

Saturday, February 25, 2012

Creating a Daemon Process in C Language On Linux



What is Dameon Process?



A Daemon process is a process which is not associated with any terminal and hence is supposed to run in background. Since, a daemon process involves background processing, it is recommended it should not include any user interaction. Therefore, it should be clear about its continuous processing not caring to wait for any input or to display any output. However, C programming does not define any such restrictions in its development as such. A daemon process developer should be well aware of its definition and ideal functionality before one starts the programming.

A well known example of a daemon process could be a mail server, which runs in background and listening to ports to receive any mail anytime. Please note, a daemon process could be called as services on windows OS, but we are not talking about windows OS here, as things are pretty different there.

The fork() call? 

Before, we begin programming on creating/handling processes, you should be well aware of the fork() C library call

pid_t fork()

fork() call initiates a new process. Process? Going back to basics, lets understand what is a process first. A process can be called as an execution context of a running program with its own process memory layout in the RAM. When we run any C-compiled executable. 
Coming back to fork() call, it would create a new child process, with the calling process as its parent. The call returns the PID i.e. the unique process id of the newly created child process. However, from the point, where a fork() is called in the program, two processes begins to run i.e. both processes executes the same control flow, as to what follows the fork() call.

Following are some important aspects of this call :
  • The child has its own unique process ID, and this PID does not match the ID of any existing process group.
  • The child’s parent process ID is the same as the parent’s process ID.
  • The child does not inherit its parent’s memory locks.
  • Process resource utilization and CPU time counters are reset to zero in the child.
  • The child’s set of pending signals is initially empty.
  • The child does not inherit semaphore adjustments from its parent.
  • The child does not inherit record locks from its parent.
  • The child does not inherit timers from its parent.
  • The child does not inherit outstanding asynchronous I/O operations from its parent, nor does it inherit  any  asynchronous I/O contexts from its parent.
Writing daemon process in C

1:Remove association of the daemon process with any terminal
2:Change the file mode maskings
3:Create a new session
4:Close standard inputs, outputs and errors
5:A daemon process logic

The Implementation

Based on the design as mentioned in the first section. Here is the complete implementation :
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
int main(int argc, char* argv[])
{
FILE *fp= NULL;
pid_t process_id = 0;
pid_t sid = 0;
// Create child process
process_id = fork();
// Indication of fork() failure
if (process_id < 0)
{
printf("fork failed!\n");
// Return failure in exit status
exit(1);
}
// PARENT PROCESS. Need to kill it.
if (process_id > 0)
{
printf("process_id of child process %d \n", process_id);
// return success in exit status
exit(0);
}
//unmask the file mode
umask(0);
//set new session
sid = setsid();
if(sid < 0)
{
// Return failure
exit(1);
}
// Change the current working directory to root.
chdir("/");
// Close stdin. stdout and stderr
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
// Open a log file in write mode.
fp = fopen ("Log.txt", "w+");
while (1)
{
//Dont block context switches, let the process sleep for some time
sleep(1);
fprintf(fp, "Logging info...\n");
fflush(fp);
// Implement and call some function that does core work for this daemon.
}
fclose(fp);
return (0);
}
$ gcc -Wall -o deamon deamon.c
$ sudo ./deamon
process_id of child process 2936
When you check the log.txt file located in the root directory, you could see that this daemon process is running.
$
$ tail -f /Log.txt
Logging info...
Logging info...
Logging info...
Logging info...
Logging info...
Logging info...
Logging info...
Logging info...
Logging info...

No comments:

Post a Comment