PoiNtEr->: February 2012

                             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[])
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
// 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
//unmask the file mode
//set new session
sid = setsid();
if(sid < 0)
// Return failure
// Change the current working directory to root.
// Close stdin. stdout and stderr
// 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
fprintf(fp, "Logging info...\n");
// Implement and call some function that does core work for this daemon.
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...

Wednesday, February 22, 2012

What is __attribute__((packed)) ???

#include <stdio.h>

struct s1 {
   char a;
   int  i;

struct s2 {
   char a;
   int i __attribute__((packed));

int main( int argc, char* argv[] ) {

  struct s1 s_1;
  struct s2 s_2;

  printf( "sizeof s1 is %d\n" , sizeof(s_1) );
  printf( "sizeof s2 is %d\n" , sizeof(s_2) );

  return( 0 );

sizeof s1 is 8
sizeof s2 is 5

__attribute__((packed)) ensures that structure fields align on one-byte boundaries.That means now your structure will be of same size on any processor.Using this attribute you tell GCC that you want to align them one after other.

As in s1: like here processor is aligning to 4-byte boundaries.
In s2:

Sunday, February 19, 2012

Eva Operating system

Eva Operating System
                  I have just lunched a new blog about my final year project.So just go and check it out.
Aim of My Project:To develop a CUI operating system including features like command interface, file system, multitasking.
Stay tuned I'll be updating it soon..

For more News On Eva-OS:
1: http://eva-os.blogspot.in

Saturday, February 18, 2012

Linux Kernel Module Programming ...Hello World!! Once Again

SO Once again i am back with something on kernel module programming.
Before  moving further i must suggest you to go through my previous post  as this will help you out to easily understand things.

First I  want to clear up few concepts which i was unable to address in my last post on kernel modules.

1:Kernel modules must have at least two functions: a "start" (initialization) function called init_module() which is called when the module is insmoded into the kernel, and an "end" (cleanup) function called cleanup_module() which is called just before it is rmmoded. Actually, things have changed starting with kernel 2.3.13.


#include <linux/module.h> /* Needed by all modules */

#include <linux/kernel.h> /* Needed for KERN_INFO */

int init_module(void)


 printk(KERN_INFO "Hello world 1.\n");


  * A non 0 return means init_module failed; module can't be loaded.


 return 0;


void cleanup_module(void)


 printk(KERN_INFO "Goodbye world 1.\n");


2:As of Linux 2.4, you can rename the init and cleanup functions of your modules; they no longer have to be called init_module() and cleanup_module() respectively. This is done with the module_init() and module_exit() macros. These macros are defined in linux/init.h.


#include <linux/module.h> /* Needed by all modules */

#include <linux/kernel.h> /* Needed for KERN_INFO */

#include <linux/init.h>  /* Needed for the macros */

static int __init hello_2_init(void)


 printk(KERN_INFO "Hello, world 2\n");

 return 0;


static void __exit hello_2_exit(void)


 printk(KERN_INFO "Goodbye, world 2\n");




// As you can see now we can easily take any name as out initial and end function// but the only thing is that we have to define it in the end

3: The macro __init and __exit are  feature of kernel 2.2 and later. Notice the change in the definitions of the init and cleanup functions. The __init macro causes the init function to be discarded and its memory freed once the init function finishes for built-in drivers, but not loadable modules. If you think about when the init function is invoked, this makes perfect sense.
For Example Check out the above Program.

4:Important Commands
a) insmod(to insert module),
example: insmod hello.ko

b) rmmod(to remove a module),
example:rmmod hello

c) dmesg(to check the output of our insert module)
example:dmesg  | tail

/* Stay tuned for more */

Friday, February 17, 2012

How to Send SMS With Kannel SMS and WAP gateway??

Continuing from My last Post.
Now We have our own SMS gateway up and working.But the million dollar question is how to send sms 
now from it?
So you can use following links on your browser or php codes to send SMS:

Just put your own username and password and you are good to go.

Now we can also build own PHP/MYSQL applications to send sms.
How We are going to do it ??
  1. User send his/her name by sms
  2. The message is forwarded to our application by the SMS box
  3. Our application store the sender name and telephone number in the database and then reply back to the user
Setting up the SMS box to foward every received message to our application{we had already done this in my last post here}

group = sms-service
keyword = default
get-url = "http://localhost/kannel/receivesms.php?sender=%p&amp;text=%b"
accept-x-kannel-headers = true
max-messages = 3
concatenation = true
catch-all = true

We must make the get-url point to our script and passed the variables as querystring
%p – is the variable for the sender number
%b – represent the text message received
you can take a look at other available variables here
A Simple PHP Script:
function smsinsert($sender,$text)
 $con = 'mysql:dbname='.DBNAME.';host='.DBHOST;
 try {
      $cmd = new PDO($con,DBUSERNAME,DBPASSWORD);
   $stmt = $cmd->prepare("INSERT INTO kannel_tuto (number,message) VALUES (:sender,:message)");
   $cmd = null;
     echo "Hello ".$text.". Thank you for your registration.";
     echo "Sorry an error has occured";
  catch (PDOException $e) {
      echo 'Connection failed: ' . $e->getMessage();

The script is very simple.
1:Create your own database with phpmyadmin or mysql. 
2:We define a function smsinsert which will save the person name and phone number in our database.
3:The query string values are passed to the function
4:The function reply back by a success or failure message
5:The message is automatically send  to the sender.