Wednesday, January 31, 2007

Critical Path Calculation for X86

http://zoo.cs.yale.edu/classes/cs490/98-99a/mcclure/sld002.html

Friday, January 19, 2007

The login Process in LINUX

http://www.linuxjournal.com/article/3121

The beginning of it all....
Virtually all Linux sessions begin with the user typing his user name at a prompt that looks like this:

login:


In this article, I will explain a little about what really happens behind the scenes and what contortions the system goes through to get a user going.


A Bit about the Shell
First, a quick look at the shell. The shell, which is just a program like any other, reads the characters you type and looks for a program with the same name. Program names are typed at the prompt and executed by the shell. Ending a command line with the & character causes the command to be run in the background.

The shell runs a program in two steps. First, the shell does an operation called a ``fork''. Forking creates a new process that looks just like the original process, inheriting many attributes of its parent such as any open files and user ID. Although it is an exact copy of the shell program, the ``child'' process does not read user commands. The child shell immediately does an operation called an ``exec'', short for ``execute'', in which it causes the Linux kernel to load the new program over the top of the child shell and run that program in its place.

At this point, the original shell simply waits for the child program to finish. Once done, it gets the next line of input from the user, then the whole procedure is repeated. In an active UNIX system, this sort of thing is happening all the time. Even on fairly inactive systems, processes are still run to do housekeeping chores, while others are simply sleeping and waiting for something to happen.

From the bash shell, you can see how exec works by typing

exec ls -l


The ls command runs as usual, but when it is done, you are no longer logged in. The shell is replaced by ls, and when it finishes, it is as if your shell had finished.

How Does it All Get Started?
When the kernel is first loaded into memory, it initializes itself and any hardware that may be attached to the computer. Once the kernel is established enough to be able to run programs, it does. The first program is called ``init''; its job is to function as the ancestor of all processes.

When init starts, it reads a file called inittab, usually located in /etc. This file tells init which programs should be run under which conditions. Not only does init run the startup scripts that bring the rest of the system up, but init also takes care of shutting the system down.

One of the responsibilities of init is running the programs that let users log into the system. For a terminal (or virtual console), the two programs used are getty and login. getty is short for ``get terminal''. A basic getty program opens the terminal device, initializes it, prints login: and waits for a user name to be entered. Modern getty programs (several are available for Linux) can do other things as well--if the terminal device is a (recent) modem, they can read status codes sent by the modem to tell if the call is voice or fax and handle the call appropriately. Most of the time, though, someone just wants to log in, so getty executes the login program, giving the user name to log in via the command line.

The login program then prompts the user for a password. If the password is wrong, login simply exits. The init process then notices that one of its children has exited and spawns another getty process on the terminal in question. If the password is good, login changes its process user ID to that of the user and executes the user's shell. At this point, the user can type commands. When the user exits by typing the shell's built-in logout command, the shell exits and init notices that its child has exited and spawns another getty on the terminal.

Why are two separate programs used to log in instead of just one? The answer is that doing it this way provides more flexibility. For example, getty doesn't have to execute login--it can execute a program to receive (or send) faxes, a PPP daemon to emulate a network connection over a serial line, or if you have a modem with ``voicemail'', one of those phone tree programs that people hate so much (``press five to hear these options again'').

Similarly, login is sometimes needed without getty; for example, when a user logs in over a network, no terminal device is waiting. Instead, each new connection is handled by a program called telnetd that forks and executes a login process. telnetd remains to pass characters between the network and the new shell.

As a partial example of how the process works, Listing 1 shows an autologin replacement for getty. This replacement is meant for people who are tired of typing their user ID and password for the bazillionth time. You can boot Linux and have it drop straight into a couple of shells--sort of like DOS, but with virtual consoles.

To install autologin, copy it to the /sbin (system binaries) directory and type:

chmod +x /sbin/autologin


as root. Still as root, edit the /etc/inittab file and change the lines that look like this:

c1:12345:respawn:/sbin/getty 38400 tty1


to:
c1:12345:respawn:/sbin/autologin tty1 login -f myid


replacing myid with your own user ID. Red Hat installations typically do not have the letter c at the beginning of the line.


Be sure to leave some of the lines containing getty exactly as they are--if you do something wrong, you are going to need a way to log into your system. On my own system, I change c1 through c3 and run three initial shells. Once the file is edited, reboot the system and all should work.

The first argument to autologin is the name of the terminal. The rest of the command line is used as the login command that does the work.

