Featured

[C] program to swap two numbers without third variable


#include<stdio.h>
int main(void)
{

int input1,input2;
int ret;
printf(“enter two numbers please\n”);
scanf(“%d %d”,&input1, &input2);
printf(“Hey,You’ve entered two numbers input1=%d and input2=%d\n”,input1,input2);
input2=(input1+input2)-(input1=input2);
printf(“and swapped values of inputs are input1=%d and input2=%d\n”,input1,input2);
return 0;
}

Featured

[Uboot][Linux-OS]How to boot zImage (os image) using uboot


There are two ways to boot zImage using uboot

1. Using mkImage command, convert zImage to uImage

2. Enable bootz feature in the uboot board config file

Add “#define CONFIG_CMD_BOOTZ “in the corresponding board config file and build the new uImage

 

 

Featured

[Linux-Admin]what is the maximum number of threads and processes possible


Maximum number of threads and processes possible on system wide , are stored in

/proc/sys/kernel/threads-max

/proc/sys/kernel/pid_max

Featured

[Linux][Memory Mapping]Linear mapping equation among Kernel base , page offset, physical start and memory start


For the linear mapping the following equation should be true:

KERNELBASE – PAGE_OFFSET = PHYSICAL_START – MEMORY_START

Also, KERNELBASE >= PAGE_OFFSET and PHYSICAL_START >= MEMORY_START

where

KERNELBASE ->the virtual address of the start of the kernel

PAGE_OFFSET->the virtual address of the start of lowmem

PHYSICAL_START-> the physical address of the start of the kernel

MEMORY_START -> the physical address of the start of lowmem

 

 

Featured

[Linux driver]Example situation for tasklet usage


The tasklet mechanism is useful in interrupt handling situation , where the hardware interrupt must be managed as quickly as possible and most of the data management can be safely delayed to a later time.

Featured

[Linx driver]Differences between tasklet and workqueue


  • Tasklet run in software interrupt context with the result that all tasklet code must be atomic. Instead, workqueue functions run in the context of a special kernel process; as a result, they have more flexibility. In particular, work queue functions can sleep.
  • Tasklets always run on the processor from which they were originally submitted, Workqueues work in the same way , by default.
  • Kernel code can request that the execution of workqueue functions be delayed for an explicit interval.
  • tasklets execute quickly, for a short period of time and in atomic mode . Workqueue functions may have higher latency but need not be atomic.

But both workqueue and tasklet has situations where it is appropriate.

 

Featured

[Linux]A glance at data types used by kernel data


Data types used by kernel data divided into three main classes . Those are

  • standard C types. eg:- int
  • explicitly sized types . eg:- u32
  • Interface specific types => types used for specific kernel objects . eg:- pid_t

 

Featured

[Linux][C]How to specify a process as the owner of the file ?


When a process invokes F_SETOWN command using fnctl system call, the process ID of the owner process is saved in filp->f_owner which can be used to address the file .

Usage eg:-

     fd = open(“/dev/filename”, O_RDONLY);

        if (fd < 0) {
                fprintf(stderr, ” failed to open /dev/filename \n”);
                return;
        }

        if ((fcntl(fd, F_SETOWN, getpid()) == 1) ||
                ((value = fcntl(fd, F_GETFL)) == 1) ||
                (fcntl(fd, F_SETFL, value | O_ASYNC) == 1)) {
                fprintf(stderr, ” fcntl failed\n”);
                exit;
        }

 

 

Featured

[Linux]What is Thundering herd problem in Linux point of view ?


We studied about the forms of wait_event and wake_up macros in Linux device driver text book. These macros are used to sleep the process while waiting for an event and  wake up from the sleep state when even occurs .

So when a process calls wake_up on a wait queue, all processes waiting on that wait queue are made runnable.This is the correct behavior in many cases. But in some other cases, it is possible to know ahead of time that only one of the processes being awakened will succeed in obtaining the desired resource, and rest will simply have to sleep again. For going to sleep again, each one of the processes has to obtain  the processor , contend for the resource(and any governing locks), and explicitly go to sleep. If the number of processes in the wait queue is large ,  this steps will cause the thundering herd behavior to the system which create serious degradation in the performance of the system .  

Featured

[Linux][C]Which are all the file operations affected by the nonblocking flag (O_NONBLOCK) ?


The file operations affected by the non blocking flag are

  • open
  • read
  • write

 

 

Featured

[Linux]A little about Timer Wheel


The original kernel timer system is known as Timer wheel.

It is based on incrementing kernel- internal value (jiffie) on every timer interrupt.

