You are on page 1of 70

CS1002 - UNIX

INTERNAL
By. K.TAMIZHARASU , HOD/CSE, JCET

UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 1


Brief UNIX History

 In1969, Ken Thompson at AT&T Bell Labs


began developing Unix
– First done in assembly language
– Ran on DEC PDP-7 with 8K words of memory
 In 1974, Unix was rewritten in C
– By writing C compilers, Unix can be ported to
other computers

UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 2


 BSD UNIX
– BSD: Berkeley Software Distribution
– UCB put the Internet Protocols (IP) into
UNIX
– 4.4BSD (Berkeley): 1994
 System V
– AT&T System V (1984)
– SVR4: System V Release 4 (current
version)

UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 3


 POSIX
– Portable Operating System Interface
for Computer Environments (1984)
– Most Unix systems try to conform with
POSIX to a certain extent
 This course is based on SVR4
– Most of the things you will learn in this
course work in different Unix systems,
perhaps with small differences

UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 4


 The UNIX Tools (Utilities)
– Keep each tool simple: have each tool do
one thing, and do that one thing really
well
– Keep tools terse, not too talkative
– More complex tasks can be accomplished
by combining tools together in scripts or
pipelines
– Originally, input and output to workstations
were slow and tedious, and this approach
made things faster and more efficient

UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 5


Overview of UNIX
 Supports various architectures
 Structure varies
 Supports preemptive multitasking
 Multiuser environment - generally secure
 Supports multithreaded applications
 Protection/Security is high on modern versions
 Supports symmetric multiprocessing
 Highly scalable/portable to various systems
 Many types/flavours of UNIX exist

UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 6


UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 7
UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 8
UNIX OS
 UNIX is very popular.
 It is a unique, simple and easy to
understand structure.
 Unix has a number of features.
 Unix is a multi-user system.(More than one
user)
 It is a Multi-process or Multi-tasking.(More
than one job at a time)
 Time Sharing.(More than one user can use
the system at anytime.)
 Portability – Unix is highly portable.
UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 9
UNIX Layers
User
Interface

The layers of a UNIX system.

UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 10


Structure of Unix system

UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 11


UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 12
Structure of Unix System
The Unix system is supported by two agencies, 1. Kernel, 2. Shell.
The Kernal interact with the H/W.
The shell interact with user.(Relation between Kernal & shell).
KERNEL :
 The Kernal is the heart of the system.
It is a collection of prog. Written in C. It communicates with the H/W directly.
There is only one Kernal for any system. The kernal is loaded into the memory
when the system is booted.
It manages the system resources.
It allocates time between users and processes.
It decides process priorities and perform all other tasks which we need not take
care off.
Kernal prog. Are independent of the H/W. The Kernal is often called the OS.
UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 13
Structure of Unix System
SHELL:
The shell is the Sleeping Beauty.
This is an Unix Command.
It is the interpreter of user.
It takes a command from the user, translate it, exchanges
information and communicates with the kernal and executes
the command.
It is an I/F between the user and the kernal. The Shell
insulates the user from the knowledge of kernal functions.
The shell takes care of redirection features (> and |).
UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 14
UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 15
Security for Unix
Unix system has a number of facilities for protecting files.
Password changing.
Password Administration
File Permissions.
Communication:
Unix is used as a means of Electronic mail to communicate with
each other.
Unix has a built in clock and calendar to store time and date.
The Unix has a scheduling mechanism enables s user to run any
job at a specific time or to run it periodically.

UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 16


User Perspective
The UNIX system such as
•The File system
•The Processing Environment
•Building Block Primitives ( Ex. Pipes)

Characteristics of UNIX file System


• A hierarchical structure
• Consistent treatment of file data.
• The ability to create and delete files.
• Dynamic growth of files.
• Protection of file data.
• Treatment of Peripheral devices as files.

UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 17


Context of a process
• Is its states as defined by its text.
• Values of its global user variables and data structures.
• Values of machine registers it uses, the value stored in its
process table slot and its user area.
• The contexts of its user and kernel stack.

UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 18


UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 19
Different types of Files

• Ordinary files
• Directories
• Special files
• Pipes

UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 20


