Sunday, 29 January 2017

Find Greatest Among 3 Numbers


1. Function to Find Greatest Among Three Numbers using if else statement.

int greatest_among_three(const int num1, const int num2, const int num3){
        int result=0;
        if( num1 > num2){
                if( num1 > num3){
                        result = num1;
                }else{
                        result = num3;
                }
        }else if(num2 > num3){
                result = num2;
        }else{
                result = num3;
        }
        return result;
}

Full Program Code ...

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int greatest_among_three(const int num1, const int num2, const int num3){
        int result=0;
        if( num1 > num2){
                if( num1 > num3){
                        result = num1;
                }else{
                        result = num3;
                }
        }else if(num2 > num3){
                result = num2;
        }else{
                result = num3;
        }
        return result;
}

int main(int argc, const char *argv[]){
        int num1=0, num2=0, num3=0;
        srand(time(NULL));
        for( int i=0; i< 10; i++){
                num1 = rand()%1000;
                num2 = rand()%1000;
                num3 = rand()%1000;
                printf("Greatest among (%d,%d,%d) is %d\n", num1, num2, num3, greatest_among_three(num1, num2, num3));
        }
        return 0;
}

Sunday, 14 August 2016

hello world c


#include<stdio.h>

int main(int argc, char *argv[]){
    print("Hello World\n");
    return 0;
}

Monday, 7 December 2015

ABOUT ME

Rajesh Kumar Sahoo
e-mail: sahoorajesh.d@gmail.com
mobile: +91-9853493896

Friday, 20 February 2015

Factorial Of A Number in C

Write A Program to Find Factorial Of A Number

Problem Can Be Solved Using 

1. Recursion

#include<stdio.h>
int fact(int num){
if((num == 0)||(num == 1))
return 1;
else
return num*fact(num-1);
}
int main(int argc, char *argv[]){
int num;
num = atoi(argv[1]);
printf("%d!=%d\n",num,fact(num));
return 0;
}
Compiling and Running Under Linux

rajesh@ThinkPad:~/Rajesh/blogs$ gcc factorial.c -o factnum
rajesh@ThinkPad:~/Rajesh/blogs$ ./factnum 4
4!=24
rajesh@ThinkPad:~/Rajesh/blogs$ ./factnum 5
5!=120
rajesh@ThinkPad:~/Rajesh/blogs$ ./factnum 6
6!=720

2. Using Loop

#include<stdio.h> 
int fact(int num){
int res;
res=1;
while(num){
res*=num;
num--;
}
return res;
}
int main(int argc, char *argv[]){
int num;
num = atoi(argv[1]);
printf("%d!=%d\n",num,fact(num));
return 0;
}
Compiling and Running Under Linux

rajesh@ThinkPad:~/Rajesh/blogs$ gcc factorial.c -o factnum
rajesh@ThinkPad:~/Rajesh/blogs$ ./factnum 4
4!=24
rajesh@ThinkPad:~/Rajesh/blogs$ ./factnum 5
5!=120
rajesh@ThinkPad:~/Rajesh/blogs$ ./factnum 6
6!=720

Thursday, 20 February 2014

Hello World Kernel Module

Hello World Kernel

file hello.c

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


#define HELLO_AUTHOR "Rajesh6115 <sahoorajesh.d@gmail.com>"
#define HELLO_DESC "A Simple Hello World Module"

static int __init hello_init(void);
static void __exit hello_exit(void);

MODULE_LICENSE("GPL");
MODULE_AUTHOR(HELLO_AUTHOR); /* Who wrote this module? */
MODULE_DESCRIPTION(HELLO_DESC); /* What does this module do */

static int __init hello_init(void){
    printk(KERN_INFO "Hello World Linux Kernel --------INIT Message");
return 0;
}

static void __exit hello_exit(void){
    printk(KERN_INFO "Good Bye Linux Kernel --------EXIT Message");
}

module_init(hello_init);
module_exit(hello_exit);

BUILD AND RUNNIG A Hello MODULE

For Building a Module We Normally Take the help of Kernel Build Script.
which present in '/lib/modules/<kernel version>/build' path.

Make File for Hello Module

obj-m += hello.o

all:
$(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
$(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

Compile by make command

Out put in my system

rajesh@ubuntu:~/rajesh/kernelmod/hello$ make
make -C /lib/modules/3.12.6-rajesh-first/build M=/home/rajesh/rajesh/kernelmod/hello modules
make[1]: Entering directory `/host/linux-3.12.6'
  CC [M]  /home/rajesh/rajesh/kernelmod/hello/hello.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/rajesh/rajesh/kernelmod/hello/hello.mod.o
  LD [M]  /home/rajesh/rajesh/kernelmod/hello/hello.ko
make[1]: Leaving directory `/host/linux-3.12.6'
rajesh@ubuntu:~/rajesh/kernelmod/hello$
Some New files will generate in same directory
And our driver file is 'hello.ko'

Run/Load the module by using 'insmod' Command
To execute that Command you should be root user or use sudo to execute

rajesh@ubuntu:~/rajesh/kernelmod/hello$ sudo insmod ./hello.ko
[sudo] password for rajesh:
rajesh@ubuntu:~/rajesh/kernelmod/hello$
'printk' function log the message to kernel log file.And we can see the same by 'dmesg' command
rajesh@ubuntu:~/rajesh/kernelmod/hello$ dmesg | tail -5
[ 1094.480372] audit_printk_skb: 177 callbacks suppressed
[ 1094.480375] type=1400 audit(1392792445.131:71): apparmor="DENIED" operation="capable" parent=1 profile="/usr/sbin/cupsd" pid=797 comm="cupsd" pid=797 comm="cupsd" capability=36  capname="block_suspend"
[ 1599.091190] perf samples too long (2510 > 2500), lowering kernel.perf_event_max_sample_rate to 50000
[ 6380.140339] Hello World Linux Kernel --------INIT Message

Unload/Remove the module by using 'rmmod' Command
To execute that Command you should be root user or use sudo to execute

rajesh@ubuntu:~/rajesh/kernelmod/hello$ sudo rmmod hello
Again we will see exit message using 'dmesg'
rajesh@ubuntu:~/rajesh/kernelmod/hello$ dmesg | tail -5
[ 1094.480375] type=1400 audit(1392792445.131:71): apparmor="DENIED" operation="capable" parent=1 profile="/usr/sbin/cupsd" pid=797 comm="cupsd" pid=797 comm="cupsd" capability=36  capname="block_suspend"
[ 1599.091190] perf samples too long (2510 > 2500), lowering kernel.perf_event_max_sample_rate to 50000
[ 6380.140339] Hello World Linux Kernel
[ 6423.920972] Good Bye Linux Kernel --------EXIT Message

Find Same Code in github at 

https://github.com/rajesh6115/kernelStudy/tree/master/hello