This timer interrupt becomes the default scheduling quantum.

All the other timers are based on jiffies.

Featured

[Linux driver]A Little about put_user & __put_user


 

  • put_user and __put_user used to write the datum to user space
  • Function Declaration of put_user => put_user(datum, ptr) 

         Function Declaration of __put_user => __put_user(datum, ptr)

When single values are to be transferred, put_user/ __put_user should be called instead copy_to_user . Reason behind this is 

      these functions(put_user/__put_user ) are relatively fast.

 

Difference between put_user and __put_user ?

put_user checks to ensure that the process is able to write to the given memory address. put_user calls access_ok function to ensure the memory access.

__put_user performs less checking (it does not call access_ok). So it is faster than put_user. This function will fail only when the memory pointed to is not writable by the user. So it is better to call access_ok function before __put_user is called.

Where to use __put_user function ?

  • To save a few cycles when implementing a read method
  • When you copy several items , thus call access_ok just once before the first data transfer .

 

 

Featured

[Linux]Significance of rc.local


This script will be executed *after* all the other init scripts.
 We can put  the customized script  here, which needed to be run after system up.

Also depending upon run level ,

there will be a local file.

Like if System’s runlevel is 5

/etc/rc5.d/S99local is the file . There you can put the script which is to be run after system init.

 

Featured

[Linux driver]Demonstrate Race condition in driver point of view


Lets think that the following code segment is there in the character driver code

if( ! dev_ptr->data[index] )
{
/*Allocate memory */
dev_ptr->data[index] = kmalloc(BUF_SIZE, GFP_KERNEL);

if( ! dev_ptr->data[index] )
{
goto out;
}

}

Suppose for a moment that two processes, A and B , are independently trying to write to the same offset of the character device. Each process reaches the if test in the first line of the fragment above at the same time. If the pointer in the question is NULL, each process will decide to allocate memory, and each will assign the resulting pointer to dev_ptr->data[index]. Since both processes are assigning to the same location, clearly one of the assignments will prevail.

So the process that completes the assignment second will win. At that point , character device will forget entirely about the memory first one allocated. So memory allocated by first one , will be dropped and never returned to system.
This sequence of events is a demonstration of race condition .

Featured

[Linux][Driver][C]Can we override the file operation functions in driver ?


Answer is Yes , We can override the file operation functions in driver .

I wrote a character driver(hello.c) to explain the same . Also a Makefile is given to compile the hello.c file in Linux OS. An application program to test the driver also given here . Please go through it

——————————————————————–

Hello_character_driver

—————————————————————-

Makefile to Compile the driver hello.c file

—————————————————————–

Hello_Makefile

—————————————————————–

Application program to test the driver functionality

——————————————————————

hello_app

Featured

[Linux driver]How to have multiple file_operations in a character driver


In the Init function of the character driver , follow the following steps

Register a character driver with the file_operations structure with only open function defined . This is the selector for real open .

Now  create class for the module , using  class_create

Now create devices under this class , using device_create . Each devices will be having separate minor and name

In the real open function ,

Depending on the minor , which can be determined by iminor function where argument to the function is inode which is passed to the function when application calls open ,  Assign file_operations structure for different devices to  filp->f_op, where filp (data type  – struct file *)  is the second argument passed in to the open function .

 

 

 

 

 

 

 

 

 

 

 

Featured

[gcc]*** stack smashing detected ***


Stack Smashing is actually a protection mechanism used by gcc to detect buffer overflow attacks.

An input of string greater than size defined  causes corruption of gcc inbuilt protection canary variable followed by SIGABRT to terminate the program.

Disabling of this gcc protection option can be done by using compiler option -fno-stack-protector

Featured

[C]Program to find whether the given machine is Little Endian or not


/*This program is to test whether the machine Little Endian or not */

#include<stdio.h>
#include <netinet/in.h>

int main (void)
{

unsigned short n=0xABCD;
printf (“%X %X \n”, n , ntohs(n));

return 0;
}

compile the program .

Run the program on the machine which is to be checked

If the output is

ABCD CDAB  then the machine is Little Endian

If the output is

ABCD ABCD then the machine is Big Endian

This program is tested with X86 machine and power pc machine .

 

X86 machine gave the output ABCD CDAB , hence Little Endian

PowerPc machine gave ABCD ABCD, hence Big Endian

Featured

[Linux driver]Module insertion error – “version magic ‘2.6.35 SMP mod_unload ‘ should be ‘2.6.35 SMP preempt mod_unload”


