Sunday, October 6, 2013

List of my computer tutorials

List of my computer tutorials.


These tutorials are mainly aimed to help the young developers. Most of the tutorials are based on linux and C/C++.

  1. Changing cpu frequency
  2. Password less ssh login
  3. Introduction to make file
  4. Advanced make file

Sunday, August 25, 2013

Changing CPU frequency

Changing CPU frequency in Linux


Note: To see my entire list of tutorials click here.

There are many scenarios where you would like to change the CPU frequency. It may be for reducing the CPU power consumption, or to get the best performance. Its recommended to choose a particular frequency for testing your tools and benchmarks. You may set the frequency minimum to reduce the heat. The default policy is called "OnDemand" which changes CPU frequency according to the need. You can set the frequency or mode for each CPU core.

Step 1: Install the required packages


The below command is for debian based system. For RPM based system use yum
sudo apt-get install cpufrequtils


Step 2: The current CPU frequency and mode


You can view the current cpu frequency and mode for each core by using the following command

cpufreq-info

Just to see the frequency you can use

more /proc/cpuinfo | grep MHz 

Step 3: Supported modes and frequencies


We can either set a particular frequency or a particular frequency mode.

Lets first see the list of supported CPU frequency.
cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies

#sample output: 2401000 2400000 1600000 800000 

To see the available modes use
cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors

#sample output: conservative ondemand userspace powersave performance

Available governors/modes

  • Performance: Use the highest possible CPU frequency
  • Powersave: Use the lowest possible CPU frequency
  • Userspace: exports the available frequency information to the user level (through the /sys file system) and permits user-space control of the CPU frequency
  • Ondemand: Set frequency based on the user demand
  • Conservative: Like the ondemand but increases frequency step by step

Step 4: Loading the CPU driver


The driver for CPU depends on your CPU, here is a short list

  • acpi-cpufreq: CPUFreq driver which utilizes the ACPI Processor Performance States. This driver also supports Intel Enhanced SpeedStep.
  • speedstep-lib: CPUFreq drive for Intel speedstep enabled processors (mostly atoms and older pentiums (< 3))


  • powernow-k8: CPUFreq driver for K8/K10 Athlon64/Opteron/Phenom processors. Deprecated since linux 3.7 - Use acpi_cpufreq.


  • pcc-cpufreq : This driver supports Processor Clocking Control interface by Hewlett-Packard and Microsoft Corporation which is useful on some Proliant servers.


  • p4-clockmod: CPUFreq driver for Intel Pentium 4 / Xeon / Celeron processors


you can use the following command to find the driver
ls /lib/modules/$(uname -r)/kernel/drivers/cpufreq/
Now load the module using the following in my case (acpi-cpufreq)
sudo modprobe acpi-cpufreq

Step 5: Setting minimum and maximum cpu frequency

If you want to set a frequency mode rather than a particular frequency skip this step To set maximum cpu frequency for core 0
echo 2401000 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
To set minimum cpu frequency for core 0
echo 800000 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq
Just repeat this step for cpu1 etc to set the same on all cores. Verify the setting using step 2

Step 6: Setting cpu frequency modes

To set the cpu frequency use the following
sudo sh -c 'echo "performance" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor' 
Verify the setting using step 2 Just repeat this step for cpu1 etc to set the same on all cores.

Step 6: Result

You have changed the cpu frequency

Saturday, February 11, 2012

Password-less logins with OpenSSH

Password-less logins with OpenSSH


Note: To see my entire list of tutorials click here.

SSH is often used to login from one system to another without requiring passwords. One thing that you probably won't want is to do though is store the remote system's password in the script. Instead you'll want to setup SSH so that you can login securely without having to give a password. The first step is to create a key-pair. I will explain this... but before that....

The ssh key-pair consists of a public key and private key. They are stored in /home/yourusername/.ssh/ folder.

Step 1: Creation of key pair


If you already have a key pair goto step 3. To check whether you already have a key pair check whether '/home/yourusername/.ssh/id_rsa.pub' file exists or not.

To create a new pair
ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/yourusername/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/skx/.ssh/id_rsa.
Your public key has been saved in /home/skx/.ssh/id_rsa.pub.
After entering 'ssh-keygen -t rsa' just keep on pressing enter.

Step 2: Adding identity to authentication agent


To add the generated key to the authentication agent either you should logout and then login or, run the following
ssh-add

Step 3: Copying the authentication to target


Step 1 and Step 2 should only be executed once. If you have more that one target, just executed step 3 multiple times (changing the target ip)

Lets assume that 10.192.35.12 is the ip address of the machine in which we want to login to. Run the following code
ssh-copy-id -i ~/.ssh/id_rsa.pub username@10.192.35.12
This will prompt you for the login password for the host, then automatically copy the key file for you, creating the correct directory and fixing the permissions as necessary.The contents of the key file will be appended to the file ~/.ssh/authorized_keys2 for RSA keys.

Once this has been done you should be able to login remotely, and run commands, without being prompted for a password.

To login use
ssh username@10.192.35.12

To execute a command say uptime use,
ssh username@10.192.35.12 uptime


Friday, February 10, 2012

Advanced Make

Advanced Make


Note: To see my entire list of tutorials click here.

Note: This post is the continuation of Make for beginners. Please refer it if you dont know the basics.

Make Shortcuts


Macros

To make life easier, we can use the macros provided by make. These are rules inbuilt in make.
data.o: data.c data.h
        gcc -c data.c
How can we reduce the above code???

