SlideShare ist ein Scribd-Unternehmen logo
1 von 4
Downloaden Sie, um offline zu lesen
CREATION OF SIMPLE CHARACTER DEVICE DRIVER - PC
Character device driver for PC
Create simple code for character device driver:
Create a new directory inside the source code and create files in this directory:
$ vi char_drv.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
int file_open (struct inode *pinode, struct file *pfile)
{
printk(KERN_INFO "Inside the %s functionn",__FUNCTION__);
return 0;
}
size_t file_read (struct file *pfile, char __user *buffer, size_t
length, loff_t *offset)
{
printk(KERN_INFO "Inside the %s functionn",__FUNCTION__);
return 0;
}
ssize_t file_write (struct file *pfile, const char __user *buffer,
size_t length, loff_t *offset)
{
printk(KERN_INFO "Inside the %s functionn",__FUNCTION__);
return length;
}
int file_close (struct inode *pinode, struct file *pfile)
{
printk(KERN_INFO "Inside the %s functionn",__FUNCTION__);
return 0;
}
/* To hold the file operations performed on the device */
struct file_operations fops = {
.owner = THIS_MODULE,
.open = file_open,
.read = file_read,
.write = file_write,
.release = file_close,
};
1
CREATION OF SIMPLE CHARACTER DEVICE DRIVER - PC
int char_drv_init(void)
{
printk(KERN_INFO "Inside the %s functionn",__FUNCTION__);
/* Register with the kernel and indicate that we are
registering character device driver */
register_chrdev(240 /* Major Number */,
"char_drv" /* Name of the driver */,
&fops /* file operations */);
return 0;
}
void char_drv_exit(void)
{
printk(KERN_INFO "Inside the %s functionn",__FUNCTION__);
/* unregister the character device driver */
unregister_chrdev(240, "char_drv");
}
module_init(char_drv_init);
module_exit(char_drv_exit);
Create Makefile:
$ vi Makefile
obj-m += char_drv.o
Building the Driver:
$ make -c /lib/modules/$(uname -r)/build M=$PWD modules
after using the above command, you get kernel object file “char_dev.ko”.
2
CREATION OF SIMPLE CHARACTER DEVICE DRIVER - PC
Inserting the char_drv module:
$ sudo insmod ./char_drv.ko
Now you can check the driver get loaded by using below command
$ lsmod | grep char
char_drv 12767 0
To check the major number we used for char_drv, found using the command:
$ cat /proc/devices
It will list all the character devices and block devices. First column is Major number
and second column is drivers.
240 char_drv
we sucessfully loaded our driver and is linked to the major number 240.
We are going to do device entry here:
$ sudo mknod -m 666 /dev/char_drv c 240 0
mknode ---> make node
666 ---> read/write permission
/dev/char_drv ---> name of the driver is char_drv
c ---> represent character driver
240 ---> Major Number
0 ---> Minor Number
$ ls -l /dev/char_drv
crw-rw-rw- 1 root root 240 , 0 Jun 27 15:20 /dev/char_drv
Verifying the File Operations: open, read, write, close:
Now open two terminal namely terminal1 and terminal2.
Terminal2 – commands used to check the functions name invoked at that time which
is display in terminal1.
Terminal1 - displays the kernel messages.
3
CREATION OF SIMPLE CHARACTER DEVICE DRIVER - PC
Terminal 2 :
$ sudo insmod ./char_drv.ko ---------->
$ cat /dev/char_drv ----------->
“cat” command usually opens the file,
reads it then displays and at last close the
file.
$ echo “testing” > /dev/char_drv ------->
“echo” command usually opens the file
as /dev/char_drv, writes testing in to this
file and then closes the file.
$ sudo rmmod char_drv --------->
Terminal 1:
$ sudo tail -f /var/log/syslog
Inside the char_drv_init function
Inside the file_open function
Inside the file_read function
Inside the file_close function
Inside the file_open function
Inside the file_write function
Inside the file_close function
Inside the char_drv_exit function
Removing the Module:
To remove the module, use the command as below:
$ sudo rmmod char_drv
your module is successfully unloaded.
4

Weitere ähnliche Inhalte

Was ist angesagt?

