Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more ➡
Download
Standard view
Full view
of .
Add note
Save to My Library
Sync to mobile
Look up keyword
Like this
33Activity
×
0 of .
Results for:
No results containing your search query
P. 1
Voyage to Kernel - First 24 Parts

Voyage to Kernel - First 24 Parts

Ratings: (0)|Views: 3,689|Likes:
Published by Abinash Bishoyi
Uploaded from Google Docs
Uploaded from Google Docs

More info:

Published by: Abinash Bishoyi on Jul 01, 2010
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See More
See less

01/04/2013

pdf

text

original

 
110
june 2008
|
LInuX For You
|
 www.pITis.cm
T
his voyage, which may seem a bit tedious, isreally ull o un and excitement. In act, theway through the waters in the deep interiorso the Amazon may seem less adventurous. Unlikein those voyages, here the controls are in our hands.We are least concerned about the deeds o nature.We know the directions and targets.Being the guide or this voyage, it is essential orme to give an introduction. As many o the people onboard are frst time travellers to this area, I shall tryto make the initial elucidations more qualitative innature.Beore we start our voyage, we shall look at theprerequisites. I expect the readers to have a copyo the kernel (version 2.2 is enough or our use,but you can have a copy o the latest stable releaseand go or a comparison). You can download thekernel rom the ofcial site
 kernel.org
. I you are anovice, I strongly recommend you don’t downloadthe development release. (O course, I take it orgranted that you’ve set sail on a Linux boat).I have done (tested) the codings in my Intelx86 machine with ‘VINU—THE FREE OS’ as theplatorm [an operating system the author hasdeveloped, based on Gentoo]. You will get thesame results i you try the coding in the terminal o Gentoo or any o its derivatives like Sabayon.By looking at the version number o the kernelitsel, one can tell whether it is a stable release ornot. The idea behind it is very simple! I the seconddigit is even, it is stable. I not, it is a developmentrelease. Quite a simple trick, right? Once you learnthe basics, the shell and kernel coding will be evensimpler or you!!It should be emphasised that most o thesignifcant data structures and many algorithmsremain unaltered in the Linux kernel versions. Thenew versions are released to fx the bugs reportedby the users.You might have read that Linux shares thesimilarities with other ‘UNIX-like operatingsystems’, like System V Release 4 (SVR4)developed by AT&T, the 4.4 BSD releaserom the University o Caliornia at Berkeley(4.4BSD), Digital Unix rom Digital EquipmentCorporation (now Compaq); AIX rom IBM; HP-UX rom Hewlett-Packard; and Solaris rom SunMicrosystems.Linus Torvalds (in 1991) developed Linuxas an operating system or IBM-compatible PCsbased on the Intel 80386 microprocessor. By now,developers have worked to make Linux availableon other architectures, including Alpha, SPARC,PowerPC, Motorola MC680x0 and IBM System/390.You may take a glance at the ollowing list to getan outline o the various architectures:
arm: Acorn personal computers
alpha: Compaq Alpha workstations
i386: IBM-compatible personal computersbased on Intel 80x86 or Intel 80x86-compatiblemicroprocessors
m68k: Personal computers based on MotorolaMC680x0 microprocessors
mips: Workstations based on Silicon GraphicsMIPS microprocessors
ppc: Workstations based on Motorola-IBMPowerPC microprocessors
sparc: Workstations based on SunMicrosystems SPARC microprocessors
sparc64: Workstations based on SunMicrosystems 64-bit Ultra SPARCmicroprocessors
s390: IBM System/390 mainrames A lion’s share o the kernel source code is
KERNEL
AVOYAGE TO THE
 Th Day Bfo th Voyag
 
111
www.pITis.cm
|
LInuX For You
|
june 2008
a voyag to thKernel
processor-independent and is written in C language. Buta small (and critical part!) is coded in assembly language.In the course o our voyage, we will be dealing with theassembly language programming in Linux, as it is requiredto study the kernel thoroughly.The source fle o the kernel, which is used as standardreerence or this voyage, has about 4,500 C and assemblyfles, stored in about 270 sub-directories (roughly 2 millionlines o code!). We are concerned only about the parts thatare essential to comprehend the working o the kernel. Youcan also browse the older
 / 
usr/src/linux
and get yoursel amiliarised with the olders there.Beore we move on to kernel programming and relatedstudies, it is essential to know some tools related to shellprogramming. I don’t expect any prior knowledge inthe feld, but the user should be amiliar with the basiccommands and how to execute them. Also, a basic knowledge o terms like commands,arguments, etc, and the syntax or writing them are expectedto be amiliar. Let’s say that,
who am i
is a special type o 
who
command. It has arguments along with the command,and the general syntax or these types o commands is:
$ command argument1 argument2 argument3 ... argumentN
 Also, to execute the
date
and
who am i
commands ina single line, we use complex commands using commandseparators as ollows:
$ date ; who am i ;
The syntax or a complex command is:
$ command1 ; command2 ; command3 ; ... ; commandN ;
You might have tried simple commands (like
ls
or
cd
)and powerul commands (like
awk
,
sed,
etc.). Suppose wegive the command or the date:
localhost ~ # date
[Yes, here I have logged in as root—note the hash sign!], you will get a response similar to the one shown below:
localhost ~ # dateThu Apr 17 05:15:19 IST 2008
Now the question is how does the computer perorm thetask when you give this command. And here starts the storyo shell...Well, let us ollow a dierent approach. Normally, text-books on shell programming will frst provide a historicaldescription, then they will speak about the types o shells,and fnally move towards the programming part. But I eelit will be better i you start doing small experiments beoremoving to those areas. On your terminal, execute theollowing command:
localhost ~ # !/bin/sh
 And you will get a response like what’s shown below:
localhost ~ # !/bin/sh/bin/shsh-3.2#
You might be a little perplexed now, ater seeing the
#!
’symbol. You may wonder why we require this symbol. Inact, it is telling the platorm that the fle (corresponding to thepath provided) is a script. And it can execute it using the linkdirected towards the interpreter. It is known as shebang (alsotermed as hashbang, hashpling, or pound bang) when they areused as the frst two characters in a script. A shebang line willhave a hash sign ollowed by an exclamation mark, and this willbe ollowed by a complete path to any interpreter program.However, it should be noted that, generally, the contentsin the shebang line are ignored by the interpreter. You willcomprehend this once we start our voyage.Let us continue with our mini-experiments. Using Box 1as a reerence, you can try some interpreters.You might have noticed the reerences to C shell, Kornshell, etc, in Box 1. They are the dierent types o shells. And we will visit these areas frst, in our voyage. Wait till thenext day and we will start!
INTERPRETERS
#!/bin/bash
is used to execute using the Bourne-again shell. If it is not available in the
 /bin/
directory,you may get a response similar to what follows:
localhost ~ # !/bin/bashbash: !/bin/bash: event not found
The following is a list of how to use the shebang:
#!/bin/bash-c‘/bin/bash’
—Using bash in the /bin/directory
#!/bin/csh
—Execute using csh, the C shell
#!/bin/ksh
—Execute using the Korn shell
#!/bin/awk-
—Execute using awk program in the /bin/ directory
#!/bin/sh
—On some systems, such as Solaris, thisis the Bourne shell of Linux
#!/bin/zsh
—Execute using zsh, the Z shell
#!/usr/bin/per
—Execute using Perl
#!/usr/bin/python
—Execute using Python
#!/usr/bin/env 
—Used to invoke any otherprogram using env program in /usr/bin
 Aasis Vinayak PG
 
is a hacker and a free software activist who does programming in the open source domain. He is thedeveloper and CEO of the Mozhi Search engine. His research works/publications are available at www.aasisvinayak.com
 
juLY 2008
|
LINuX For You
|
 www.penITis.cm
80
KERNEL
AVOYAGE TO THE
W
e have seen that the shell acts as an interaceor UNIX (the UNIX system’s commandinterpreter). It is quite essential to make itclear that the shell is not just an interpreter but is a dooropening towards a very powerul programming languagewith conditional statements, loops and unctions.On a large rame we can categorise the types o shells as the Bourne shell (
 sh
,
ksh
and
bash
) and the Cshell (
csh
and
tcsh
). Normally, by looking at the promptsymbol itsel one can guess the type o shell. I you fndthe $ character by deault, then you are in a Bourne-type shell and i you have the
%
character by deaultthen you are using a C-type shell. As indicated earlier, there are dierent kinds o C-type and Bourne shells. The C-types are: C shell (
csh
)and TENEX/TOPS C shell (
tcsh
). And we have shelltypes like the Bourne shell (
 sh
), the Korn shell (
ksh
),the Bourne Again shell (
bash
) and the POSIX shell (
 sh
),belonging to the second group.The pages rom history will tell you that in themid-1970s, the original shell was the Bourne shell andwas written by Stephen R. Bourne (who was workingor the AT&T Bell Labs). I you have been exposedto the ALGOL language, you will fnd that the syntaxused in the Bourne shell is quite similar to that o  ALGOL language.Later in the 1980s, Bill Joy (o the University o Caliornia at Berkeley) came out with the C shell. Theprime motivation to develop the shell was that writingprograms would be easier in this than in a shell whereone needs to ollow the ALGOL style syntax. And,moreover, the C language was quite amiliar to theprogrammers working on UNIX at Berkeley. Featureslike command history (or recalling the scripts thatwere executed already), the fle name completionoption and aliases (mnemonic names or commands)made it quite popular.We have already seen some simple commands. Andtoday we will look at more useul commands. Let usbegin with some interesting stu. I you want to knowwhen you have booted the system, you can use
 who -b
.
vinayak@gnubox:~$ who -bsystem boot 2008-05-14 21:46
 At times, you may need to know your currentdirectory, especially when you want to meddle with your fles. You can get the inormation by using the
pwd
command.
vinayak@gnubox:~$ pwd/home/vinayak
While doing the programming or compiling relatedwork, we may want to look or the virtual memorystatus. For that you can go or
 vmstat
(reer toListing 1)
.
You might have used the line
clrscr()
many timeswhen you have written programs in C. Here you have amore lucid command—just type
clear
; that will do thework. Another useul one is the
 fnger
command thatwill tell you about the logged in users, including theuser's
.plan
and
.project.
Following are some o the basic commands that wecan use:
 pwd
—Print the current directory
cd
—Change directory
 fnd
— Locate fles and directories
ls
—List fles in a directory
 fle
—Print the type
Day O
Listing 1:
vinayak@gnubox:~$ vmstatprocs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----r b swpd free buff cache si so bi bo in cs us sy id wa0 0 0 216460 15148 344048 0 0 388 32 1002 532 12 4 77 7
Pat 2

Activity (33)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads
Lars Wirfelt liked this
cvaniyan added this note|
I'm glad to have this beautiful tutorial on kenrel
ABHISHEK ANAND liked this
ABHISHEK ANAND liked this
Deepro Datta liked this
Gayathri Karthik liked this
manisha2010 liked this

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->