Make knows that when the target file is a ".o" file, then it must use gcc -c on the corresponding ".c" file. Applying this, we can eliminate the action (gcc -c ...)
data.o: data.c data.h
Make knows that when the target file is a ".h" file, then the target should also contain the corresponding ".c" file. Applying this we can eliminate data.c (since data.h is included)
data.o: data.h

Variables

You can also use variables when writing Makefiles. It comes in handy in situations where you want to change the compiler, or the compiler options.

# The variable CC will denote which compiler to use (for ex. gcc or cc)
CC=gcc

# CFLAGS will be the options passed to the compiler.
CFLAGS=-c -Wall

project1: data.o main.o io.o
        $(CC) data.o main.o io.o -o project1
data.o: data.c data.h
        $(CC) $(CFLAGS) data.c
main.o: data.h io.h main.c
        $(CC) $(CFLAGS) main.c
io.o: io.h io.c
        $(CC) $(CFLAGS) io.c

Note that you can use the Macros provided in the previous section to reduce the code size considerably.

Multiple Targets


We can put more than one file in the target section of the dependency rules. If a file appears as a target more than once in a dependency, all of its source files are included as sources for that target.

Here is our sample Makefile again:
project1: data.o main.o io.o
 gcc data.o main.o io.o -o project1
data.o main.o: data.h
io.o main.o: io.h

This Makefile shows main.o appearing in two places. Make knows by looking at all the dependencies that main.o depends on both data.h and io.h.

Multiple targets

Till now we executed Makefile by typing the command "make". As explained earlier this will execute the default target (the first target in Makefile), in our case project1.
If you just wanted to compile the data.o file (for some reason) you can execute the "make data.o", and only that action will be executed.

One target which is usually give in make is clean (executed by "make clean"), which cleans the installation. Just create a new target called clean and in the action part give the corresponding action. For ours it will be
project1: data.o main.o io.o
 gcc data.o main.o io.o -o project1
data.o main.o: data.h
io.o main.o: io.h

clean:
 rm *.o

Our clean target will just remove all the ".o" files. you can also do other things. For example to display a message
clean:
 rm *.o
 echo "Uninstall complete. Thanks for reading the blog."

Sunday, February 5, 2012

About Me

Hi,


I am Aravind S R. I am doing my M.tech in Computer Science Engg.

Please visit the following site for more details

Note: To see my entire list of tutorials click here.

How to create a Make file

A simple introduction to make file


Note: To see my entire list of tutorials click here.


Before we begin to learn how to make a "make file", we should under why it is needed and the background

Background

Skip background

compile

When you type
gcc file.c 
or
cc file.c 
the following happens

1. Compiler stage: All C language code in the .c file is converted into a lower-level language called Assembly language; making .s files.
2. Assembler stage: The assembly language code made by the previous stage is then converted into object code which are fragments of code which the computer understands directly. An object code file ends with .o.
3. Linker stage: The final stage in compiling a program involves linking the object code to code libraries which contain certain "built-in" functions, such as printf. This stage produces an executable program, which is named a.out by default.

Now lets consider compiling multiple files

make file

When your program becomes very large, it makes sense to divide your source code into separate easily-manageable .c files. The figure above demonstrates the compiling of a program made up of two .c files and a single common.h file. The command is as follows:
gcc green.c blue.c
Now you can see that the linker needs to combine multiple .o objects. The 'o' files are first generated and then they are combined in the next step. the 'o' file green.c can be produced by specifying the -c option as follows
gcc -c green.c
Do the same for blue.c. Now you can produce the executable by
gcc green.o blue.o
Enough of the introduction... Lets look at a real makefile

Sample makefile


make dependency graph

Consider the above dependency graph.
you would normally compile this as
gcc -c data.c
gcc -c io.c
gcc -c main.c
gcc data.o io.o main.o
which will produce a.out file

Now you have to repeat the above steps when each of the .c or .h files are changed.

Assume that data.c contains the function which you want to make as a library
sample data.c file
include "data.h" //You should include data.h

//welcome functions definition
void welcome()
{
    printf("Welcome to my blog http://aravind-sr.blogspot.in \n");
}

//other functions

Note that the data.h file must be included

sample data.h file
extern void welcome(); //the keyword extern is must
//other declarations

Note that all function declaration should contain the keyword extern

we will create a make file as follows. Create a file named Makefile in the same directory. The contents of the file is as follows

#introduction to make file

project1: data.o main.o io.o
        gcc data.o main.o io.o -o project1
data.o: data.c data.h
        gcc -c data.c
main.o: data.h io.h main.c
        gcc -c main.c
io.o: io.h io.c
        gcc -c io.c
'#' represent the comment.
Every line is of the from

target : source file(s)
command (must be preceded by a tab)

meaning, which all files (source) are needed for the target (this is the dependency). Followed by the action to be done. If you are confused read the below.

Lets assume that we modified data.c or data.h. data.o is dependent on these files. This is exactly represented by third line of the program (including comment). Now what action should we do? We should re-compile the data.c to produce new data.o. This is represented by the fourth line.

To run the make file you should type "make". When you type make the following happens

1. It checks whether the main.o, or io.o, or data.o needs to be updated.
2. If any of the three needs updated it will run that target.
3. Finally it will make the whole project.

Assume that we modified data.c. Now when we type make, the control to project1. From there it goes to main.o, io.o, and data.o. From data.o it knows that it is dependent on data.c and since its modified that command will be executed. Now it will re-compile the project1. So the output will be
gcc -c data.c
gcc data.o main.o io.o -o project1
This is just an introduction to make file. Please click here to go to the next level, where compilation of specific targets, variables, shortcuts,multiple targets etc will be explained

For advance tutorial click here.