Ansible for Beginners
Ansible for BeginnersAnsible for Beginners
Ansible for BeginnersArie Bregman
 
Shell实现的windows回收站功能的脚本
Shell实现的windows回收站功能的脚本Shell实现的windows回收站功能的脚本
Shell实现的windows回收站功能的脚本Lingfei Kong
 
(Practical) linux 101
(Practical) linux 101(Practical) linux 101
(Practical) linux 101Arie Bregman
 
(Practical) linux 104
(Practical) linux 104(Practical) linux 104
(Practical) linux 104Arie Bregman
 
Unix Programming with Perl
Unix Programming with PerlUnix Programming with Perl
Unix Programming with PerlKazuho Oku
 
Using the Power to Prove
Using the Power to ProveUsing the Power to Prove
Using the Power to ProveKazuho Oku
 
Unix And Shell Scripting
Unix And Shell ScriptingUnix And Shell Scripting
Unix And Shell ScriptingJaibeer Malik
 
Fluentd intro at Tajo seoul meetup
Fluentd intro at Tajo seoul meetupFluentd intro at Tajo seoul meetup
Fluentd intro at Tajo seoul meetupDongmin Yu
 
Quick start bash script
Quick start   bash scriptQuick start   bash script
Quick start bash scriptSimon Su
 
Unix primer
Unix primerUnix primer
Unix primerdummy
 
Unix Programming with Perl 2
Unix Programming with Perl 2Unix Programming with Perl 2
Unix Programming with Perl 2Kazuho Oku
 
Unix Commands
Unix CommandsUnix Commands
Unix CommandsDr.Ravi
 
Clojure + MongoDB on Heroku
Clojure + MongoDB on HerokuClojure + MongoDB on Heroku
Clojure + MongoDB on HerokuNaoyuki Kakuda
 
Leveraging APIs without Programming in FME Server Provisioning
Leveraging APIs without Programming in FME Server ProvisioningLeveraging APIs without Programming in FME Server Provisioning
Leveraging APIs without Programming in FME Server ProvisioningSafe Software
 

Was ist angesagt? (20)

08 php-files
08 php-files08 php-files
08 php-files
 
Linux Command Line
Linux Command LineLinux Command Line
Linux Command Line
 
Ansible for Beginners
Ansible for BeginnersAnsible for Beginners
Ansible for Beginners
 
Shell实现的windows回收站功能的脚本
Shell实现的windows回收站功能的脚本Shell实现的windows回收站功能的脚本
Shell实现的windows回收站功能的脚本
 
(Practical) linux 101
(Practical) linux 101(Practical) linux 101
(Practical) linux 101
 
(Practical) linux 104
(Practical) linux 104(Practical) linux 104
(Practical) linux 104
 
Unix Programming with Perl
Unix Programming with PerlUnix Programming with Perl
Unix Programming with Perl
 
Using the Power to Prove
Using the Power to ProveUsing the Power to Prove
Using the Power to Prove
 
Unix And Shell Scripting
Unix And Shell ScriptingUnix And Shell Scripting
Unix And Shell Scripting
 
Unix - Shell Scripts
Unix - Shell ScriptsUnix - Shell Scripts
Unix - Shell Scripts
 
Fluentd intro at Tajo seoul meetup
Fluentd intro at Tajo seoul meetupFluentd intro at Tajo seoul meetup
Fluentd intro at Tajo seoul meetup
 
Quick start bash script
Quick start   bash scriptQuick start   bash script
Quick start bash script
 
Unix primer
Unix primerUnix primer
Unix primer
 
Mach-O Internals
Mach-O InternalsMach-O Internals
Mach-O Internals
 
Unix Programming with Perl 2
Unix Programming with Perl 2Unix Programming with Perl 2
Unix Programming with Perl 2
 
Unix Commands
Unix CommandsUnix Commands
Unix Commands
 
Bash Scripting Workshop
Bash Scripting WorkshopBash Scripting Workshop
Bash Scripting Workshop
 
Clojure + MongoDB on Heroku
Clojure + MongoDB on HerokuClojure + MongoDB on Heroku
Clojure + MongoDB on Heroku
 