File System
The UNIX system has very large number of files. So it should be organized in a
systematic manner to locate the files.
Unix it is necessary to divide these things into three categories.
Ordinary Files or Regular Files. – A Steam of data which is resident on some
permanent magnetic media.This includes all data, source prog. Object &
executable code, all Unix prog. As well as any file created by the user.
The ordinary files are also referred to as a regular file. All the text files also
belong to this type.
Directory Files. – A directory does not contain any data, but it keeps an account
of all the files and sub-directories that it contains. The Unix file system is
organized with a number of directories & sub-directories which can be created
as and when required.
Device Files. – The physical device are considered as files. Physical device
include printers, tapes, floppy disk, HDD,Terminal etc.

UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 21


Structure of the File
System
• File system is organized as a hierarchy of directories
• It starts from a single directory called root (represented by a /).

/(root)
|
--------------------------------------------------------------------
| | | | | |

/bin /dev /etc /tmp /usr /kernelfile


UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 22
Directories
• Directory is file containing list of files and
subdirectories
• It has fixed size records of 16 bytes each which contains
- a filename(14 bytes)
- an inode number (2 bytes) which acts as a pointer to where
the system can find info about the file.

UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 23


Special Files
• Special files are contained in the directory /dev.
• They are used to represent a real physical device such as a printer,
tape device etc.
• Ex: Special device - /dev/null (unwanted output can be redirected).

UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 24


Pipes
 UNIX allows us to link commands together
using a pipe.
 The pipe acts as a temporary file which
only exists to hold data from one
command until its read by another.
ex:
command1 | command2 | command3....

UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 25


Structure of File Systems
 The login directory src can be specified as /usr/src. It is also called as a path
name.
 If the first character of the path name is “/”, then it indicates that the file
location must be determined with respect to root. Such path names are called
as Absolute Path Names.
 A tree like structure enables us to create as many directories as required.
 The tree like structure permits us to use two files with a same name, but in
different directories.
 The directories shown directly under root are normally found in every
UNIX system.
The directories, “/tmp /bin/etc/usr/unix/dev”
• Are system directories and so one should not try to work in that directory.

UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 26


Structure of File Systems
The other directories /tmp and /usr are meant for the users.
The /bin and /usr/bin directories contain commands like, who, wc,cat, sh,
date etc..
The /dev contain the device files of all H/W device. (/dev/tty00,
/dev/tty01)
The /etc contain utilities used by the system administrator.(/etc/passwd)
The /tmp is used by some UNIX utilities (Visual Editor) as well as by a
user to store temporary files.
The /usr directory contains all the files created by a user, including user
login directory. (/usr/src, /usr/src/cmd/date.c)
Checking the current directory :
Syntax is : $pwd <Enter> . – Present working directory.

UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 27


Access Permissions

Permission to access a file is controlled by access permissions


associated with the file.
Access permissions can be set independently to control
1.Read , 2. Write and execute permission for three classes of users:
* The file owner,
* A file group and
* others
Users may create files if directory access permission allow it.
The newly creates files are leaf nodes of the file system
directory structure.

UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 28


Operating System Services:
• Controlling the execution of processes by allowing their creation
termination or suspension, and communication.
• Scheduling. Processes share the CPU in a Time-sharing manner.
• Allocating main memory for an executing process. The kernel allows
processes to share portions of their address space under certain
conditions, but protects the private address space of a process from
outside tampering.
• If the kernel writes entire processes to a swap device, the
implementation of the UNIX system is called a swapping system. If it
write pages of memory to a swap device, it is called a paging system.
• Allocating secondary memory for efficient storage and retrieval of
user data. The kernel allocates secondary storage for user files,
structures the file system in a well understood manner, and protect user
files from illegal access.
•Allowing process controlled access to peripheral devices such as
terminals, tape drives, disk drives and network devices.

UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 29


Assumptions about H/W:

The execution of user processes on UNIX systems is divided into


two levels:
* User Mode – Processes in user mode can access their
instructions and data but not kernel instructions and data. Some
machine instructions are privileged and result in an error when
executed in user mode.

* Kernel Mode.- Can access kernel and user addresses.


When a process executes s system call, the execution of
the process changes from user mode to kernel mode.

UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 30


UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 31
Architecture of the UNIX OS.

lock diagram of the system kernel.

wo major components of the kernel

File subsystem and

process control subsystem.

hree levels
UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 32
Architecture of the Unix OS.
The set of system calls into those that interact
with
1. File subsystem and
2. Process control subsystem.

