Saturday, September 18, 2010

What is the difference between a thread and a process?

What is the difference between physical memory addresses and virtual memory addresses? What is a page table / page table entry?

What is the difference between kernel mode and user mode? What is a system call?

What is a spinlock? Why would you use one? When and when not?

What is a context switch? What happens when a thread is "blocked"?

What does the scheduler do?

What happens (to the state of a running thread) when an interrupt occurs?

What sorts of things must an interrupt handler do?

Why you want to write a device driver?

How will you register it?

How will you keep track of its usage?

Under what license will you write your driver?

How will you allocate memory in your driver?

Why not vmalloc?

What if you got to know that your memory is leaking?

What is IRQ? how will you register your IRQ no? Are you sure you will get the IRQ no you have requested? What if you don't get?

Where will you place filesystem? above device driver or below it?

Thursday, September 9, 2010

Setting up Version Control systems in Linux

Version Control System fits the definition of a friend in the common adage "A friend in need is a friend indeed " , for a software coder , who writes millions of lines of code over his lifetime , most of the times updating / improving his code .

An orderly life certainly warrants the need of keep a check of all the "Kodak Moments of life" , when a certain combination of the code worked brilliantly , bringing a smile on the developers face.

Over the course of time , the code is updated for getting a bigger smile .. but yeah sometimes one is not able to remember or browse through the whole source code to find out as to where the error has crept in from . And any frantic search of the Kodak Moment in the memory just does not seem to work out .

This is where the CVS comes to the rescue , as it acts as the database , which has stored the snapshots of the code , perhaps with a comment about those glorious days of the working code .

Just download the base-lined version and voila , it works . Now its just needed to compare what the updated code has more in it , that caused the error.

Subversion is an alternative to CVS that is growing in popularity.

apt-get install subversion subversion-tools

Creating a Repository :

Create a directory by the name repo/ under your home folder say /home/rajesh/

This is the place where we would be placing the repositories of all the projects that I am working on . This is different from my workspace directory.

timesheetapp is the application that I am working on and I want to keep a repository of that .

svnadmin create ~/repo/timesheetapp

The contents of the folder would look like this :
rajesh@rajesh-desktop:~$ ls -lrt /home/rajesh/repo/timesheetapp/
total 24
-rw-r--r-- 1 rajesh rajesh 229 2010-09-09 12:29 README.txt
drwxr-xr-x 2 rajesh rajesh 4096 2010-09-09 12:29 locks
drwxr-xr-x 2 rajesh rajesh 4096 2010-09-09 12:29 hooks
drwxr-xr-x 2 rajesh rajesh 4096 2010-09-09 12:29 conf
drwxr-sr-x 6 rajesh rajesh 4096 2010-09-09 12:29 db
-r--r--r-- 1 rajesh rajesh 2 2010-09-09 12:29 format

For optimum use of the subversion its advisable to use create a directory structure
mkdir timesheetapp
mkdir timesheetapp/trunk
mkdir timesheetapp/branches
mkdir timesheetapp/tags
svn import . file:///home/rajesh/repo/timesheetapp --message 'Creating repository for timeSheetApp

Steps for Creating a debian package installer for an application in Linux

Debian Package

Debian packages are standard Unix archives that include two gzipped, bzipped or lzmaed tar archives: one that holds the control information and another that contains the data.

The canonical program for handling these packages is dpkg, most commonly via apt/aptitude. [apt/aptitude are programs for application installers that come by default with Ubuntu]

A deb file contains two main files :

  • control.tar.gz
  • data.tar.gz
Certain things to be kept in mind for Linux

1) All executable binaries in Linux are usually to be installed under /usr/bin/ ; /usr/sbin/ ; /bin/

Why ? Coz these directories are added to the global PATH variable and all files are accessible from any folder.
To try this out : Open a terminal , type echo $PATH
You will see a number of directories , these are all having binaries / executables which can be accessed from any location . E.g the command 'cat' or 'ls' are all built-in executables that are present under /bin/ which is available in the PATH variable.

/home/rajesh/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

2) To find out the dependent shared libraries for an executable , try the ldd command

Why ? Coz this would give the libraries required by this executable , and can thus be listed in the control file of the .deb
Now we can see a step by step approach to creating a debian package with an example :

control file : This is the main “brain” of a debian file: it contains all of the informations reguarding this package, without this file the entire creation of a deb package has no meaning at all. A control file looks like this
__________________________________________________________________________________________________________
Package: ModBusTCPSim
Version: 1.01
Architecture: i386
Maintainer:
Installed-Size:
Depends:
Section: apps
Priority: optional
Description: ModBus TCP/IP Slave Simulator

ModBus TCP / IP Slave Simulator is a GUI implementation of the Slave Code of Modbus , built on the Sunlux Slave Stack Library.
This finds use as an slave simulator running on Linux / Ubuntu .
Original-Maintainer: Rajesh CB cb.rajesh.in@gmail.com>
________________________________________________________________________________________________________
  • Package: you have to put here the name of the package, never use spaces, never use a version number. It is case sensitive, be warned!
  • Version: of course, the version number / code. It can also be, for example, opengeu-3.0.2 or any other weird code, as long as the last part is a number. Always remember that chaning the version to a higher number triggers the system to consider that new file as an update of course!
  • Priority: optional, high, low, only system or security updated should have something like high in the priority.
  • Architecture: i386, amd64, all, you should specify here what platform this package is compiled for. If it is only an artowork or a metapackage, you can put all in the Architecture field.
  • Depends: a list of all the packages this package depends on. A metapackage, for example, can include almost nothing but depend on a lot of other packages so that installing this metapackage automatically installs a lot of other packages! That’s what happens with opengeu-desktop for example!
  • Conflicts: if you know that your package can’t work if another package is installed, well, you should then list that package here.
  • Description: put only a very short description in the first line, then enter a longer description on the second line, like it is shown in the example above.