If you are getting the module insertion error in kernel , “version magic ‘2.6.35 SMP mod_unload ‘ should be ‘2.6.35 SMP preempt mod_unload”, while loading even a simple character driver , then rebuild your kernel by enabling preempt option , then build the driver module and try inserting . 

Set following option in kernel config file:
CONFIG_PREEMPT=y

Close and save the file. Then compile the kernel , finally the driver. 

 

 

 

 

Featured

[C]How to compile a c program for a 32 bit machine on a 64 bit machine


gcc -m32 hello.c -o hello 

will give 32 bit binary 

In order to verify the result, you can run  the command file hello , which will output 32-bit LSB executable 

 

Featured

[Shell Script]How to execute multiple shell commands using a single shell variable ?


To execute multiple shell commands using a single shell variable,

Assign the multiple shell commands to a variable.

execute the shell variable using echo 

eg:

Find a.txt in the present directory 

Let the multiple commands are ls and grep 

Let the shell variable is cmd .

cmd=$(ls | grep a.txt )

To execute the commands ,

echo $cmd

 

cmd=$(ls | grep test.txt)

 

 

 

Featured

[Java]Steps to run java program in Windows


Install JDK

set path environment variable for Java

right click on my computer -> Advanced->Environment Variables->path

Add the following in the variable value of path after putting a semicolon

C:\Program Files\Java\jdk1.7.0_10\bin (or the location where JDK installed)

Compiling java program

compiler used for compiling java program is javac

javac AbsolutePathToFile

This command will create a class file with the same file name

Execute the java program

java command is used to execute java program

java -classpath fileLocation executable

here fileLocation is the place where java and class file is created

executable is simply the  filename without .class extension

eg:

fileLocation :- E:\java

filename :- HelloWorld.java

File content :

public class HelloWorld {

public static void main(String[] args) {
System.out.println(“Hello, World”);
}

}

 

Take any editor and paste the content . Save the file as HelloWorld.java

Now open command window (on run window, type cmd and press enter)

Next step is to compile your java code , use javac command

javac E:\java\HelloWorld.java

Next step is to execute the program

java  -classpath  E:\java\ HelloWorld

or

java  -cp  E:\java\ HelloWorld

 

……………………………………………………………………………………………………….

We can minimize the command length

For that your working directory should be in C drive .

create a new environmental variable , CLASSPATH to your working directory  (go to right click on my computer -> Advanced->Environment Variables  click on new and add CLASSPATH as variable and set variable value as your working directory)

Then if you execute the command , java  HelloWorld , it will work successfully .

Featured

[Linux]How to change the the back ground color and writing color of the message on the Linux console


For changing background color of the message on the Linux console , please read 

How to set back ground color for the message on console

For changing the writing color , please read 

How to print colorful message on console ?

You can combine both commands to create a colorful message 

eg: – green writing  on  blue background 