The file subsystem and the process control


subsystem interact when loading a file into
memory for execution.
File subsystem manages files allocates file space.
1. Administers free space.
2. Controls access to files and retrieves data to users.
3. File subsystem uses buffering mechanism for block I/O
4.Device drivers and interacts directly with raw I/O device

drivers.
UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 33
System Kernel
The File subsystem and those that interact with the Process control
subsystem.

File subsystem:
•Process interact with the file subsystem via a specific set of system
calls, such as open, close, read, write, stat, chown (Change the record
of owner), chmod ( i.e access permission).
•The file system accesses file data using a buffering mechanism. Data
flow between the kernel and secondary storage devices.
•The buffering mechanism interacts with block I/O device drivers to
initiate data transfer to and from the kernel.
•Device drives are the kernel models that control the operation of
peripheral devices.
UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 34
Process Control Subsystem
The process control subsystem is responsible for process
synchronization, interprocessor communication, memory
management, and process scheduling.
The file subsystem and the process control subsystem interact when
loading a file into memory for execution .The process subsystem
reads executable files into memory before executing them.
Some of the system calls:
•fork – Create a new process.
•exec- Overly the image of a program onto the running process.
•exit – Finish executing a process.
•wait – synchronize process execution with the exit of a previously
forked process).
•brk – control the size of memory allocated to a process.
•signal – control process response to extraordinary events.

UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 35


Memory management modules
Memory management modules controls the allocation of memory. The
swapper process is sometime called the scheduler. The scheduler modules
allocates the CPU to processes.

It schedules them to run in turn until they awaiting a resource or until the
kernel preempts them when their recent run time exceeds a time quantum.

scheduler then chooses the highest priority eligible process to run; the
original processes run again when it is the highest priority eligible process
available.

H/W controller is responsible for handling interrupts and for


communicating with the machine. Device such as disks or terminals may
interrupt the CPU while a process is executing.

UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 36


1. Important fields of ‘U’ area?
- a pointer to the process table slot of the currently executing
process parameters of the current system call, return values and
error codes
- file descriptors for all open files
- Internal 1/0 parameters
2. Services provided by the kernel?
-controlling the execution of processes by allowing their
creation, termination or suspension and communication
-scheduling processes fairly for the execution on the CPU
-allocating main memory for an executing process
-allocating secondary memory for efficient storage and retrieval
of user data
UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 37
System concepts
Introduction to System concepts:
• File system

• Process
- Context of a process
- Sate transitions
- Sleep and wakeup

UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 38


UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 39
3. Fields of super block?
-the size of the file system
-the number of free blocks in the file system
-a list of free blocks available on the file system
-the index of the next free block in the free block list
4. Formula for computing the block number and byte offset of a
mode in a block?
Block number
Block nunv=((inode number-i )/number of modes per block)+
start block of mode list
Byte offset
((mode number-i) modulo (number of modes per block))*size of
disk mode

UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 40


File system

Boot block super block inode list data blocks

• Boot block occupies the beginning of a file system. The first


sector, and may contain the bootstrap code that is read into the
machine to boot or initialize the OS. Only one boot block is needed
to boot the system, every file system has a boot block.
Inode List :
The kernel reference inodes (i.e. index node) by index into the inode
list. One inode is the root inode of the file system. Administrators
specify the size of the inode list when configuring a file system.

UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 41


Booting UNIX (1)
 The first sector of the boot disk (master
boot record) is read in and executed
 This sector loads the boot program
 Boot reads root directory, loads kernel and
starts its execution
 Kernel reads the rest of the operating system
(main C-code section)
 C code does some initialization, allocates
system data structures, loads device drivers
and handcrafts the first process, process 0

UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 42


Booting UNIX (2)

cp
The sequences of processes used to boot some systems

UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 43


Super block
 The size , no of free blocks available on the file system.
 The index of the next free block in the free block list, the size
of the inode list, the no of free inodes in the file system.
 The list of free inodes in the file system, the index of the next
free inode in the free inode list.
 Lock fields for the block and free inode list, a flag indicating
