| Same major number devices will pick same driver
Steps -------- - A module is made to register with kernel for the file /dev/myown withe major number 254. - Then open/write function were implemeneted for file_operations structure. - Two character device files were created with the same major number and different minor no under /dev using the following command, mknod /dev/myown c 254 0 mknod /dev/myown1 c 25 0 - Two application programs were written to open the file /dev/mywon & /dev/myown1 - Run the exes of the two application programs, you will get the printk messages from the kernel for both of these programs. (i.e from sample_write function of driver ) Observation: ------------ --- - The driver is said not to be associated with the device file /dev/myown but to the device files who are all having major no 254. - A driver never actually knows the name of the device being opened, just the device number�and users can play on this indiffer ence to names by aliasing new names to a single device for their own convenience. If you create two special files with the same major/minor pair, the devices are one and the same, and there is no way to differentiate between them. The same effect can be obtained using a symbolic or hard link, and the preferr ed way to implement aliasing is creating a symbolic link - From LDD - Linux Device Driver book. Driver code driver.c ---------- #include <linux/kernel.h> #include <linux/module.h> #include <linux/fs.h> static int sample_write(struct file *file,const char* data,int len,loff_t *off); static int sample_open(struct inode* inode,struct file* filep); static struct file_operations rwstruct={ open:sample_open, write:sample_write, owner:THIS_MODULE }; static int major; int init_module(void) { printk(" <0>inside init_module"); major = register_chrdev(254,"myown",&rwstruct); printk(KERN_EMERG "The major umber assigned is %d",major); return 0; } void cleanup_module(void) { int cleanup = unregister_chrdev(major,"myown"); printk(KERN_EMERG "The major number cleaned up is %d",cleanup); } static int sample_open(struct inode* inode,struct file* file) { printk(KERN_EMERG "Inside open %d \n",inode->i_rdev); return 0; } static int sample_write(struct file *file,const char* data,int len,loff_t *off) { printk(KERN_EMERG "Inside sample_write lengths is %d \n",len); return 0; } Application code appl1.c ---------- #include<stdio.h> #include<unistd.h> int main() { int fd; fd = open("/dev/myown",O_WRONLY); while(1) { printf("Writig a myown character"); write(fd,"c",1); sleep(3); } close(fd); } appl2.c --------- #include<stdio.h> #include<unistd.h> int main() { int fd; printf("Pgrogram starts \n"); fd= open("/dev/myown1",O_RDONLY); while(1) { printf("Writing myown1 character"); write(fd,"MANI",1); sleep(3); } close(fd); } |