Leveraging APIs without Programming in FME Server Provisioning
Leveraging APIs without Programming in FME Server ProvisioningLeveraging APIs without Programming in FME Server Provisioning
Leveraging APIs without Programming in FME Server Provisioning
 
Examples -partII
Examples -partIIExamples -partII
Examples -partII
 

Andere mochten auch

前端构建工具
前端构建工具前端构建工具
前端构建工具wkylin
 
Getting started with social media
Getting started with social mediaGetting started with social media
Getting started with social mediaBusiness Glitter
 
EL VIAJERO DON ALEJANDRO ALDAVE
EL VIAJERO DON ALEJANDRO ALDAVEEL VIAJERO DON ALEJANDRO ALDAVE
EL VIAJERO DON ALEJANDRO ALDAVEZuniga Agustin
 
Prezentacija vračev gaj koji volim 3
Prezentacija   vračev gaj koji volim 3Prezentacija   vračev gaj koji volim 3
Prezentacija vračev gaj koji volim 3stojavracevgaj
 
Multimedia como recurso interactivo
Multimedia como recurso interactivoMultimedia como recurso interactivo
Multimedia como recurso interactivosequeracarolina
 
NOCHE EXTREMA EN LA IGLESIA DE CHIQUIAN
NOCHE EXTREMA EN LA IGLESIA DE CHIQUIANNOCHE EXTREMA EN LA IGLESIA DE CHIQUIAN
NOCHE EXTREMA EN LA IGLESIA DE CHIQUIANZuniga Agustin
 
Planeacion 8. marzio
Planeacion 8. marzioPlaneacion 8. marzio
Planeacion 8. marzioOsi Menvar
 
Planeación semanal de las actividades realizadas
Planeación semanal de las actividades realizadasPlaneación semanal de las actividades realizadas
Planeación semanal de las actividades realizadasdaisy gonzalez
 
کتاب بدن سازی با خام گیاهخواری
کتاب بدن سازی با خام گیاهخواریکتاب بدن سازی با خام گیاهخواری
کتاب بدن سازی با خام گیاهخواریFarid Kamali
 
REACTOR NUCLEAR RP10 Y FISICA DE REACTORES EN EL IPEN-PERU
REACTOR NUCLEAR RP10 Y FISICA DE REACTORES EN EL IPEN-PERUREACTOR NUCLEAR RP10 Y FISICA DE REACTORES EN EL IPEN-PERU
REACTOR NUCLEAR RP10 Y FISICA DE REACTORES EN EL IPEN-PERUZuniga Agustin
 
Reflections from beyond the digital team | Panel: the death of the digital sp...
Reflections from beyond the digital team | Panel: the death of the digital sp...Reflections from beyond the digital team | Panel: the death of the digital sp...
Reflections from beyond the digital team | Panel: the death of the digital sp...CharityComms
 
Feridun Zaimoglu i migrantska književnost u Nemačkoj
Feridun Zaimoglu i migrantska književnost u NemačkojFeridun Zaimoglu i migrantska književnost u Nemačkoj
Feridun Zaimoglu i migrantska književnost u NemačkojJelena Kostic-Tomovic
 

Andere mochten auch (20)

前端构建工具
前端构建工具前端构建工具
前端构建工具
 
Getting started with social media
Getting started with social mediaGetting started with social media
Getting started with social media
 
BCCI
BCCIBCCI
BCCI
 
EL VIAJERO DON ALEJANDRO ALDAVE
EL VIAJERO DON ALEJANDRO ALDAVEEL VIAJERO DON ALEJANDRO ALDAVE
EL VIAJERO DON ALEJANDRO ALDAVE
 
Prezentacija vračev gaj koji volim 3
Prezentacija   vračev gaj koji volim 3Prezentacija   vračev gaj koji volim 3
Prezentacija vračev gaj koji volim 3
 
Multimedia como recurso interactivo
Multimedia como recurso interactivoMultimedia como recurso interactivo
Multimedia como recurso interactivo
 
Ejercicio clase
Ejercicio claseEjercicio clase
Ejercicio clase
 
NOCHE EXTREMA EN LA IGLESIA DE CHIQUIAN
NOCHE EXTREMA EN LA IGLESIA DE CHIQUIANNOCHE EXTREMA EN LA IGLESIA DE CHIQUIAN
NOCHE EXTREMA EN LA IGLESIA DE CHIQUIAN
 