Data
Every file that has to be installed into the system goes into the debian folder. Let’s say that this is like the heart of our deb package. Consider this folder as the “/” mount point of your system, the main system folder or whatever you call it. Basically, you have to recreate into the debian folder the structure of the folders and files you wish to be installed into the system. So, let’s say that our package has to install the ModBusTCPSim into the /usr/bin/ModBusTCPSim

then, you’ll have to create or edit the files and folders status to look like this:

Rajesh/debian/usr/bin/ModBusTCPSim

The final debian package creation :

Thus the final directory structure would look like this :

Rajesh/
`-- debian
`-- DEBIAN
|-- control
`-- usr
`-- bin
`-- ModBusTCPSim

Now cd to the folder Rajesh/ through a terminal and execute the command dpkg-deb –build debian
and you will have the debian.deb package created under the folder Rajesh/

We can rename or move the folder to a better name by :


mv debian.deb ModBusTCPSim.deb

'mv' - move command in Linux


Now through the GUI double click on
ModBusTCPSim.deb and the package gets installed.

Grey areas :

1) control can either be a file or a
control.tar.gz zipped archive of some more packages fro data integrity check.
2) This is a very basic set of files required to make a debian package , and does not contain all checks for data corruption et all , good debian packages contain manuals for the application too.

Wednesday, June 11, 2008

Programming with Bytes :

Programming with Bytes :

The world - 21st Century

There was a huge empire … an empire of cranky devices christened “Computers”. These later on went to be called “Supercomputers” , machines which could do anything and after a few decades probably everything .

Such was the extent to which they had spread themselves over our lives and turning back was unimaginable. But all said and done , everything surviving on earth needs to follow the Rules of the nature i.e even the largest of Dinosaurs were to be created from the tiniest of cells.

Thus the Supercomputers that we see today work on nothing more than a bit a.k.a. cells of the computer . A ‘bit’ symbolizes the smallest unit of information that can be stored in a memory (Artificial).So what does it store afterall . Any resemblence or attempt to relate this to the human brain could prove fatal as the complexity of the human is really complex. Hats off to the Human brain anyways especially the one that makes me write this.

Well back to what we were discussing , a bit . A bit stores either a ‘ON’ or a ‘OFF’ , this can be very easily an safely be related to a switch , a very normal Electric switch , the one one at home used to turn ON or OFF a bulb.

So how do the 108 keys on your keyboard become recognisable data to the computer.

Lets take an example , say A , * , 1 need to be represented in an understandable format to the processor.

Now for the computer these 3 are no different from each other , neither does it understand that these are English Alphabets , a special symbol , a Maths number respectively.For the processor these are mere unique values which need to be always stored in a particular form onto the memory such that on reading from the processor , for those to whom it really matters (i.e you and me) do get to understand as them as English Alphabets , a special symbol , a Maths number respectively.

Here is where we come an imporatant industry standard , ASCII - Short for American Standard Code for Information Interexchange . which assigns letters, numbers and other characters within the 256 slots available in the 8-bit code.

So ASCII values come as great help over here. ASCII values are standard values that are uniform all around the world . So A is said to have an ASCII value ( let me tell you its 65 ) We have a Appendix ‘A’ section later on in this blog for a list of ASCII values.

At this stage let me introduce you to Procy , Procy is our friendly processor who would amuse and awe you with his antics and in the process help us understand the working of a processor.

Over to Procy …

Hello Everyone ! A very good day to you !


Well to start with let me be courteous enough to introduce myself. I am Procy , I am a processor. The Adam of my family came to earth sometime around in the early 1970. The first of my kind as far as I have heard was a quite fat and bulky , almost the size of a cricket bat . But since then , with all these slimming packages available , here I am the slim beauty , a mirror cracking material ! Oops am boasting too much , well I am just telling the truth.

Now , let me take 2 cases for our study !

1) A simple file (to keep the complexity to the bare minimum let us take a “text” file written and saved in a notepad) . We will migrate to MS Word documents later on this section.

If we type the letter ‘A’ in a notepad and save it onto the harddisk with under a text file format , we notice that it takes 1 byte of space in the memory.You may also take a notepad on your system and type the letter ‘A’ in it (without quotes) .

Now how does this ‘A’ get stored in the memory finally . I mean if you just pluck out the memory and are somehow able to view or read the contents marked at the location where the file is saved.

Well as per my understanding the ‘A’ would first get converted into its ASCII value ( Just to remind – its 65 )and then this again gets converted into an 8 bit digit in its binary form . So this is how it would be stored in the bare memory , and the Notepad program that was developed would have in its list of important values – the address where this particular 8 bit value resides.

So from an OS perspective when we again try to open the file using NotePad :

First the program files are loaded into memory , ( now the RAM ) , then the data whose address in the harddisk would be stored along with the tasks of the NotePad application will be copied on to the RAM.

Now these ASCII values are converted to human readable characters by the NotePad application (well that’s what it does and sells for).

If we are clear with the saving and retrival of data for a NotePad application , we can move on to an MS Word document application which also stores along with the basic structure of the alphabet , the extra formatting parmeters like the font , size , indentation etc.