Search This Blog

Tuesday, June 13, 2017

Freeup RAM in Linux System - Linux Basics


            Some times linux Servers/Low memory linux boards tends to low memory conditions. Even though linux has excellent memory management system Some un-closed files, network memeroies and caches  causes out of memory or low memory conditions. By deleting and cleaning pagecache, dentries and inodes we can get the unused memory.

we can check ram usage with free command

root@arm:~# free
                   total         used         free      shared    buffers     cached
Mem:         50204      47352       2852          0       6188      20768
-/+ buffers/cache:      20396      29808
Swap:            0              0              0

here i am having only 2852KB of memory free  out of 64MB RAM.

Option 1: Free pagecache

This is suitable for webserver as they tend to store lots of pagecaches.

To free pagecache:

sync; echo 1 > /proc/sys/vm/drop_caches


(
To free pagecache, dentries and inodes(not recommended)
sync; echo 3 > /proc/sys/vm/drop_caches 
)

after this 

root@arm:~# echo 1 > /proc/sys/vm/drop_caches
root@arm:~# free
                      total       used       free     shared    buffers     cached
Mem:         50204      26412      23792          0         80       6720
-/+ buffers/cache:      19612      30592
Swap:            0                0          0

the free memory is 23MB.

Check Memory Usage in Linux - Linux Basics

Commands to Check RAM/Memory usage in Linux

free               shows usage in KiloBytes

root@arm:~# free
                   total        used           free     shared    buffers     cached
Mem:         50204      45392       4812          0       5740        20464
-/+ buffers/cache:      19188      31016
Swap:            0          0          0

free -b               shows usage in Bytes
free -k               shows usage in KiloBytes
free -m              shows usage in MegaBytes
free -g               shows usage in GigaBytes

RAM Problem in Linux Board - Linux Basics

Some small/low cost linux boards having small RAM size. For example olinux single chip computer boards having 64MB ram only.
for some instalations it will show the error  like " Cannot allocate memory" so we need to increase the RAM size virtually.

GNU/Linux supports swapping to a file, with performance depends on your SD card writing and reading speed. This might be useful whenever needing a temporary increase in available memory,

Create a file (following example would create a 1024x1M=1G swap size) will zero fill

dd if=/dev/zero of=/.swapfile bs=1024 count=1M

it will take minutes depends on flsah/memory card speed

command to Setup a swap area in it:

mkswap /.swapfile

To activate swap to this file (would not survive a reboot):

swapon /.swapfile

To check it did it:

swapon -s

Disable swap to this file:

swapoff /.swapfile


Saturday, July 23, 2016

Starting with RPi

RaspberryPi:


Raspberry Pi is a amazing little computer that fits in the palm of your child's hand also, Also Packed with powerful enough to run your home media center, a VPN, mini server, and a lot more. Before you can do anything awesome, first you need to install an operating system and configure it.Here's how.




What You'll need to Launch Your Raspberry Pi:


  • Raspberry Pi
  • HDMI Cable : To Connect to HDMI compatible monitor or TV
  • USB Keyboard and USB Mouse : Any Standered Usb Keyboard and or Mouse will do the job.
  • 3.5 mm audio cable : if you need audio output through your home theare
  • Download the latest Raspbian OS (Here
  • A good Quality Power supply 5Volts 2A is recommended or Any Good Power Bank: Most modren smart phone  charger is suffecient if you doing light wieght projects.
  • 8 GB micro SDcard: A class 4 micro sd card is enough 
  • SD CardReader
  • 2 Softwares to write the Raspibian OS on your memory Card
  • SDFormater
  • win32DiskImager



Setting Up Your RPi with Basic Operating System (Rasppibian):

You can use the Raspberry Pi for all sorts of different things—some of which may require their own special operating systems—but to start out, it’s a good idea to get acquainted with the Pi by installing Raspbian, a Raspberry Pi-focused version of Linux. Here’s what you need to do.

Step One: Prepare Your SD Card

Before powering the Raspberry You need the Install OS on to your SD card . To do so You must put your SD card into SDcard reader and connecto to  you PC/Laptop and then format SD card using SDFormater Software (Becarefull You might Select the Wrong Drive , if you do so all the data in that drive is wiped out).
Once the formating is done then open Win32DiskImager software and Browse the Rasbian Image file which you downloaded earlier. and select Drive(which is pointer to SD Card). There is 2 otions in the drive one is read and the other is write. "read" is to read the sd card and write on the file and "write" is to write the image to the sd card.
now click on "write"(if you select "read" your Image file will be over write by the data in SD car, here sd card having no data so --> image file will be 0 Bytes) so don't do that). now the OS will starts writing on to SD card.
Wait for some time(15 min for mine)
When it finishes, safely eject your SD card and insert it into your Raspberry pi
Step Two:
In step one you successfully write the OS onto your SD card now its time to Power Up your RPi.
Connect usb mouse and keyboard to your RPi and HDMI cable to monitor and your RPi.
Check once all are connected well, then connect the power to RPi.
Now your mini computer start booting up and you can see that in the monitor.

is it asks for user name and password then type the default user name and default password as bellow
user name:pi
password: raspberry

Now you can play with your PRi it having in bult browser,image viewer,document write,...etc .

by default the file system shows only less tha 4 gb even though you use 8GB sd card. there you will face some problem with lowe memory so, first you need to expand the file system so that OS can access all the physical memory on the sd card to do that you need to configure your raspberry.....

to be continued...............

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>




Friday, August 30, 2013

Creating header file in keil

Creating header file in keil :

        To develop a complex or a big project in keil we need to write more code so , as the number of lines increasing the complexity of editing the code is also increases so we need to optimize the code structure to access it easily. we can do so by creating header files. Header files mainly contains some functions which are called by main function and these header files may stored in the same folder or in another folder. if the header file and the project is in the same folder then  "#include<header_file_name>" is used to call a header file. if the header files stored in other location then "#include"path../header_file_name"" is used.

        Here is the simple procedure is shown about creating header file in keil is shown. you can find the clear view in video on creating header file in keil.

there are some steps for this, those are

step1:first create 3 folders with in out project folder and name them as 
      'APP','DRV','OUT'
     APP-is for our application file i.e., main files
     DRV-is for driver functions 
     OUT-is for output files

step2: create a project in keil and in the target option add 2 source groups and name them as 'APP','DRV'

step3:make delay.c and delay.h files as shown in the video 
      delay.c file having the function body and delay.h file having the function declaration
      save the both files in DRV folder

step4: now add delay.c file to DRV source group so that it will get compile along with main.c file

step5:go to 'Options for target'  the go to 'output' tab then click on 'folder for objects' and select OUT       
     folder

step6:then go to 'C51' tab and in 'include paths' add 'APP' and 'DRV' folders so that the compiler 
     searches the header file in that included paths

step7:add #include "delay.h" line in main file  and compile the code

follow the video for better understanding of creating header file in keil




Full