echo -e “33[44m 33[32m Hello world”

blue writing on grey background 

echo -e “33[47m 33[34m Hello world” 

 

 

 

Featured

[Linux]How to set back ground color for the message on console


The supported colors are red(41), green(42), yellow(43),blue(44),pink(45), greenish blue(46) , grey(47) and black(40)

Use the following command to set back ground message 

echo -e “33[xxm Hello World” where xx can be any value between 41-47 .

eg: echo -e “33[44m Hello world” will set blue back ground color 

 

Featured

[Linux]How to print colorful message on console ?


The supported colors are red(31), green(32), yellow(33),blue(34),pink(35), greenish blue(36) , grey(37) and black(30)

Use the following command to print the colorful message 

echo -e “33[xxm Hello World” where xx can be any value between 31-37 .

eg: echo -e “33[34m Hello world” will print hello world in blue color 

 

 

Featured

[Linux]Which process is the parent of all processes on the linux system


answer is init process.

Please find the information below

init is the parent of all processes on the system, it is executed by the kernel and is responsible for starting all other processes; it is the parent of all processes whose natural parents have died and it is responsible for reaping those when they die.

Processes managed by init are known as jobs, and can be further split into two types; services are supervised and re-spawned if they should terminate unexpectedly, and tasks are simply run once and not re-spawned if they should terminate.

On startup init reads the /etc/event.d directory, each file describes a job that should be managed. This includes the particulars about what
binary or shell script code should executed while the job is running, and which events can cause the job to be started or stopped.

to read about init , use the command man init

Featured

[Linux][Shell script] print fibonacci series of 12 numbers


#!/bin/sh
# Fibonacci series – eg:- “0 1 1 2 3 5 8 13 21 34 55 …”

a=0 #First value of the series
b=1 #Second value of the series
#print the first value of the series.
echo $a
#print the second value of the series.
echo $b
#Now a small mathmatics for finding other numbers in the series .

for I in 0 1 2 3 4 5 6 7 8 9 10 #You can find upto any number . Here it is upto 10 more numbers in the series .
do
# add a and b and assign to c
c=$(($a+$b))
#print c which is the next number in the Fibonacci series
echo $c
#Now assign b to a
a=$b
#Assign c to b
b=$c
done

Featured

[Shell Script][bash]Finding factorial of a given number using shell script


#!/bin/bash
#This script will print the factorial of the number passed as arguement . eg:- ./factorial.sh 4 will print 4! value

number=$1

factorial=1

i=$number

while [[ $i != 1 && $i != 0 ]]

do
factorial=`expr $factorial \* $i`
i=` expr $i – 1 `

done

echo “Factorial of $number is $factorial”

Featured

[Linux]Distribution version file for Fedora, Red-Hat, Debian & Ubuntu systems


Distribution version file for

  • Fedora ->                 /etc/fedora-release
  • Red-Hat ->               /etc/redhat-release
  • Debian & Ubuntu ->  /etc/debian-version

In Ubuntu ,  distribution release details can be seen in the file , /etc/lsb-release

 

Other distributions’s version file

  • Slackware ->/etc/slackware-version
  • SuSe -> /etc/SuSE-release
  • Gentoo -> /etc/gentoo-release
  • Mandrake -> /etc/mandrake-release
Featured

[Linux][Fedora][Ubuntu] Fix for the issue “ssh connection refused “


Fedora –

Install openssh-server as below

sudo yum install openssh-server

or if you are not in sudoer list , become root user and install openssh-server (yum install openssh-server)

Ubuntu –

Install openssh-server –

sudo apt-get install openssh-server

or if you are not in sudoer list , become root user and install openssh-server (apt-get install openssh-server)

Featured

[Linux]How to know UUID and file type of a device


UUID (Universally Unique Identifier) and file type can get from

  • blkid(print block device attributes like filesystem type , UUID ) . You should be root user .

UUID (Universally Unique Identifier) also can get from

  • ls -l  /dev/disk/by-uuid  (root user)

 

Featured

[Linux]How to edit fstab for auto mounting hard disk partition


  • become root user 
  • Back up fstab (cp /etc/fstab /etc/fstab.old)
  • fstab requires UUID (Universally Unique Identifier) of the partition , mount point and file type .
  • UUID and file type will get by executing the command blkid (root user ) 
  • Now edit  the /etc/fstab (root user) -. eg – UUID=0ABB /media/disk ntfs defaults 0 2 
  •  save the /etc/fstab
  • Reboot the system to test the modification. 
  1. Here UUID=type the value got while executing blkid 
  2. create a directory to mount (eg- /media/disk)
  3. next file type (given by blkid command)
  4. next is option (use defaults ) .
  5. Dump is seldom used . So put it as 0
  6. Next filesystem check order (fsck order). Use “1” for  root partition, / and 2 for the rest 
Featured

[Linux][C]How to add user defined include directory during compilation


User defined  include directories can be added during compilation .

eg:

Write a simple c program

#include <hello.h>

int main(void)
{
printf(“Hello world \n”);
return 0;
}

Save the program as hello.c

Now create a directory named  include  in your working directory . Create an  hello.h inside include directory .

The content of hello.h is

#ifndef __HELLO_HH__

#include <stdio.h>

#endif

save the file as hello.h in the include directory

If you compile hello.c using gcc “gcc hello.c “, you will get the following error

hello.c:1:19: error: hello.h: No such file or directory
hello.c: In function main:
hello.c:4: warning: incompatible implicit declaration of built-in function printf

Reason – Compiler will check for hello.h in the system’s include directory . To fix this error , we need to inform the gcc compiler the path to search for hello.h. This can be done by adding -I followed by path  where headers are kept . 

Now compile the same using the following command “gcc hello.c -I include

This command will create an executable named a.out . a.out will print hello world on execution.

Featured

[Linux]how to use terminal (ttyS0, ttyS1)


  • install minicom if minicom is not installed 
  • login as root 
  • open minicom with minicom -s 
  • select “serial port setup”
  • change the Serial device to /dev/ttyS0 or /dev/ttyS1
  • change baudrate to required value 
  • save setup as default and exit (not exit from minicom )

 

Featured

[Linux]mounting floppy disc on linux


check whether floppy driver module inserted  in the  OS by executing the following command

lsmod | grep floppy

If user is a sudoer , then follow the below steps

If module is not present , insert the module by “sudo modprobe floppy

sudo udisks  –mount /dev/fd0 /media/floppy0 

now floppy content will be shown at /media/floppy0 .

After the completion of work ,  eject the floppy using following command.

sudo umount /media/floppy0

 

If user is not a sudoer, then log in as root and follow the below steps

modprobe floppy

udisks  –mount /dev/fd0 /media/floppy0

now floppy content will be shown at /media/floppy0 .

After the completion of work ,  eject the floppy using following command.

umount /media/floppy0

 

Featured

[Linux]what is DAEMONS ? example for daemons


Daemons are server processes that run continuously in background, which often starts at boot time . Typically daemon names end with the letter d .

Main tasks for daemon 

 – Serve the function of responding to network requests, hardware activity, or other programs by performing some task.

 – Configure hardware (like udevd on linux system)

 – Run scheduled tasks (like cron job)

 – Perform a variety of other tasks.

eg :- syslogd , sshd , xinetd 

Featured

[Fedora][Linux]How to set up static ip address on fedora


This is for fedora OS

System->network->network configuration->devices->eth0->Ethernet Device->General

tick the following

  • controlled by Network manager
  • Activate device when computer starts
  • statically set IP addresses – update address, subnet mask , default gateway address, primary DNS and secondary DNS
Also make sure that you updated ifcfg-eth0 (adding ip , net mask, gateway) in the directory (/etc/sysconfig/network-scripts)

restart the system  or the network service (service network restart)

Featured

Did you hear about sha-bang ?


The Sha-bang (#!) at the head of a script tells Linux system that this file is a set of commands to be fed to the command interpreter indicated. Immediate following the sha-bang is a path-name

eg:- #!/bin/bash

Is it worth reading ?  Please like the post if its helpful. Please give your suggestions too 

Featured

[Linux][Shell Script]Create shortcut in bash


You can create shortcut in a bash shell

  • Go to home directory (cd ~)
  • Edit .bashrc file
  • At the end of the file , you can see “User specific aliases and functions
  • write shortcuts there in the following format “alias t=’cd /home/username/test’ ” (test is just a directory name. You can give any directory name which is present in your home directory)
  • To test the above settings, execute “bash” on your terminal. Then enter “t” . It should go to the directory you set in the .bashrc file
  • Same way, vi editor can be aliased to vim . Paste the following in .bashrc file ,  alias vi=vim
  • To set the tab space for vi editor, edit .vimrc file , copy the following  , set tabstop=4
Is it worth reading ?  Please like the post if its helpful. Please give your suggestions too
Featured

[Fedora][Ubuntu][Linux admin]How to configure yum (in fedora) and apt-get (in ubuntu) with proxy


Steps to configure yum in fedora

  •  Edit yum.conf in /etc folder . Update proxy (eg: proxy=http://ipaddress:port)
  •  Comment baseurl in fedora.repo file in the directory, /etc/yum.repos.d
  •  Export http_proxy from terminal (export http_proxy=http://ipaddress:port).  eg: http_proxy=http://192.168.100.100:80.

Steps to configure apt-get in Ubuntu

  • Create the file /etc/apt/apt.conf if not present
  • Add the following line
  • Acquire::http::proxy “http://ipaddress:port”;

Note that ipaddress and port are your system ip and port

Featured

[Linux]How to compile a new kernel (2.6)


  • Download new kernel source code
  • Go to the directory
  • Do a make menuconfig if you want to build a customized  kernel by enabling/disabling the modules you need
  • make
  • make modules
  • make modules_install (should be root user)
  • make install
After completing this command execution, menu.lst is getting updated . Then reboot the system 
The system will boot with new kernel
Is it worth reading ?  Please like the post if its helpful. Please give your suggestions too

Sweet Risks


Daily writing prompt
Describe a risk you took that you do not regret.

I have taken risks to stick to truth, to help others monetarily when I was also having money issues.

most of the time , the sincerity I had, did not reflected back.

Some of them told they will return money on so and so date. They did not return it back.

Thank to god, I still managed to come out of that situation .

I get advice , even from my mother, for believing the people .

I just say these words to them “I just think of me being in that described state. Imagining in that state makes me panic and I just give the money to help that person.”

And I ask a question in return – Did you see me in any helpless situation?

I keep a pause and give the answer to them -“No, I got help from the unexpected people .

I believe this as my Karma and I am happy about it. I do not care about the return from the people whom I helped, coz karma/god assigned another person to help me in the upcoming tough situation. That hope is enough 🙂 to have a happy present life :).