Planeacion 8. marzio
Planeacion 8. marzioPlaneacion 8. marzio
Planeacion 8. marzio
 
Planeación semanal de las actividades realizadas
Planeación semanal de las actividades realizadasPlaneación semanal de las actividades realizadas
Planeación semanal de las actividades realizadas
 
کتاب بدن سازی با خام گیاهخواری
کتاب بدن سازی با خام گیاهخواریکتاب بدن سازی با خام گیاهخواری
کتاب بدن سازی با خام گیاهخواری
 
REACTOR NUCLEAR RP10 Y FISICA DE REACTORES EN EL IPEN-PERU
REACTOR NUCLEAR RP10 Y FISICA DE REACTORES EN EL IPEN-PERUREACTOR NUCLEAR RP10 Y FISICA DE REACTORES EN EL IPEN-PERU
REACTOR NUCLEAR RP10 Y FISICA DE REACTORES EN EL IPEN-PERU
 
50 iş günü staj defteri(düzenlenmiş)
50 iş günü staj defteri(düzenlenmiş)50 iş günü staj defteri(düzenlenmiş)
50 iş günü staj defteri(düzenlenmiş)
 
Fraudes en la red
Fraudes en la redFraudes en la red
Fraudes en la red
 
Plant glycosides
Plant glycosidesPlant glycosides
Plant glycosides
 
Theories of Communication
Theories of CommunicationTheories of Communication
Theories of Communication
 
Reflections from beyond the digital team | Panel: the death of the digital sp...
Reflections from beyond the digital team | Panel: the death of the digital sp...Reflections from beyond the digital team | Panel: the death of the digital sp...
Reflections from beyond the digital team | Panel: the death of the digital sp...
 
G_VALLE_RESUME3
G_VALLE_RESUME3G_VALLE_RESUME3
G_VALLE_RESUME3
 
Planeación semanal 2
Planeación semanal 2Planeación semanal 2
Planeación semanal 2
 
Feridun Zaimoglu i migrantska književnost u Nemačkoj
Feridun Zaimoglu i migrantska književnost u NemačkojFeridun Zaimoglu i migrantska književnost u Nemačkoj
Feridun Zaimoglu i migrantska književnost u Nemačkoj
 

Ähnlich wie Character_Device_drvier_pc

Writing Character driver (loadable module) in linux
Writing Character driver (loadable module) in linuxWriting Character driver (loadable module) in linux
Writing Character driver (loadable module) in linuxRajKumar Rampelli
 
Hands_on_multipath_p1.pdf
Hands_on_multipath_p1.pdfHands_on_multipath_p1.pdf
Hands_on_multipath_p1.pdfHossein Mehrara
 
exercises-log-management-rsyslog.pdf
exercises-log-management-rsyslog.pdfexercises-log-management-rsyslog.pdf
exercises-log-management-rsyslog.pdfSngB2
 
Unix.system.calls
Unix.system.callsUnix.system.calls
Unix.system.callsGRajendra
 
Asian Spirit 3 Day Dba On Ubl
Asian Spirit 3 Day Dba On UblAsian Spirit 3 Day Dba On Ubl
Asian Spirit 3 Day Dba On Ublnewrforce
 
Chapter 3 Using Unix Commands
Chapter 3 Using Unix CommandsChapter 3 Using Unix Commands
Chapter 3 Using Unix CommandsMeenalJabde
 
파이썬 개발환경 구성하기의 끝판왕 - Docker Compose
파이썬 개발환경 구성하기의 끝판왕 - Docker Compose파이썬 개발환경 구성하기의 끝판왕 - Docker Compose
파이썬 개발환경 구성하기의 끝판왕 - Docker Composeraccoony
 
A character device typically transfers data to and from a user appli.pdf
A character device typically transfers data to and from a user appli.pdfA character device typically transfers data to and from a user appli.pdf
A character device typically transfers data to and from a user appli.pdfaptind
 
A character device typically transfers data to and from a user appli.pdf
A character device typically transfers data to and from a user appli.pdfA character device typically transfers data to and from a user appli.pdf
A character device typically transfers data to and from a user appli.pdfaptind
 
