I read in Linux Device drivers book that kernel will not allow to remove a module if it is busy. That is if a process is accessing the device(open/read/write..etc) then the module cannot be removed.

To test this i wrote a sample module and a test application to access the character device continuously and then removed the module when the application is using the driver.

Driver code


#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>

static int sample_write(struct file *file,const char* data,int len,loff_t *off);

static struct file_operations rwstruct={
write : sample_write

static int major;

int init_module(void)
major = register_chrdev(0,"myown",&rwstruct);
printk(KERN_ALERT "The major umber assigned is %d",major);
return 0;

void cleanup_module(void)
int cleanup = unregister_chrdev(major,"file");
printk(KERN_ALERT "The major umber cleaned up is %d",cleanup);


static int sample_write(struct file *file,const char* data,int len,loff_t *off)
printk(KERN_ALERT "Inside sample_write ");
return 0;

I compiled the above driver code and loaded with insmod command then created a device under /dev as 'myown' with the major number displayed in /proc/devices for the module.

int main()

int fd;
printf("Program starts \n");
fd= open("/dev/myown","w");
while(1) {
printf("Writing character\n");

In two screens i was running the

1) tail -f /var/log/messages
2) ./a.out

In both the screens i was getting the o/p displayed and in other screen,when i did
#rmmod samplew.ko

Then the system got hung and kernel paniced.A reboot is required to get the system back running.


1) Why did the kernel didnot prevent us from removing the module when it is being used by a process ?

Is this the expected behavior ?

Can some one answer my query

Thanks in advance,

- Mani