Search This Blog

Showing posts with label LINUX. Show all posts
Showing posts with label LINUX. Show all posts

Friday, May 20, 2016

Basic Char Driver - Linux Device Driver

Here I am going to write on simple basic module which can load into kernel and  can remove from kernel . This sample module do nothing we can see the module loading and removing message in syslog.

First the useful things to write code

  • Your favorite code editor 
    • Here I used geany code editor
  • Header files
    • basic header file/libraries you must include in c file are 
      • linux/init.h
      • linux/module.h
  • Basic functions
    • basic functions/macros to add your function to kernal ar
      • module_init(fun);
      • module_exit(fun); 
  • Makefile
  • some commands to view the log
c code - filename : ex1_simple_module.c

#include <linux\init.h>
#include <linux\module.h>
int ex1_simple_module_init(void)
{
    printk(KERN_ALERT"Module ex1 Inside the Function %s",__FUNCTION__);
    return 0;
}
void ex1_simple_module_exit(void)
{
    printk(KERN_ALERT"Module ex1 Inside the Function %s",__FUNCTION__);
}

module_init(ex1_simple_module_init);
module_exit(ex1_simple_module_exit);



the above c code havin 2 user function
ex1_simple_module_init() and ex1_simple_module_exit()
and both having printk functions . the printk function send the data to system log . we can see that data in "var/log/syslog" file

module_init() function take the function as argument and sends that function to kernel as init funtion and module_exit() function send the function to kernel as exit function

to compile the kernel we need to know the current kernel version . To get that we can use the command


uname -r



Now write the make file to compile the module

Create a file with name Makefile and insert the below code in that

obj - m := ex1_simple_module.o


here the file name is ex1_simple_module.c so we use ex1_simple_module.o as output .This simple line in the makefile compile our c code and generate object file.

To run the  Makefile we need to use the below command in bas terminal

make -C /lib/modules/$(uname -r)/build M=$PWD modules




here the kernel version is got from uname -r command and the source file directory is fetch from PWD command. So we must run this command from the directory where the source file is stored

now the source is compiled and the .ko files are generated now we can insert and remove the module to kernel.To do this first we need to follow the syslog. For that we can us the below command in terminal.

sudo tail -f /var/log/syslog


here tail means print the tail part(last lines)
        -f means follow the last lines in the file syslog

after this command current terminal will the print syslog data so we need to open the other terminal to insert the module into kernel

To insert the module use insmod command and to remove use rmmod command

sudo insmod ./ex1_simple_module.ko

sudo rmmod ex1_simple_module






That it . I hope this post is useful to you .Please comment below and check out other posts and my youtube channel



Wednesday, June 4, 2014

Compiling C++ program in linux


Compiling C++ program in linux is little bit different than Compiling a C program ,

             gcc compiler is the commonly used compiler for c in linux but for compiling c++ program in linux we need to use another compiler called g++ .the syntax for this is

g++ <your_program>

It compiles the program and gives a.out as the output

But there is a problem with this , if any of the modern concepts like range for is used in the program it gives an error like,

15.cpp: In function ‘int main()’:
15.cpp:10:14: error: range-based ‘for’ loops are not allowed in C++98 mode
 for(char c : str){
 
the modern concepts are compiled with C++11 supported compiled. so there is a need to use extra option for compiling this.


i.e.,

g++ -std=c++0x <your program>

Using this flag every time is somewhat difficult so we nee to enable c++11 perminently

To set that first open the terminal

sudo gedit ~/.bashrc

then a file will be opend , now add below line as the last line

alias g++="g++ --std=c++0x" 

then save the file and restart the terminal then compile the program as

g++ <your program>


if you want to compile non c++11 program then you must use
g++ --std=c++98 <your program>




Wednesday, August 28, 2013

compiling c program-linux

Compiling a c program in linux:

to run a c program in your linux machine you need 2 software packages

1.vi or vim editor
2.gcc compiler


check those software by typing the gcc and vi or vim command on the terminal
if it shows command not found then you need to install that software

to do so a simple command is useful for you

yum install software

yum command automatically searches the software package and installs the latest one

so type
yum install gcc vi vim

the it automatically install the 3 packages

vi and vim both are code editors vi is a default one but it show the code in single color
where as vim show the different colors .like variables,strings,functions...etc

its look like



see the colors in vim editor






after installing the packages first create  c file uisng vim editor

vim hw.c

its creats hw.c file and enter into editor now editor is in command mode so you need to enter into insert mode to edit the code for the type 'i'
the edit the code

the press 'esc' key to enter into command mode the ':x' to save and exit

* :x<Return> quit vi, writing out modified file to file named in original invocation
  :wq<Return> quit vi, writing out modified file to file named in original invocation
  :q<Return> quit (or exit) vi
* :q!<Return> quit vi even though latest changes have not been saved for this vi call


after exiting the editor compile the code using gcc compiler

gcc hw.c------------by default it outputs the file with a.out name

gcc hw.c -o hw.o----now the output fie name is hw.o

if there are no errors it will creates the output file
to run that file type

./a.out   or   ./hw.o

then your code will shows the output..


Tuesday, August 27, 2013

create folder-linux

Creating a folder

mkdir is a simple command to create a folder/directory

mkdir espot

then espot directory is created

cd espot

navigates to 'espot' directory

here  
ls
command  shows the files and directories with in present directory




remove folder -linux

Command to remove a empty and non-empty directory:

rm directory
or
rmdir directory

both will deletes the 'directory' if it is an empty directory

and
if it is not a empty directory then these commands will return an error message

for rm command:
rm: cannot remove ‘directory’: Is a directory

for rmdir command:
rmdir: failed to remove ‘directory’: Directory not empty


Solution is with rmdir we can not delete the non-empty directory

by using rm command with argument

rm -rf directory

 Here,




-r, -R, --recursive
              remove directories and their contents recursively

-f, --force
              ignore  nonexistent  files  and  arguments,  never
              prompt