Beyond Golden Containers: Complementing Docker with Puppet
Beyond Golden Containers: Complementing Docker with PuppetBeyond Golden Containers: Complementing Docker with Puppet
Beyond Golden Containers: Complementing Docker with Puppetlutter
 
Intrusion Detection System using Snort
Intrusion Detection System using Snort Intrusion Detection System using Snort
Intrusion Detection System using Snort webhostingguy
 
Intrusion Detection System using Snort
Intrusion Detection System using Snort Intrusion Detection System using Snort
Intrusion Detection System using Snort webhostingguy
 
linux_Commads
linux_Commadslinux_Commads
linux_Commadstastedone
 
Sheet1DAKOTA OFFICE PRODUCTSWrite in the names of yourActivitycost.docx
Sheet1DAKOTA OFFICE PRODUCTSWrite in the names of yourActivitycost.docxSheet1DAKOTA OFFICE PRODUCTSWrite in the names of yourActivitycost.docx
Sheet1DAKOTA OFFICE PRODUCTSWrite in the names of yourActivitycost.docxlesleyryder69361
 
RHCSA EX200 - Summary
RHCSA EX200 - SummaryRHCSA EX200 - Summary
RHCSA EX200 - SummaryNugroho Gito
 

Ähnlich wie Character_Device_drvier_pc (20)

Writing Character driver (loadable module) in linux
Writing Character driver (loadable module) in linuxWriting Character driver (loadable module) in linux
Writing Character driver (loadable module) in linux
 
Hands_on_multipath_p1.pdf
Hands_on_multipath_p1.pdfHands_on_multipath_p1.pdf
Hands_on_multipath_p1.pdf
 
exercises-log-management-rsyslog.pdf
exercises-log-management-rsyslog.pdfexercises-log-management-rsyslog.pdf
exercises-log-management-rsyslog.pdf
 
lec4.docx
lec4.docxlec4.docx
lec4.docx
 
Unix.system.calls
Unix.system.callsUnix.system.calls
Unix.system.calls
 
Linux configer
Linux configerLinux configer
Linux configer
 
Asian Spirit 3 Day Dba On Ubl
Asian Spirit 3 Day Dba On UblAsian Spirit 3 Day Dba On Ubl
Asian Spirit 3 Day Dba On Ubl
 
Chapter 3 Using Unix Commands
Chapter 3 Using Unix CommandsChapter 3 Using Unix Commands
Chapter 3 Using Unix Commands
 
파이썬 개발환경 구성하기의 끝판왕 - Docker Compose
파이썬 개발환경 구성하기의 끝판왕 - Docker Compose파이썬 개발환경 구성하기의 끝판왕 - Docker Compose
파이썬 개발환경 구성하기의 끝판왕 - Docker Compose
 
Book
BookBook
Book
 
A character device typically transfers data to and from a user appli.pdf
A character device typically transfers data to and from a user appli.pdfA character device typically transfers data to and from a user appli.pdf
A character device typically transfers data to and from a user appli.pdf
 
A character device typically transfers data to and from a user appli.pdf
A character device typically transfers data to and from a user appli.pdfA character device typically transfers data to and from a user appli.pdf
A character device typically transfers data to and from a user appli.pdf
 
Beyond Golden Containers: Complementing Docker with Puppet
Beyond Golden Containers: Complementing Docker with PuppetBeyond Golden Containers: Complementing Docker with Puppet
Beyond Golden Containers: Complementing Docker with Puppet
 
Linux.pdf
Linux.pdfLinux.pdf
Linux.pdf
 
Intrusion Detection System using Snort
Intrusion Detection System using Snort Intrusion Detection System using Snort
Intrusion Detection System using Snort
 
Intrusion Detection System using Snort
Intrusion Detection System using Snort Intrusion Detection System using Snort
Intrusion Detection System using Snort
 
linux_Commads
linux_Commadslinux_Commads
linux_Commads
 
Sheet1DAKOTA OFFICE PRODUCTSWrite in the names of yourActivitycost.docx
Sheet1DAKOTA OFFICE PRODUCTSWrite in the names of yourActivitycost.docxSheet1DAKOTA OFFICE PRODUCTSWrite in the names of yourActivitycost.docx
Sheet1DAKOTA OFFICE PRODUCTSWrite in the names of yourActivitycost.docx
 
