Professional Documents
Culture Documents
Shell
The Kernel
System
Hardware
Kernel:
APPLICATION
• Core and most important part of the OS
KERNEL
• Monolithic Kernel
• Micro Kernel
• ExoKernel
HARDWARE
Kernel Architecture (UNIX) :
User program
Library User level
Inter process
File Subsystem
communication
Device driver
User Mode
Kernel Mode
Micro Kernel :
User Mode
Kernel Mode
Hybrid Kernel :
User Mode
Kernel Mode
ExoKernel :
GRUB-I
• GRand Unified Bootloader
• 2 boot signature
Inter process
File Subsystem
communication
Device driver
device num
Buffer Header
Buffer Header Status Bits :
• #define BH_Uptodate /*1 if the buffer contains valid
data*/
Forward ptrs
Back ptrs
28 4 64
blkno0 mod 4
blkno1 mod 4 17 5 97
blkno2 mod 4 98 50 10
blkno3 mod 4 3 35 99
28 4 64
blkno0 mod 4
17 5 97
blkno1 mod 4
98 50 10
blkno2 mod 4
blkno3 mod 4 3 35 99
freelist header
17 5 97
blkno1 mod 4
98 50 10
blkno2 mod 4
blkno3 mod 4 3 35 99
freelist header
24
28 4 64
blkno0 mod 4
17 5 97
blkno1 mod 4
98 50 10
blkno2 mod 4
blkno3 mod 4 3 35 99
freelist header 25
28 4 64
blkno0 mod 4
17 5 97
blkno1 mod 4
98 50 10 18
blkno2 mod 4
blkno3 mod 4 35 99
freelist header
26
28 4 64
blkno0 mod 4
17 5 97
blkno1 mod 4
delay
98 50 10
blkno2 mod 4
blkno3 mod 4 3 35 99
delay
freelist header
27
28 64
blkno0 mod 4
17 5 97
blkno1 mod 4
writing
98 50 10 18
blkno2 mod 4
blkno3 mod 4 3 35 99
writing
freelist header
28 4 64
blkno0 mod 4
17 5 97
blkno1 mod 4
blkno2 mod 4 98 50 10
blkno3 mod 4 3 35 99
freelist header
29
28 4 64
blkno0 mod 4
17 5 97
blkno1 mod 4
98 50 10
blkno2 mod 4
blkno3 mod 4 3 35 99
busy
freelist header 30
32
Reading and Writing disk blocks
• To read block ahead
• The kernel checks if the block is in the cache or not.
• If the block in not in the cache, it invokes the disk driver to read the
block.
• The the process goes to sleep awaiting the event that the I/O is
complete.
• The disk controller interrupts the processor when the I/O is complete
• The disk interrupt handler awakens the sleeping processes
• The content of disk blocks are now in the buffer
• When the process no longer need the buffer, it releases it so that other
processes can access it
33
Reading and Writing disk blocks
• To write a disk block
• The kernel informs the disk driver that it has a buffer whose contents
should be output.
• The disk driver schedules the block for I/O.
• If the write is synchronous, the calling process goes the sleep awaiting
I/O completion and releases the buffer when awakens.
• If the write is asynchronous, the kernel starts the disk write. The kernel
release the buffer when the I/O completes.
34
Internal Representation Of File:
Lower Level File system Algorithms
namei
alloc free ialloc ifree
iget iput bmap
• Algorithms alloc and free allocate and free disk blocks for files
• Algorithms ialloc and ifree assigns and free inodes for files.
Inode :
• contains the information necessary for a process to access a
file
• exits in a static form on disk and the kernel reads them into an
in-core inode
• consists of
- file owner identifier
- file type
- file access permissions
- file access times
- number of links to the file
- table of contents for the disk address of data in a file
- file size
Inodes :
• in-core copy of the inode contains
- status of the in-core inode
- logical device number of file system
- inode number
- pointers to other in-core inodes
- reference count
Algorithm iget :
1. The kernel finds the inode in inode cache and it is on inode free list
remove from free list
increment inode reference count
2. The kernel cannot find the inode in inode cache so it allocates a
inode from inode free list
remove new inode from free list
reset inode number and file system
remove inode from old hash queue, place on new one
read inode from disk(algorithm bread)
initialize inode
3. The kernel cannot find the inode in inode cache but finds the free
list empty
error
4. The kernel finds the inode in inode cache but it was locked
sleep until inode becomes unlocked
iget (inode_no) //getIncoreInode
• while (not done)
• if (inode in inode cache)
• if (inode locked)
• sleep(event inode becomes unlocked)
• continue
• if (inode on inode free list)
• remove from free list
• return locked inode
• if (no inode on free list)…. return error
• remove new inode from free list
• set inode number
• remove inode from old hash queue and place on new one
• read inode from disk
• set reference count 1
• return locked inode
Algorithm iput :
- The kernel locks the inode if it has not been already locked
- The kernel decrements inode reference count
- The kernel checks if reference count is 0 or not
- If the reference count is 0 and the number of links to the file is
0, then the kernel releases disk blocks for file(algorithm free),
free the inode(algorithm ifree)
• If the file was accessed or the inode was changed or the
file was changed , then the kernel updates the disk inode
• The kernel puts the inode on free list
- If the reference count is not 0, the kernel releases the inode
lock
iput (inode_no) //releaseIncoreInode
• lock inode if not locked
• decrement inode reference count
• if (reference count==0)
• if (inode link==0)
• free disk block
• set file type to 0
• free inode
• if (file accessed or inode changed or file changed)
• update disk inode
• put inode on free list
• Release inode lock
Structure of a regular file :
Inode Data Blocks
direct0
direct1
direct2
direct3
direct4
direct5
direct6
direct7
direct8
direct9
single indirect
double indirect
triple indirect
Fig. Direct and Indirect Blocks in Inode
Structure of a regular file :
Assuming System V UNIX :
Assume that a logical on the file system holds 1K bytes and that a block
number is addressable by a 32 bit integer, then a block can hold up to
256 block numbers
• consists of
- 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
- the size of the inode list
- the number of free inodes in the file system
- a list of free inodes in the file system
- the index of the next free inode in the free inode list
- lock fields for the free block and free inode lists
- a flag indicating that the super block has been modified
Inode assignment to a new file :
18 19 20
array1
index
18 19 20
array2
index
Assigning Free Inode from Middle of List
Inode assignment to a new file :
Super Block Free Inode List
470 empty
0
array1
index
0
48 49 50
109
211 208 205 202 ………………… 112
211
310 307 304 301 ………………… 214
310
409 406 403 400 ………………… 313
109
211 208 205 202 …………………………….. 112
original configuration
109
211 208 205 202 ………………………………. 112
109
211 208 205 202 ………………………………. 112
211
344 341 338 335 ………………………………. 243
• Contiguous allocation
• Linked allocation
• Indexed allocation
Contiguous allocation :
Linked allocation :
Indexed allocation :
Access Methods :
• Sequential Access
read next
write next
reset
no read after last write
(rewrite)
• Direct Access
read n
write n
position to n
read next
write next
rewrite n
n = relative block number
Free Space Management :
• Bit vector (n blocks)
0 1 2 n-1
…
bit[i] =
1 block[i] free
0 block[i] occupied
• Relatively simple
Free Space Management :
• Linked list
• Hard to find contiguous space easily
• But no waste of space
• Grouping
• Store addresses of n free blocks in the first block
• Last of these addresses is to a block that contains addresses
of another n free blocks
• So many free blocks can be found at one time
• Counting
• Clusters of contiguous free blocks recorded together
• Keep list of first block address, count of contiguous free
ones
Disk Management :
• Low-level formatting, or physical formatting — Dividing a disk into
sectors that the disk controller can read and write
• Each sector can hold header information, plus data, plus error correction
code (ECC)
• Usually 512 bytes of data but can be selectable
• To use a disk to hold files, the operating system still needs to record its
own data structures on the disk
• Partition
• Logical formatting
• To increase efficiency most file systems group blocks into clusters
• Disk I/O done in blocks
• File I/O done in clusters
• Boot block
• bootstrap stored in ROM
• Bad Blocks
• Sector Sparing
• Sector Slipping
Swap Space :
• Swap-space — Virtual memory uses disk space as an extension of
main memory.
• Swap-space Use :
• Hold the process image
• Swap-space Location :
• Using normal file system
• Separate Raw partition