A Synopsis of autologin
The first line tells the kernel how to run this program, in this case by letting the bash shell interpret it. The first exec line is a Bourne shell trick that lets a shell script change the source/destination of its standard input, standard output and standard error. We want to set file descriptors 0, 1 and 2 to refer to the terminal device as expected by login (and many other programs) when they run. The cat command displays the system's standard logon message. The shift command shifts the positional parameters to the shell script. Argument $1 is deleted, argument $2 becomes $1, argument $3 becomes $2 and so on. The last line executes the rest of the command line as a program. In this case, the login -f option performs the normal login procedure, with the -f option telling login not to bother with passwords

Thursday, January 18, 2007

Types of Shells IN LINUX

http://rpmfind.net/linux/RPM/System_Shells.html


ash-1.6.1-2 The Ash shell linux/i586
ash-1.6.1-1.1 The Ash shell linux/i586
bash-completion-20050121-3 Programmable completion for bash linux/noarch
bash-completion-20040711-2 Programmable completion for bash linux/noarch
bash-3.0-15.2 The GNU Bourne-Again Shell
bash-3.0-15 The GNU Bourne-Again Shell linux/i586
bash-3.0-8.2 The GNU Bourne-Again Shell linux/x86_64 linux/i586 linux/i586 linux/i586 linux/i586
bashish-2.0.7-1.bashish.generic Theme Environment For Text Terminals linux/noarch
bashish-2.0.6-1.bashish.generic Theme Environment For Text Terminals linux/noarch
bashish-2.0.5.1-1.bashish.generic Theme Environment For Text Terminals linux/noarch
bashish-2.0.5-1.bashish.generic Theme Environment For Text Terminals linux/noarch
bashish-2.0.4-1.bashish.generic Theme Environment For Text Terminals linux/noarch
pdksh-5.2.14-786 Public Domain Korn Shell linux/i586
pdksh-5.2.14-783 Public Domain Korn Shell linux/i586
sash-3.7-32 A stand-alone shell with built-in commands linux/i586
sash-3.7-31 A stand-alone shell with built-in commands linux/i586
tcsh-6.14.00-2.2 The C SHell linux/i586 linux/i586 linux/i586 linux/x86_64 linux/ppc linux/i586 linux/i586 linux/i586 linux/x86_64 linux/ppc
tcsh-6.12.00-455 The C SHell linux/i586
tcsh-6.12.00-453.3 The C SHell linux/i586
tcsh-6.12.00-453.2 The C SHell linux/x86_64 linux/i586 linux/i586 linux/i586
zsh-4.2.4-5 Shell with comprehensive completion

Linux Shell Scripting Tutorial (LSST) v1.05r3

http://www.freeos.com/guides/lsst/index.html

if condition

if condition which is used for decision making in shell script, If given condition is true then command1 is executed.
Syntax:

if condition
then
command1 if condition is true or if exit status
of condition is 0 (zero)
...
...
fi

Condition is defined as:
"Condition is nothing but comparison between two values."

For compression you can use test or [ expr ] statements or even exist status can be also used.

Expreession is defined as:
"An expression is nothing but combination of values, relational operator (such as >,<, <> etc) and mathematical operators (such as +, -, / etc )."

Following are all examples of expression:
5 > 2
3 + 6
3 * 65
a < b
c > 5
c > 5 + 30 -1

Type following commands (assumes you have file called foo)
$ cat foo
$ echo $?
The cat command return zero(0) i.e. exit status, on successful, this can be used, in if condition as follows, Write shell script as
$ cat > showfile
#!/bin/sh
#
#Script to print file
#
if cat $1
then
echo -e "\n\nFile $1, found and successfully echoed"
fi

Run above script as:
$ chmod 755 showfile
$./showfile foo
Shell script name is showfile ($0) and foo is argument (which is $1).Then shell compare it as follows:
if cat $1 which is expanded to if cat foo.

Detailed explanation
if cat command finds foo file and if its successfully shown on screen, it means our cat command is successful and its exist status is 0 (indicates success), So our if condition is also true and hence statement echo -e "\n\nFile $1, found and successfully echoed" is proceed by shell. Now if cat command is not successful then it returns non-zero value (indicates some sort of failure) and this statement echo -e "\n\nFile $1, found and successfully echoed" is skipped by our shell.

Exercise
Write shell script as follows:
cat > trmif
#
# Script to test rm command and exist status
#
if rm $1
then
echo "$1 file deleted"
fi

Press Ctrl + d to save
$ chmod 755 trmif

Answer the following question in referance to above script:
(A) foo file exists on your disk and you give command, $ ./trmfi foo what will be output?
(B) If bar file not present on your disk and you give command, $ ./trmfi bar what will be output?
(C) And if you type $ ./trmfi What will be output?