Basic Linux kernel
Basic Linux kernelBasic Linux kernel
Basic Linux kernel
 
RHCSA EX200 - Summary
RHCSA EX200 - SummaryRHCSA EX200 - Summary
RHCSA EX200 - Summary
 

Character_Device_drvier_pc

  • 1. CREATION OF SIMPLE CHARACTER DEVICE DRIVER - PC Character device driver for PC Create simple code for character device driver: Create a new directory inside the source code and create files in this directory: $ vi char_drv.c #include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/fs.h> int file_open (struct inode *pinode, struct file *pfile) { printk(KERN_INFO "Inside the %s functionn",__FUNCTION__); return 0; } size_t file_read (struct file *pfile, char __user *buffer, size_t length, loff_t *offset) { printk(KERN_INFO "Inside the %s functionn",__FUNCTION__); return 0; } ssize_t file_write (struct file *pfile, const char __user *buffer, size_t length, loff_t *offset) { printk(KERN_INFO "Inside the %s functionn",__FUNCTION__); return length; } int file_close (struct inode *pinode, struct file *pfile) { printk(KERN_INFO "Inside the %s functionn",__FUNCTION__); return 0; } /* To hold the file operations performed on the device */ struct file_operations fops = { .owner = THIS_MODULE, .open = file_open, .read = file_read, .write = file_write, .release = file_close, }; 1
  • 2. CREATION OF SIMPLE CHARACTER DEVICE DRIVER - PC int char_drv_init(void) { printk(KERN_INFO "Inside the %s functionn",__FUNCTION__); /* Register with the kernel and indicate that we are registering character device driver */ register_chrdev(240 /* Major Number */, "char_drv" /* Name of the driver */, &fops /* file operations */); return 0; } void char_drv_exit(void) { printk(KERN_INFO "Inside the %s functionn",__FUNCTION__); /* unregister the character device driver */ unregister_chrdev(240, "char_drv"); } module_init(char_drv_init); module_exit(char_drv_exit); Create Makefile: $ vi Makefile obj-m += char_drv.o Building the Driver: $ make -c /lib/modules/$(uname -r)/build M=$PWD modules after using the above command, you get kernel object file “char_dev.ko”. 2
  • 3. CREATION OF SIMPLE CHARACTER DEVICE DRIVER - PC Inserting the char_drv module: $ sudo insmod ./char_drv.ko Now you can check the driver get loaded by using below command $ lsmod | grep char char_drv 12767 0 To check the major number we used for char_drv, found using the command: $ cat /proc/devices It will list all the character devices and block devices. First column is Major number and second column is drivers. 240 char_drv we sucessfully loaded our driver and is linked to the major number 240. We are going to do device entry here: $ sudo mknod -m 666 /dev/char_drv c 240 0 mknode ---> make node 666 ---> read/write permission /dev/char_drv ---> name of the driver is char_drv c ---> represent character driver 240 ---> Major Number 0 ---> Minor Number $ ls -l /dev/char_drv crw-rw-rw- 1 root root 240 , 0 Jun 27 15:20 /dev/char_drv Verifying the File Operations: open, read, write, close: Now open two terminal namely terminal1 and terminal2. Terminal2 – commands used to check the functions name invoked at that time which is display in terminal1. Terminal1 - displays the kernel messages. 3
  • 4. CREATION OF SIMPLE CHARACTER DEVICE DRIVER - PC Terminal 2 : $ sudo insmod ./char_drv.ko ----------> $ cat /dev/char_drv -----------> “cat” command usually opens the file, reads it then displays and at last close the file. $ echo “testing” > /dev/char_drv -------> “echo” command usually opens the file as /dev/char_drv, writes testing in to this file and then closes the file. $ sudo rmmod char_drv ---------> Terminal 1: $ sudo tail -f /var/log/syslog Inside the char_drv_init function Inside the file_open function Inside the file_read function Inside the file_close function Inside the file_open function Inside the file_write function Inside the file_close function Inside the char_drv_exit function Removing the Module: To remove the module, use the command as below: $ sudo rmmod char_drv your module is successfully unloaded. 4