Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Friday, May 25, 2012

What is an Operating System? (Extended Machine and Resource Manager)

WHAT IS AN OPERATING SYSTEM?
I think its hard to pin down what an operating system is other than saying it is the software that runs in kernel mode and even that is not always true. Basically operating systems perform two unrelated functions: providing application programmers a clean abstract set of resources instead of the messy hardware ones and managing these hardware resources.

Therefore, OPERATING SYSTEM = EXTENDED MACHINE + RESOURCE MANAGER

Depending on who is doing the talking, you might hear mostly about one function or the other.

But here we will now look at both.

First case: Extended Machine
The architecture (instruction set, memory organization, I/O, and bus structure) of most computers at the machine language level is primitive and awkward to program, especially for input/output. To make this point more concrete, consider how floppy disk I/O is done using the NEC PD765 compatible controller chips used on most Intel-based personal computers. We use the floppy disk as an example, because, although it is obsolete, it is much simpler than a modem hard disk.

[ The PD765 has 16 commands, each specified by loading between 1 and 9 bytes into a device register. These commands are for reading and writing data, moving the disk arm, and formatting tracks, as well as initializing, sensing, resetting, and recalibrating the controller and the drives. The most basic commands are read and write, each of which requires 13 parameters, packed into 9 bytes. These parameters specify such items as the address of the disk block to be read, the number of sectors per track, the recording mode used on the physical medium, the intersector gap spacing, and what to do with a deleted-data-address-mark. When the operation is completed, the controller chip returns 23 status and error fields packed into 7 bytes. As if this were not enough, the floppy disk programmer must also be constantly aware of whether the motor is on or off. If the motor is off, it must be turned on (with a long startup delay) before data can be read or written. The motor cannot be left on too long, however, or the floppy disk will wear out.]
If you do not understand this mumbo jumbo, do not worry; that is precisely the point-it is rather esoteric. The programmer is thus forced to deal with the trade-off between long startup delays versus wearing out floppy disks (and losing the data on them). Without going into the real details, it should be clear that the average programmer probably does not want to get too intimately involved with the programming of floppy disks (or hard disks, which are worse). Instead, what the programmer wants is a simple, high-level abstraction to deal with.
In the case of disks, a typical abstraction would be that the disk contains a collection of named files. Each file can be opened for reading or writing, then read or written, and finally closed. Details such as whether or not recording should use modified frequency modulation and what the current state of the motor is should not appear in the abstraction presented to the application programmer.

Abstraction is the key to managing complexity. Good abstractions turn a nearly impossible task into two manageable ones. The first one of these is defining and implementing the abstractions. The second one is using these abstractions to solve the problem at hand.
One abstraction that almost every computer user understands is the file. It is a useful piece of information, such as a digital photo, saved e-mail message, or Web page. Dealing with photos, e-mails, and Web pages is easier than the details of disks, such as the floppy disk described above. The job of the operating system is to create good abstractions and then implement and manage the abstract objects thus created. Abstractions are one of the keys to understanding operating systems. This point is so important that it is worth repeating in different words. Real processors, memories, disks, and other devices are very complicated and present difficult, awkward, idiosyncratic, and inconsistent interfaces to the people who have to write software to use them. Sometimes this is due to the need for backward compatibility with older hardware, sometimes due to a desire to save money, but sometimes the hardware designers do not realize (or care) how much trouble they are causing for the software. One of the major tasks of the operating system is to hide the hardware and present programs with nice, clean, elegant, consistent, abstractions to work with instead.

Operating systems turn the ugly into the beautiful using abstraction concept.

It should be noted that the operating system's real customers are the application programs (via the application programmers, of course). They are the ones who deal directly with the operating system and its abstractions. In contrast, end users deal with the abstractions provided by the user interface, either a commandline shell or a graphical interface. While the abstractions at the user interface may be similar to the ones provided by the operating system, this is not always the case. To make this point clearer, consider the normal Windows desktop and the line-oriented command prompt. Both are programs running on the Windows operating system and use the abstractions Windows provides, but they offer very different user interfaces. Similarly, a Linux user running Gnome or KDE sees a very different interface than a Linux user working directly on top of the underlying (text-oriented) X Window System, but the underlying operating system abstractions are the same in both cases.That is a large and important subject, but one only peripherally related to operating systems.

Second case: Resource Manager
Modern computers consist of processors, memories, timers, disks, mice, network interfaces, printers, and a wide variety of other devices. The concept of an operating system as primarily providing abstractions to application programs is a top-down view. An alternative, bottom-up, view holds that the operating system is there to manage all the pieces of a complex system. In the alternative view, the job of the operating system is to provide for an orderly and controlled allocation of the processors, memories, and I/0 devices among the various programs competing for them.

Modem operating systems allow multiple programs to run at the same time. Imagine what would happen if three programs running on some computer all tried to print their output simultaneously on the same printer. The first few lines of printout might be from program 1, the next few from program 2, then some from program 3, and so forth. The result would be chaos. The operating system can bring order to the potential chaos by buffering all the output destined for the printer on the disk. When one program is finished, the operating system can then copy its output from the disk file where it has been stored for the printer, while at the same time the other program can continue generating more output, oblivious to the fact that the output is not really going to the printer (yet). When a computer (or network) has multiple users, the need for managing and protecting the memory, 110 devices, and other resources is even greater, since the users might otherwise interfere with one another. In addition, users often need to share not only hardware, but information (files, databases, etc.) as well.