the super block has been modified.
Data Blocks:
•The data blocks start at the end of the inode list and contain file data and
administrative data. An allocated data block can belong to one and only one
file in the file system.
A file system consists of a sequence of logical blocks, each containing 512,
1024, 2048 or multiple of 512 bytes, depending on the system
implementation. Using large logical blocks increase the effective data transfer
rate between disk and memory, because the kernel can transfer more data per
disk operation and therefore make fewer time-consuming operations.

UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 44


UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 45
UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 46
UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 47
UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 48
UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 49
UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 50
UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 51
UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 52
Many process appear to execute stimulatingly as the kernel
schedules them for execution, and several processes may
be instances of one program.

A process on a, UNIX system is the entity that is created by


the fork system call. Every process except process 0 is
created when another process executes the fork system call.
The process that invoked the fork system call is the parent
process, and the newly created process is the child process.
Every process has one parent process, but a process can
have many child process. The kernel identifies each
process by its process number, called the process ID (PID).

UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 53


PROCESS

Process 0 is a special process that is create by hand when


the system boots; after forking a child process (process 1 ),
process 0 becomes the swapper process. Process 1, known
as init.

Discuss in detail about the various process


transitions.
States of a process.
o       The process is currently executing in user mode.
o       The process is currently executing in kernel
mode.
o       The process is not executing but it is ready to
run.
o      
UNIT-I The process isK.TAMIZHARASU,
sleeping. HOD/CSE, JCET 54
UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 55
Process transitions

State transition diagram

Kernel allows context switch only when process moves

from the state kernel running to the state a sleep in

memory.

Kernel is sometimes said to be non preemptive.

The process moves from user mode to kernel mode on a


UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 56
Sleep and wakeup process

Sleep process :
Processes go to sleep because they are awaiting the

occurrence of some event.

The event can be


 Waiting for I/O completion from a peripheral device.
 Waiting for a process to exit.
 Waiting for system resources to become available.

Sleeping processes do not consume CPU resources.


UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 57
Wakeup Process
 On the occurrence of an event the sleeping process moves to the ready
to run state.
 Kernel implements locks by checking a condition and sleeps the
process.
 It unlocks the lock and awakens all the processes asleep on the lock.

Set condition false;


wakeup (event: the condition is false);

UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 58


Building Block Primitives:

The Unix system is to provide OS primitives.


• redirect I/O.
• Pipe.
redirect I/O : Processes conventionally have access to three files.
They read from their standard input file.
Write to their standard output files. And
Write error messages to their standard error file.
Example : ls : Lists all the files in the current directory on the
standard output.
ls > output
where > redirects the standard output to the file called :output “ in
the current directory, using the create system call.

UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 59


The second building block primitive is the pipe:

A mechanism that allows a stream of data to be


passed between reader and writer processes.

Processes can redirect their standard output to a pipe


to be read by other processes that have redirect their
standard input to come from the pipe.

The programmer uses the pipe primitive and


redirection of I/O to integrate the piece parts.

UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 60


Interrupts and exceptions :
Interrupts to happen between the execution of two
instructions.
The hardware usually prioritizes devices according to the
order that interrupts should be handled.

When the kernel services an interrupt, it blocks out lower


priority interrupts but services higher priority interrupts.

Exceptions :

Exceptions happen “ in the middle “ of the execution of an


instruction.

UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 61


Kernel Data Structures
Most kernel data structure occupy fixed-size tables rather than
dynamically allocated space. The file table, with one entry allocated
for every opened file in the system, the user file descriptor table,
with one entry allocated for every file descriptor known as a
process, and the mount table, containing information for every
active file system.
System Administration:
Administrative processes are loosely classified as those process that
do various functions. Such function include disk formatting, creation
of new file systems, repair of damaged file systems, kernel debugging,
and others.
Buffer Headers:
The kernel allocates space for a number of buffers, configurable according to
memory size and system performance. A buffer consists of two parts:
A memory array – That contains data from the disk and a buffer header that
identifies the buffer. A one to one mapping of buffer headers to data arrays.

UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 62


UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 63
UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 64
SVR4 – Morden Unix Kernal
SVR4 (System Version Realizes 4) : Morden Unix
Kernal:
New features in the release include

Real-time processing supports


Process scheduling classes
Dynamically allocated data structures
Virtual memory management
Virtual file system and
A preemptive kernel.

UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 65


UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 66
UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 67
UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 68
UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 69
UNIT-I K.TAMIZHARASU, HOD/CSE, JCET 70