1

I have just started writing a character driver.So, i when inserted my first driver code that prints "hello kernel" from init_module1 and "Bye kernel" from exit module in kernel log. When i insert the driver and use dmesg to see kernel log i cant find the "Hello kernel" message but when i remove the driver(using rmmod) i get both "Hello kernel" as well as "Bye kernel" in the log. Cant figure out how & why. This is my code...

header.h

    #include<linux/init.h>    
    #include<linux/module.h>

    MODULE_LICENSE("GPL");

init.c

    #include"header.h"

    static int init_module1(void)
    {
      printk(KERN_ALERT "Hello kernel");
      return 0;
    }
    module_init(init_module1);

exit.c

    #include"header.h"

    static void exit_module(void)
    {
      printk(KERN_ALERT "Bye Kernel");
    }
    module_exit(exit_module);

Makefile-

    INSTALLDIR= $(shell pwd)/modules
    ifneq ($(KERNELRELEASE),)
    obj-m := c.o
    c-objs := init.o exit.o

    else
              KERNDIR ?= /lib/modules/$(shell uname -r)/build
              PWD := $(shell pwd)

    default:
              $(MAKE) -C $(KERNDIR) M=$(PWD) modules
              @rm -rf $(INSTALLDIR)
              @mkdir $(INSTALLDIR)
              @mv *.ko *.mod.c *.o .*.cmd $(INSTALLDIR)

    clean:
              rm -rf $(INSTALLDIR)

    endif
Imdad
  • 683
  • 1
  • 9
  • 27

1 Answers1

2

The kernel log handles only complete lines. Add the missing new line character:

     printk(KERN_ALERT "Hello kernel\n");
CL.
  • 173,858
  • 17
  • 217
  • 259
  • Thanks,that works.So does it mean that **printk** works exactly in a similar way as **printf** apart from writing in kernel log? – Imdad Jun 22 '14 at 16:56
  • 1
    There are other differences; for example, `printf` is not always line-buffered. – CL. Jun 22 '14 at 18:15