In short, this view of the operating system holds that its primary task is to keep track of which programs are using which resource, to grant resource requests, to account for usage, and to mediate conflicting requests from different programs and users.

Resource management includes multiplexing (sharing) resources in two different ways: in time and in space. When a resource is time multiplexed, different programs or users take turns using it. First one of them gets to use the resource, then another, and so on. For example, with only one CPU and multiple programs that want to run on it, the operating system first allocates the CPU to one program, then, after it has run long enough, another one gets to use the CPU, then another, and then eventually the first one again. Determining how the resource is time multiplexed- who goes next and for how long-is the task of the operating system. Another example of time multiplexing is sharing the printer. When multiple print jobs are queued up for printing on a single printer, a decision has to be made about which one is to be printed next.

Tuesday, April 3, 2012

LINUX/UNIX C Shell Alias Command Syntax Example Code

LINUX/UNIX Aliases:
The C shell allows you to create and customize your own commands by using the built-in command alias. Commonly used for a long strings that are frequently used. Alias allows you to have a small more familiar command or name to execute a long string. An alias will last for the life of the shell session.
Regularly used aliases can be set from the shell's configuration file (~/.cshrc or the systemwide /etc/csh.cshrc for csh, or ~/.bashrc or the systemwide /etc/bashrc or /etc/bash.bashrc for bash) so that they will be available upon the start of the corresponding shell session. The alias commands may either be written in the config file directly or sourced from a separate file, typically named .alias (or .alias-bash, .alias-csh, etc., if multiple shells may be used).

Syntax and description of the alias shell command:
Shell Command: alias [word [string] ]
alias supports a simple form of command-line customization. If you alias word to be equal to string and then later enter a command beginning with word, the first occurrence of word is replaced by string and then the command is reprocessed.
If you don't supply word or string, a list of all the current shell aliases is displayed. If you only supply word, then the string currently associated with the alias word is displayed. If you supply word and string, the shell adds the specified alias to its collection of aliases. If an alias already exists for word, it is replaced.
If the replacement string begins with word, it is not reprocessed for aliases to prevent infinite loops. If the replacement string contains word elsewhere, an error message is displayed when the alias is executed.

To give you some exposure of how alias command works on running linux system, below is an example of alias in action:
$ alias dir 'ls -aF' ...register an alias.
$ dir ...same as typing "ls -aF".
./ main2.c p.reverse.c reverse.h
../ main2.o palindrome.c reverse.old
$ dir *.c ...same as typing "ls -aF *.c".
main2.c p.reverse.c palindrome.c
$ alias dir ...look at the value associated with "dir".
ls -aF
$ _

In the following example, I aliased a word in terms of itself:
% alias ls 'ls -aF' ...define "ls" in terms of itself.
% ls *.c ...same as typing "ls -aF *.c".
main2.c p.reverse.c palindrome.c
% alias dir 'ls' ...define "dir" in terms of "ls".
% dir ...same as typing "ls -aF".
./ main2.c p.reverse.c reverse.h
../ main2.o palindrome.c reverse.old
% alias who 'date; who' ...infinite loop problem.
% who
Alias loop.
% alias who 'date; /usr/bin/who' ...full path avoids error
% who ...works fine now.
Fri May 13 23:33:37 CST 2005
smith ttyp0 Feb 13 23:30 (xyplex2)
% _

Removing an Alias:
To remove an alias, use the built-in command unalias.

Syntax and description of the unalias shell command:
Shell Command: unalias pattern unalias removes all of the aliases that match pattern. If pattern is *, then all aliases are removed.

Monday, March 19, 2012

Changing login shell on a Linux machine

How can you change your default login shell on a Linux machine?

Whenever you get your first account on a Linux machine, your account will use Bourne Again Shell as its default login shell. But using bash is not a requirement, you can choose any shell to be your login shell. The chsh utility is used to change your default login shell.

'chsh' allows you to change your default login shell. It prompts you for the full pathname of the new shell, which is then used as your shell for subsequent logins.

For example, I changed my default login shell from Bash to a C shell:

$ chsh ...change the login shell.

Changing shell for smith

Password: ...must verify you are who you say.

New shell [/bin/bash]: /bin/tcsh ...enter full pathname.

$ ^D ...terminate login shell.

login: glass ...log back in again.

Password: ...its private

% _ ...this time I'm in a C shell.


List of full pathnames of the shells as needed for chsh linux utility.

Shell
Full pathname
Bash /bin/bash (or /bin/sh)
Korn /bin/ksh
C /bin/tcsh (or /bin/csh)

There is also another way to find out the full pathname of your login shell, which is to type the following:

$ echo $SHELL ...display the name of my login shell.

/bin/tcsh ...full pathname of the C shell.

$ _

Thursday, March 1, 2012

Gettimeofday.c Code To Measure Creation/Termination Time for a Process/Thread

#include
#include
#include

struct timeval start, stop;

float elapsed_time, seconds, useconds;

void start_timer()
{
gettimeofday(&start, NULL);
}

float stop_timer()
{
gettimeofday(&stop, NULL);

seconds = stop.tv_sec - start.tv_sec;
useconds = stop.tv_usec - start.tv_usec;

elapsed_time = ((seconds) + useconds/1000000.0);

return (elapsed_time);

// printf("Elapsed time: %f seconds\n", elapsed_time);

// return 0;
}