Session –3 VI Editor
&
File commands
Review
Inode structure
Mode Time Stamp Direct Blocks Indirect Blocks Double Indirect Blocks Triple Indirect Blocks Owner Info SizeReview
Directory
Directory is a file
Contains sequential list of inode number and file name mapping for all files and subdirectories
Size of directory entry was 16 bits on Unix systems –
2 bytes for inode and 14 bits for file name
Reading Directory
System calls to read directory
• opendir --- to open directory file • readdir --- to read director entry
struct dirent {
long d_ino; /* inode number */
off_t d_off; /* offset to next dirent */
unsigned short d_reclen; /* length of this dirent */
char d_name [NAME_MAX+1]; /* filename (null-terminated) */ }
Directory hierarchy example
• Current Directory
[pilani@labserver sample]$ pwd /home/pilani/sample
• inode list of current directory
[pilani@labserver sample]$ ls -ai
2490381 . 2490384 direntry 2490383 file1hlink.txt 2490382 file2.txt
2490369 .. 2490387 direntry.c 2490383 file1.txt
[pilani@labserver sample]$ ls -ai /home/pilani
2490369 . 2490376 cs1 2 .. 2490381 sample
• inode list of pilani home directory
[pilani@labserver sample]$ ls -ai /home/ 2 . 2490369 pilani
2 .. 2621441 pilani1
Inode role in file operations
Example: Opening file /home/pilani/sample/file1.txt
• Kernel has inode number of root (/)
• Load data block of / and find inode number of home • Load data block of /home and find inode of sample
• Load data block of /home/sample and find inode of file1.txt • Load first data block of file1.txt
File permission
What does executable permission of directory mean?
File permission
What does executable permission of directory mean?
File listing details
[user1@labserver ~]$ ls -l
total 4
-rw-r--r-- 1 user1 user1 36 Aug 30 16:02 file1.txt
File Type – Directory/File/Link Permission to Owner - r/w/x Owner group name
File size Date of last change
File Modification time
File has two data
Content (data)
Metadata
Changing contents changes Metadata also
Changing Metadata may not change content
$ls -lu Display access time
$ls –lc Display change time You can also use stat command
File listing details
[user1@labserver ~]$ ls -l
total 4
-rw-r--r-- 1 user1 user1 36 Aug 30 16:02 file1.txt
File Type – Directory/File/Link Permission to Owner - r/w/x ? Owner group name
File size Date of last change
File listing details
[user1@labserver ~]$ ls -l
total 4
-rw-r--r-- 1 user1 user1 36 Aug 30 16:02 file1.txt
File Type –
Directory/File/Link
Permission to Owner - r/w/x
Number of files
linked to this inode
Owner group name
File size Date of last change
VI Editor
Powerful Text Editor on Unix
Enhanced versions on Linux -- vim, gvim
Need some learning time but once learned editing text files (scripts, code, documents..) becomes
Modes
File Xxxxxxxxxxxxxxxxx Xxxxxxxxxxxxxxxxx Xxxxxxxxxxxxxxxxxx Xxxxxxxxxxxxxxxxxx Search, move, Replace, delete Command Mode File Xxxxxxxxxxxxxxxxx Xxxxxxxxxxxxxxxxx Xxxxxxxxxxxxxxxxxx Xxxxxxxxxxxxxxxxxx Xxxxxxxxxxxxxxxxxxx Edit Mode i,a,o Escape File Xxxxxxxxxxxxxxxxx Xxxxxxxxxxxxxxxxx Xxxxxxxxxxxxxxxxxx Xxxxxxxxxxxxxxxxxx :w,s,q,Command Line Mode
Escape & :
File Management
File Descriptor
Handle or Index returned to process when file is opened e.g. using fopen call
File Descriptor Table
Maintained by Kernel for each process
Contains entries for all files opened by the process
File descriptor indexes into this table
File Entry
System wide tabled maintained by Kernel
Contains entries for all files opened by all processes on this server
Per process file descriptor table entries indexes into this table to access the file
Device Drivers
Every physical device has its own hardware controller (apart from CPU)
Device Drivers are low level hardware controller handling software routines
Each device in the system is represented by special device file and listed in /dev directory
Devices are identified by major and minor number
Major number mapping can be viewed from file /proc/devices
Device Drivers
Block Devices
Examples: Hard disk, RAM, CDROM …
Usually I/O is performed using buffer cache
Device file is identified by ‘b’
Character Devices
Examples: keyboard, printer, …
I/O is performed byte/stream without buffering
Device Drivers
Block Devices
Examples: Hard disk, RAM, … Device file is identified by ‘b’
Character Devices
Examples: keyboard, printer, … Device file is identified by ‘c’
$ls –l /dev
brw-rw---- 1 root disk 1, 0 Sep 22 13:21 ram0 brw-rw---- 1 root disk 8, 0 Sep 22 13:21 sda crw-rw-rw- 1 root tty 5, 0 Sep 22 13:21 tty crw-rw---- 1 root lp 6, 0 Sep 22 13:21 lp0
Device file
vs
regular file?
$ls –li /dev/ram0
8207 brw-rw---- 1 root disk 1, 0 Sep 22 13:21 /dev/ram0
$ls –li file2.txt
2490382 -rw--- 1 pilani pilani 42 Sep 17 10:46 file2.txt
$ls –ldi /home/pilani/sample
2490381 drwx--- 2 pilani pilani 4096 Sep 21 13:48 /home/pilani/sample/
Device file
vs
regular file?
$ls –li /dev/ram0
8207 brw-rw---- 1 root disk 1, 0 Sep 22 13:21 /dev/ram0
$ls –li file2.txt
2490382 -rw--- 1 pilani pilani 42 Sep 17 10:46 file2.txt
$ls –ldi /home/pilani/sample
2490381 drwx--- 2 pilani pilani 4096 Sep 21 13:48 /home/pilani/sample/
Type: b/c/-/d
Size: File content size/
Device file
vs
regular file?
$ls –li /dev/ram0
8207 brw-rw---- 1 root disk 1, 0 Sep 22 13:21 /dev/ram0
$ls –li file2.txt
2490382 -rw--- 1 pilani pilani 42 Sep 17 10:46 file2.txt
$ls –ldi /home/pilani/sample
2490381 drwx--- 2 pilani pilani 4096 Sep 21 13:48 /home/pilani/sample/
Device Driver Sections
Auto Configuration Routines I/O Servicing Routines Interrupt Service RoutinesTriggered due to system calls
Triggered due to hardware interrupts Triggered during driver
Device Driver
Interrupt driven device driver
Controller raises interrupt when it is ready to be
serviced e.g. network driver is ready when packet is received from network
Kernel passes on interrupt to correct device driver to process the interrupt