What is an operating system (OS)?
An operating system (OS) is a resource manager. It takes the form of a set of software routines that allow
users and application programs to access system resources (e.g. the CPU, memory, disks, modems, printers,
network cards etc.) in a safe, efficient and abstract way.
For example, an OS ensures safe access to a printer by allowing only one application program to send data
directly to the printer at any one time. An OS encourages efficient use of the CPU by suspending programs
that are waiting for I/O operations to complete to make way for programs that can use the CPU more
productively. An OS also provides convenient abstractions (such as files rather than disk locations) which
isolate application programmers and users from the details of the underlying hardware.
User Applications
Application Programs System Utilities
System Call Interface
Operating System Kernel
Processor/Hardware
UNIX Operating system allows complex tasks to be performed with a few keystrokes. It doesn’t tell or warn
the user about the consequences of the command.
Kernighan and Pike (The UNIX Programming Environment) lamented long ago that “as the UNIX system has
spread, the fraction of its users who are skilled in its application has decreased.” However, the capabilities of
UNIX are limited only by your imagination.
What is Unix?
The UNIX operating system is a set of programs that act as a link between the computer and the user.
The computer programs that allocate the system resources and coordinate all the details of the computer's
internals is called the operating system or kernel.
Users communicate with the kernel through a program known as the shell. The shell is a command line
interpreter; it translates commands entered by the user and converts them into a language that is understood
by the kernel.
Unix was originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken
Thompson, Dennis Ritchie, Douglas McIlroy, and Joe Ossanna.
There are various Unix variants available in the market. Solaris Unix, AIX, HP Unix and BSD are few
examples. Linux is also a flavor of Unix which is freely available.
Several people can use a UNIX computer at the same time; hence UNIX is called a multiuser system.
A user can also run multiple programs at the same time; hence UNIX is called multitasking.
Unix Architecture
UNIX architecture comprises of two major components viz., the shell and the kernel. The kernel interacts
with the machine’s hardware and the shell with the user.
Division of labour
The kernel is the core of the operating system. It is a collection of routines written in C. It is loaded into
memory when the system is booted and communicates directly with the hardware. User programs that need
to access the hardware use the services of the kernel via use of system calls and the kernel performs the job
on behalf of the user. Kernel is also responsible for managing system’s memory, schedules processes, decides
their priorities.
The shell performs the role of command interpreter. Even though there’s only one kernel running on
the system, there could be several shells in action, one for each user who’s logged in. The shell is responsible
for interpreting the meaning of metacharacters if any, found on the command line before dispatching the
command to the kernel for execution.
The File and Process
A file is an array of bytes that stores information. It is also related to another file in the sense that both belong
to a single hierarchical directory structure.
A process is the second abstraction UNIX provides. It can be treated as a time image of an executable file. Like
files, processes also belong to a hierarchical structure. We will be discussing the processes in detail in a
subsequent chapter.
System Calls
It is the programmatic way in which a computer program requests a service from the kernel of the operating
system it is executed on. In UNIX like operating system we have a write system call write on to the file but in
windows we have fprintf library function. Similarly read system call in Unix and fscanf in Windows.
Features of UNIX
Several features of UNIX have made it popular. Some of them are:
Multiuser
The UNIX design allows multiple users to concurrently share hardware and software. This can happen in two
ways:
i. Multiple users can run separate jobs.
ii. A single user can also run multiple jobs.
Multitasking
UNIX allows a user to run more than one program at a time. In fact more than one program can be running
in the background while a user is working foreground. User can switch jobs between background and
foreground, suspend or even terminate them.
The building Block Approach
Instead of executing a single command at a time, Unix provides to execute multiple commands at a time by
using pipe ( | ), semicolon ( ; ) and etc.
Ex: $ ls | wc
Which will counts number of files in your directory. And there is no separate command was designed to
perform the job.
The UNIX toolkit
There are general purpose tools available with UNIX they are, text manipulation utilities, compilers,
interpreters, networked applications and system administration tools.
Pattern Matching
If you wanted to list all the chapters of the text by using the ls command with an argument (chap*) instead
of explicitly specifying all file names. The * is a special character used by the system to indicate that it can
match a number of filenames.
Note: 1. The * isn’t the only special character used by UNIX system, there are several other.
2. The matching is not restricted to filenames only.
Programming facility
The UNIX shell is also a programming language, it was designed for a programmer, not a casual end user. It
has all the necessary statements like, loops and variables that establish it as a powerful programming
language. These features are used to design shell scripts.
Documentation
The online help facility available is the man command, which remains the most important reference for
commands and their configuration files.
POSIX and the Single UNIX Specifications
Unix wasn’t portable. Some of the features (applications) are not portable with UNIX. Later, “Write once,
Adopt everywhere” approach to this development, Institution of Electrical and Electronics engineers (IEEE)
were developed Portable Operating System Interface for Computer Environments (POSIX). Two of the most
cited standards from POSIX are Posix.1 and POSIX.2. POSIX.1 specifies the C application programming
interface and the system calls. POSIX.2 deals with shell and utilities.
Understanding some basic commands
Here we present some of the basic commands used by the user regularly,
1. echo command(DISPLAYING A MESSAGE)
That outputs the strings it is being passed as arguments.
$echo This is a pen $echo “Error: Server not found”
This is a pen Error: Server not found
$ x=5
$ echo The number is $x
The number is 5
2. printf command(AN ALTERNATIVE TO echo)
This command copies its argument back to the terminal.
$ printf This is a pen $ printf “This is a pen”
This is a pen This is a pen
$printf “The number is %d” 5(shouldn’t use comma ( , ) as a separator between format
specifier and the digit)
The number is 5
3. ls command(LIST OF FILES)
The general syntax for ls if as follows,
$ ls [options] [file | dir]
Ex: $ ls --This command lists all the files in the present working directory.
File1 file2 file3 file4
List with long format (with attributes): $ ls -l
It can be used with different options to display file attributes in detail.
ls -l → Displays detailed information such as permissions, number of links, owner, group, size,
and last modified date.
ls -a → Shows all files, including hidden files (files starting with .).
ls -lh → Displays file sizes in human-readable format (KB, MB, etc.).
ls -lt → Lists files sorted by modification time (latest first).
ls -ld → Shows directory attributes instead of its contents.
ls -i → Displays the inode number of files.
4. who command(WHO ARE THE USERS)
who displays all the users currently logged into the system.
Ex: $ who
PC ttyq1 Aug 14 15:23
CSE ttyq0 Aug 28 12:15
$ who –H (displays a header that explains each column)
NAME LINE TIME
PC ttyq1 Aug 14 15:23
CSE ttyq0 Aug 28 12:15
5. date command(DISPLAYING THE SYSTEM DATE)
The date command displays the system date and time.
$ date (displays the current date and local time)
Wed Aug 18 17:56:52 IST 2017
$ date -u (displays the time in GMT)
Wed Aug 18 08:24:19 GMT 2017
6. passwd command(CHANGING YOUR PASSWORD)
The passwd command is used to change your password.
$ passwd
Changing password for-
Old password:
New password:
Re-enter new password:
If we make mistake and the new password don’t agree, we’ll see a message like,
They don’t match
Try again…
7. cal command(THE CALENDER)
This command displays the calendar for a specified month or for a year.
$ cal (displays the present month from calendar)
August 2017
Su Mo Tu We Th Fr Sa
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
$ cal 1 2017 (displays the first month of 2017)
January 2017
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
[Link]: Browsing The Manual Pages Online
UNIX commands are rather cryptic. When you don’t remember what options are supported by a command
or what its syntax is, you can always view man (short for manual) pages to get online help. The man command
displays online documentation of a specified command.
A pager is a program that displays one screenful information and pauses for the user to view the
contents. The user can make use of internal commands of the pager to scroll up and scroll down the
information. The two popular pagers are more and less. more is the Berkeley’s pager, which is a superior
alternative to original pg command. less is the standard pager used on Linux systems. less if modeled after a
popular editor called vi and is more powerful than more as it provides vi-like navigational and search
facilities. We can use pagers with commands like ls | more. The man command is configured to work with a
pager.
9. chmod
The chmod command is used to change file and directory permissions in UNIX.
It controls access by assigning read (r), write (w), and execute (x) permissions.
In the numeric method: read = 4, write = 2, execute = 1.
Permissions are set for owner, group, and others using three digits.
Example: chmod 755 [Link] → owner has read, write, execute; group and others have read and
execute.
Example: chmod 644 [Link] → owner has read and write; group and others have read only.
It is used for security and proper file access control in UNIX systems.
The file System
A simple description of the UNIX system is this:
“On a UNIX system, everything is a file; if something is not a file, it is a process.”
A UNIX system makes no difference between a file and a directory, since a directory is just a file containing
names of other files. Programs, services, texts, images, and so forth, are all files. Input and output devices,
and generally all devices, are considered to be files, according to the system.
Most files are just files, called regular files; they contain normal data, for example text files, executable
files or programs, input for or output from a program and so on.
Directories: files that are lists of other files.
Device Files: All devices and peripherals are represented by files. To read or write a device, you have
to perform these operations on its associated file. Most special files are in /dev.
Link files: a system to make a file or directory visible in multiple parts of the system's file tree.
Types of files:
Ordinary (Regular) File
This is the most common file type. An ordinary file can be either a text file or a binary file.
A text file contains only printable characters and you can view and edit them. All C and Java program
sources, shell scripts are text files. Every line of a text file is terminated with the newline character.
A binary file, on the other hand, contains both printable and nonprintable characters that cover the
entire ASCII range. The object code and executables that you produce by compiling C programs are
binary files. Sound and video files are also binary files.
Directory File
A directory contains no data, but keeps details of the files and subdirectories that it contains. A directory file
contains one entry for every file and subdirectory that it houses. Each entry has two components namely, the
filename and a unique identification number of the file or directory (called the inode number).
When you create or remove a file, the kernel automatically updates its corresponding directory by adding or
removing the entry (filename and inode number) associated with the file.
Device File
All the operations on the devices are performed by reading or writing the file representing the device. It is
advantageous to treat devices as files as some of the commands used to access an ordinary file can be used
with device files as well.
Device filenames are found in a single directory structure, /dev. A device file is not really a stream of
characters. It is the attributes of the file that entirely govern the operation of the device. The kernel identifies
a device from its attributes and uses them to operate the device.
Filenames in UNIX
On a UNIX system, a filename can consist of up to 255 characters. Files may or may not have extensions and
can consist of practically any ASCII character except the / and the Null character. You are permitted to use
control characters or other nonprintable characters in a filename. However, you should avoid using these
characters while naming a file. It is recommended that only the following characters be used in
filenames:
Alphabets and numerals.
The period (.), hyphen (-) and underscore (_).
UNIX imposes no restrictions on the extension. In all cases, it is the application that imposes
that restriction. Eg. A C Compiler expects C program filenames to end with .c, Oracle requires
SQL scripts to have .sql extension.
A file can have as many dots embedded in its name. A filename can also begin with or end
with a dot.
UNIX is case sensitive; chap01, Chap01 and CHAP01 are three different filenames that can
coexist in the same directory.
Directories and Files
A file is a set of data that has a name. The information can be an ordinary text, a user-written computer
program, results of a computation, a picture, and so on. The file name may consist of ordinary characters,
digits and special tokens like the underscore, except the forward slash (/). It is permitted to use special
tokens like the ampersand (&) or spaces in a filename.
vi BASICS
Invoking vi editor
vi <filename>
In all probability, the file doesn’t exist, and vi presents you a full screen with the filename
shown at the bottom with the qualifier, [New file].
The cursor is positioned at the top and all remaining lines of the screen show a ~. They
are non-existent lines.
The last line is reserved for commands that you can enter to act on text. This line is also
used by the system to display messages.
When file is opened in a vi editor, by default it will be opening in a command mode.
This is the mode where you can pass commands to act on text, using most of the keys
of the keyboard.
This is the default mode of the editor where every key pressed is interpreted as a
command to run on text. You will have to be in this mode to copy and delete text.
To enter text, you must switch to the input mode. First press the key i, and moved to
input mode and ready to input text. Subsequent key depressions will then show up on the
screen as text input.
After text entry is complete, the cursor is positioned on the last character of the last line.
This is known as current line and the character where the cursor is stationed is the current cursor position.
This mode is used to handle files and perform substitution.
Sample vi editor file with some text entered
This is the vi editor [Enter]
It operates in three different modes
This is just a sample text.
~
~
~
Use [Esc] key to revert to command mode.
Actually, the text entered has not been saved on disk but exists in some temporary storage
called a buffer.
To save the entered text, you must switch to the execute mode
Invoke the execute mode from the command mode by entering a : which shows up in The
last line.
THE THREE MODES OF vi EDITOR
Command Mode:
When user starts up vi editor, user are in “command mode”. The default mode of the
editor where every key pressed is interpreted as a command to run on text.
This mode allows to copy, delete text (edit files) and to move to any other modes. When
keys are pressed in this mode it simply performs functions, it doesn’t displayed on the
screen. When you are in command mode, letters of the keyboard will be interpreted as
commands.
Some of the command mode commands are : To delete a text command x is used, to
delete entire line use dd command, to put the character use P or p command, to yank the
text use y command, use of yy copies entire line, to undo the recent actions use u
command.
When user in a command mode, the letters of the keyboard will be interpreted as
commands. These commands are one or two characters long, and can be entered with
few keystrokes.
Once the editing is finished save the changes by moving to ex mode by using :w
command.
Unnecessary pressing of [Esc] in this mode sounds a beep but also conforms you are in
this mode.
As shown in the figure, from command mode user can get into the input mode by
giving any of these keys i a I A o O r R s S.
The control can be brought back to the command mode by using the <Esc> key.
From within the command mode the user can get into the ex mod by using the :
Input mode:
To enter the text, have to change from default command mode to Input mode
Every key pressed after switching to this mode actually shows up as text. This mode is
invoked by pressing one of the keys among i a I A o O r R s S.
In the above command list i and I stands for insertion, a and A stands for appending the
text, o and O stands for opening a new blank new line, r and R stands for replacement of
text, s and S stands for substitution of new text. This mode permits the insertion of new
text, appending the existing text and replacement, substitution of text.
The i can be used to insert text anywhere in a line, I insert text only at the beginning of a
line.
To append text to the right of the cursor position use a. To append text at the end of the
line use A.
To open a line below the current line use o. To open a line above the current line use O.
To replace one single character with another, user r followed by the character that
replaces the one under the cursor.
To replace more than a single character use R followed by the text and then press [Esc].
To replace a single character with multi-character text, move the cursor to the point and
then press s.
The S replaces the entire line irrespective of the cursor position. After pressing S, the
entire line vanishes from sight. Key in your text, and then press [Esc].
As shown in the figure, at any time user can go back to the command mode from insert
mode by pressing the <Esc> key.
Ex mode (Last line mode):
This mode is also known as the last-line mode and allows the user to use the command
in the bottom line in vi screen.
This mode is used to handle files (like saving) and perform substitution.
Pressing a : in the command mode invokes this mode and cursor moves to the bottom of
the screen.
The bottom line of the vi screen is called the command line and it’s used to display the
entered ex mode commands. The commands entered in this mode are displayed in the
command line, the last line. Anything entered in front of the colon : prompt it is taken as
an ex command.
Some of the sample ex mode commands are : :w to save the file contents, command
:wq used to save and quit the editor, :q to just quit the editor, :q! to quit the editor by
discarding the changes made
After inserting ex mode command press [Enter], so it moved back to command mode.
After the command is run then it’s moved back to the default command mode.
Switching between the modes is depicted in the following diagram.
As shown in the figure; when user starts up vi editor, user is in “command mode”. From
command mode user can enter to input mode by pressing I,i,a,A,s,S,R,r,O,o commands.
From input mode user can move back to command mode by pressing [esc]. From
command mode to ex mode is invoked by pressing :. From ex mode pressing [enter]
move to the command mode.