Operating Systems , 132

40
Operating Systems, 132 Practical Session 12 File Systems, part 2 1

description

Operating Systems , 132. Practical Session 12 File Systems, part 2. File system layout ( Tanenbaum ). Quick recap: i -Nodes. An i -node (index node) is a data structure containing pointers to the disk blocks that contain the actual file contents. - PowerPoint PPT Presentation

Transcript of Operating Systems , 132

Page 1: Operating Systems ,  132

Operating Systems, 132

Practical Session 12 File Systems, part 2

1

Page 2: Operating Systems ,  132

File system layout (Tanenbaum)

2

Page 3: Operating Systems ,  132

Quick recap: i-Nodes

โ€ข An i-node (index node) is a data structure containing pointers to the disk blocks that contain the actual file contents.

โ€ข Every i-node object represents a single file.โ€ข An i-node needs to be in Main Memory only if

the correspondent file is open.

3

Page 4: Operating Systems ,  132

Quick recap: FAT

โ€ข A FAT (File allocation table) contains the chains of disk blocks. Each chain defines a different file.

โ€ข Every entry in the FAT points to the next disk block of the file.

โ€ข Chains of blocks are terminated by a special marker โ€“ end_of_file character.

โ€ข Directory entry points to first block in a file (i.e. specifies the block number).

โ€ข The FAT is held in Main Memory and its size is proportional to the disk size.

Page 5: Operating Systems ,  132

Recap: MS-DOS directory entryโ€ข Tree structure (no links)โ€ข directories provide information about location of file blocks (directly or

indirectly..)โ€ข Both names and attributes are IN the directory

MS-DOS uses fixed size 32-byte directory entries

โ€ข read-onlyโ€ข hiddenโ€ข systemโ€ข archive

An index into the 64K โ€“entry FAT

5

Page 6: Operating Systems ,  132

NTFSโ€ข Each file is represented by a record in a special file called

the master file table (MFT).โ€ข Directories are also files.โ€ข The first 16 records of the table are reserved for special

information and the first record of this table describes the master file table itself, followed by a MFT mirror record.

โ€ข The seventeenth and following records of the master file table are for user files and directories.

โ€ข The master file table allocates a certain amount of space for each file record (for attributes and initial data).

โ€ข Small files can be entirely contained within the master file table record in this case file access is very fast.

Page 7: Operating Systems ,  132

MFT records

Page 8: Operating Systems ,  132

The MFT record of a long file

A file that requires three MFT records to store its runs

Ben-Gurion University Operating Systems, Semester B 2008, Danny Hendler8

Page 9: Operating Systems ,  132

NFS โ€“ Network File System

โ€ข Allows sharing of file Systems.โ€ข A server exports part of its file system.โ€ข A client can mount an exported file system.โ€ข A stateless protocol. The server remembers

nothing about previous requests from a client.

Page 10: Operating Systems ,  132

10

EXT4 NTFS 6 FAT32

Allocation method Indexed Indexed, by โ€œrunsโ€ Linked

File representation

i-node(default size 256KB)

MFT record(default size 1Kb)

Chain of clusters pointed from a Directory entry

Location of filename Directory entry Directory entry,

MFT record Directory entry

Location of attributes i-node MFT record Directory entry

Data accessi-node contain pointers with different levels of

indirection

For small files- the contents inside the MFT

record, for larger- organized as 'runs'

which may span multiple MFT records

Directory entryPoints to 1st link in

chain

Location of index table

i-nodes table immediately after the

superblockMFT anywhere, pointed

by MBRFAT anywhere,

pointed by MBR

Design compare

Page 11: Operating Systems ,  132

11

EXT4 NTFS 6 FAT32

Built in security Yes Yes No

Recoverability Yes, via data and metadata journaling

Yes, via metadata journaling No

Efficient disk usage Yes, via sparse files Yes, via compression,

sparse files No

Fragmentation problem No Some, tries to store

files sequentially Yes

Max file size 16TB(for 4k block)

16EB by design, 16TB by implementation 4GB

Max volume size 1EB(1EB=1,000,000TB) 16EB 2TB

Max filename length

255 chars(case sensitive)

255 chars(case sensitive)

Originally 8+3, extended to 255

(not case sensitive)

Hard links Yes Yes No

Soft links Yes Yes No

Features compare

Page 12: Operating Systems ,  132

12

EXT4 NTFS 6 FAT32

Pros

โ€ข Supports large volumes and files

โ€ข Built in security and permissions

โ€ข Recoverabilityโ€ข Unicode file namesโ€ข Extended file attributesโ€ข No fragmentation

problems, no maintenance needed

โ€ข Small memory footprint

โ€ข Modernโ€ข Supports large volumes

and filesโ€ข Built in security and

permissionsโ€ข Recoverabilityโ€ข Unicode file namesโ€ข Extended file attributesโ€ข High performance on

large directories and files

โ€ข Simpleโ€ข Widely supportedโ€ข Efficient for small

files on small volumes and sequential access

Cons

โ€ข Compatibility issues with other OS

โ€ข Larger disk footprint than NTFS

โ€ข Fragmentation problemโ€ข Inefficient on small

volumesโ€ข Performance degrades

when disk is almost fullโ€ข Size of MFT grows with

usage

โ€ข Obsoleteโ€ข Not suitable for

large volumes and files

โ€ข No recoverabilityโ€ข No securityโ€ข No user permissionsโ€ข Fragmentation

reduce performance

Pros & Cons

Page 13: Operating Systems ,  132

Question 1: FAT

A FAT starts with the following values:

Assuming that slots with -1 indicates an end of file and slots with 0 indicates a free slot:1. If the directory containing a certain file has 7 as

the starting block, how many blocks does that file contain?

2. The owner of the file has written two more blocks to the file. Adjust the FAT to the change.

Page 14: Operating Systems ,  132

Question 1: FAT

1. 42. 14 , 19 , 8 , -1 , -1 , 9 , 3 , 2 , 5 , 10 , -1

0 X1 X2 EOF3 134 25 96 87 Free8 Y9 12

10 311 FREE12 EOF13 EOF14 FREE15 BAD

Not allocated

End of file

Marked as a bad block

6 8 4 2

5 9 12

10 3 13

File A:

File B:

File C:

Page 15: Operating Systems ,  132

Question 2: FAT (2007a)

ื‘ืฉืืœื” ื–ื• ืขืœื™ื›ื ืœื”ืฆื™ืข ืฉื™ื ื•ื™ื™ื )ื‘ืžื‘ื ื™ ื”ื ืชื•ื ื™ื ืขืœ ืžื ืช MS-DOSื•ื‘ืงื•ื“( ืฉืœ ืžืขืจื›ืช ื”ืงื‘ืฆื™ื ืฉืœ

ืœืื—ืจ ื”ืฉื™ื ื•ื™, ืชืชืžื•ืš . linksืœื”ื•ืกื™ืฃ ืœื” ืชืžื™ื›ื” ื‘-MS-DOS:ื‘ืฉืชื™ ื”ืคืงื•ื“ื•ืช ื”ื—ื“ืฉื•ืช ื”ื‘ืื•ืช

ื”ืคืงื•ื“ื”:โ€“ soft-link old-path new-path

.old-path ืืœ new-path ื—ื“ืฉ ืž-soft link ื™ื•ืฆืจืช ื”ืคืงื•ื“ื”:โ€“ hard-link old-path new-path

.old-path ืืœ new-path ื—ื“ืฉ ืž-hard link ื™ื•ืฆืจืช

Page 16: Operating Systems ,  132

Question 2: FAT (2007a) MS-DOS ื ืงื•ื“ื•ืช( ืื ื• ืจื•ืฆื™ื ืœื”ื•ืกื™ืฃ ืœืžืขืจื›ืช ื”ืงื‘ืฆื™ื ืฉืœ 13)ื.

ืžื‘ืœื™ ืœื‘ืฆืข ืฉื™ื ื•ื™ ื›ืœืฉื”ื•ื ื‘ื˜ื‘ืœืช , soft linksืชืžื™ื›ื” ื‘-. ื•ืžื‘ืœื™ ืœื”ื•ืกื™ืฃ ืžื‘ื ื™ ื ืชื•ื ื™ื ื—ื“ืฉื™ืFATื”-

)a ืชืืจื• ื‘ืคื™ืจื•ื˜ ืื™ืœื• ืžื‘ื ื™ ื ืชื•ื ื™ื ืฉืœMS-DOS.ื™ืฉืชื ื• ื•ื›ื™ืฆื“

, ื”ืžืฆื™ื™ืŸ directory entry ื‘-flagื”ืฉื™ื ื•ื™ ื”ื™ื—ื™ื“ ื”ื ื“ืจืฉ ื”ื™ื ื• ืชื•ืกืคืช ืื• ืœื. symbolic link ื”ื™ื ื• entryื”ืื ื”ืงื•ื‘ืฅ ื”ืžื•ืฆื‘ืข ืข"ื™ ื”-

ื”ื™ื ื• entry ืžื•ืจื, ืชื•ื›ื ื• ืฉืœ ื”ืงื•ื‘ืฅ ืฉืฉืžื• ืžื•ืคื™ืข ื‘-flagื‘ืžื™ื“ื” ื•ื”-(.old-path )ื›ืœื•ืžืจ, linkื”-

Page 17: Operating Systems ,  132

Question 2: FAT (2007a))bื›ืชื‘ื• ืคืกืื•ื“ื•-ืงื•ื“ ื”ืžืชืืจ ืืช ืคืขื•ืœืชื” ืฉืœ ืคืจื•ืฆื“ื•ืจื”

soft-link(old-path, new-path) .soft-linkื”ืžืžืžืฉืช ืืช ื”ืคืงื•ื“ื”

Procedure soft-link(old-path, new-path) } // get directory-name and entry-name Convert new-path to <dirName, entryName> // allocate a new directory entry in directory dirName new-entry โ† allocate-new-entry(dirName) new-entry.filename โ† entryName new-entry.link โ† true // indicate that this is a symbolic link allocate a single block, initialize with old-path,

set it as new-entry's single block new-entry.data โ† old-path {

Page 18: Operating Systems ,  132

Question 2: FAT (2007a)c ื”ืื ื ื“ืจืฉื™ื ืฉื™ื ื•ื™ื™ื ื ื•ืกืคื™ื ื‘ืงื•ื“ ืฉืœ )MS-DOS.ืื ื›ืŸ, ืฆื™ื™ื ื• ืื•ืชื ื‘ืงืฆืจื” ?

ืฉื‘ื• )ืื ื™ืฉื ื directories ืขื•ื‘ืจื™ื ืขืœ ื”-pathname ืœ-parsingื›ืืฉืจ ืžื‘ืฆืขื™ื )ื‘ืชืœื•ืช current directory ืื• ื”-root directoryื›ืืœื”( ืขืœ ืคื™ ื”ืกื“ืจ, ื”ื—ืœ ืžืŸ ื”

ื‘ื• ื ืชืงืœื™ื, ื™ืฉ ืœื‘ื“ื•ืง name(. ืขื‘ื•ืจ ื›ืœ ืฉื relative path ืื• absoluteืื ื–ื”ื• ืฉื”ื•ืกืคื ื•. ืื ื›ืŸ, ื™ืฉ ืœืงืจื•ื ืืช ืชื•ื›ืŸ flag, ืข"ื™ ื‘ื“ื™ืงืช ื”-linkืื ื”ื•ื ืžื™ืฆื’

, ื•ืœื”ืžืฉื™ืš ืืช ืคืขื•ืœืช link ื‘-name, ืœื”ื—ืœื™ืฃ ืืช link ืœืžืฉืชื ื” nameื”ืงื•ื‘ืฅ .link ื”ื—ืœ ืžืชื—ื™ืœืช parsingื”-

. ื™ืฉ ืœืžื ื•ืข ืœื•ืœืื” link ืœ-link. ืžืจืฉื” ื”ืฆื‘ืขื•ืช ืฉืœ bื”ืžื™ืžื•ืฉ ืฉืœื ื• ื‘ืกืขื™ืฃ , ืœืžืฉืœ ืข"ื™ ืกืคื™ืจื” ืฉืœ ืžืกืคืจ ื”ืคืขืžื™ื ื‘ื”ืŸ parsingืื™ื ืกื•ืคื™ืช ื‘ืžื”ืœืš ืคืขื•ืœืช ื”-

ื•ื™ืฆื™ืื” ืขื ื”ื•ื“ืขืช ืฉื’ื™ืื” ืžืชืื™ืžื” ื‘ืžื™ื“ื” ื•ื”ืžื•ื ื” ืžื’ื™ืข linkื”ืชื”ืœื™ืš ื ืชืงืœ ื‘-ืœืขืจืš ืกืฃ ืฉื ืงื‘ืข ืžืจืืฉ. ืืคืฉืจื•ืช ื ื•ืกืคืช ื”ื™ื ืœืžื ื•ืข ืžืจืืฉ ื”ืฆื‘ืขื•ืช ื‘ื™ืŸ

ืœื™ื ืงื™ื.

.false ืœ-linkื›ืืฉืจ ื™ื•ืฆืจื™ื ืงื•ื‘ืฅ ืจื’ื™ืœ, ื™ืฉ ืœืืชื—ืœ ืืช ื“ื’ืœ

Page 19: Operating Systems ,  132

Question 2: FAT (2007a) ืชืžื™ื›ื” MS-DOS ื ืงื•ื“ื•ืช( ื›ืขืช ื ื•ืกื™ืฃ ืœืžืขืจื›ืช ื”ืงื‘ืฆื™ื ืฉืœ 12)ื‘.)ื”ื ื™ื—ื• ื›ื™ FATืžื‘ืœื™ ืœื‘ืฆืข ืฉื™ื ื•ื™ ื›ืœืฉื”ื•ื ื‘ื˜ื‘ืœืช ื”-, hard linksื‘-

ื ื™ืชืŸ ืœื”ื™ืขื–ืจ ื‘ืžื‘ื ื™ ื ืชื•ื ื™ื ื ื•ืกืคื™ื(.a ืชืืจื• ื‘ืคื™ืจื•ื˜ ืื™ืœื• ืžื‘ื ื™ ื ืชื•ื ื™ื ืฉืœ )MS-DOS ื™ืฉืชื ื• ื•ื›ื™ืฆื“, ื•ืื™ืœื•

ืžื‘ื ื™ ื ืชื•ื ื™ื ื™ืชื•ื•ืกืคื•.

. Unix ื‘-inodesื ื•ืกื™ืฃ ื˜ื‘ืœื” ื”ื“ื•ืžื” ืžื‘ื—ื™ื ื•ืช ืžืกื•ื™ืžื•ืช ืœื˜ื‘ืœืช ื”- directory. ืฉื™ื ื•ื™ ื ื•ืกืฃ ื”ื•ื ื‘ืžื‘ื ื” ืฉืœ dnodesื ืงืจื ืœื” ื˜ื‘ืœืช

entries ื›ืœ .directory entry-ื™ืฆื‘ื™ืข ืœื›ื ื™ืกื” ื‘ื˜ื‘ืœืช ื” dnodes ื‘ืžืงื•ื ืœื”ืฆื‘ื™ืข ืœื‘ืœื•ืง ื”ืจืืฉื•ืŸ ืฉืœ ื”ืงื•ื‘ืฅ. ื‘ื›ืœ ื›ื ื™ืกื” ื›ื–ื• ื™ื”ื™ื• ืฉื ื™

ืฉื“ื•ืช. ื”ืฉื“ื” ื”ืจืืฉื•ืŸ ืžื•ื ื” ืืช ืžืกืคืจ ื”ื”ืฆื‘ืขื•ืช ืœืจืฉื•ืžืช FATื”- ื•ื”ืฉื“ื” ื”ืฉื ื™ ืžืฆื‘ื™ืข ืœื‘ืœื•ืง ื”ืจืืฉื•ืŸ ืฉืœ ื”ืงื•ื‘ืฅ. dnodeื”-

.ื ืฉืืจ ืœืœื ืฉื™ื ื•ื™

Page 20: Operating Systems ,  132

Question 2: FAT (2007a)

Page 21: Operating Systems ,  132

Question 2: FAT (2007a)b) ื›ืชื‘ื• ืคืกืื•ื“ื•-ืงื•ื“ ื”ืžืชืืจ ืืœ ืคืขื•ืœืชื” ืฉืœ ืคืจื•ืฆื“ื•ืจื”hard-link(old-path, new-path) ,

.hard-linkื”ืžืžืžืฉืช ืืช ื”ืคืงื•ื“ื”

Procedure hard-link(old-path, new-path) } // get directory-name and entry-name Convert new-path to <dname, ename> // allocate a new directory entry in directory dname new-entry โ† allocate-new-entry(dname) new-entry.filename โ† ename // locate old-path dnote entry dnode-entry-num โ† locate-dnote-entry(old-path) // point from directory entry to dnode entry new-entry.dnode โ† dnode-entry-num // Increment links number dnodes[dnodes-entry-num].links++ {

Page 22: Operating Systems ,  132

Question 2: FAT (2007a)

c) ื”ืื ื ื“ืจืฉื™ื ืฉื™ื ื•ื™ื™ื ื ื•ืกืคื™ื ื‘ืงื•ื“ ืฉืœMS-DOS ?.ืื ื›ืŸ, ืฆื™ื™ื ื• ืื•ืชื ื‘ืงืฆืจื”

ืžื•ื‘ืŸ ื›ื™ ื›ืขืช ื›ืœ ื”ืคืจื•ืฆื“ื•ืจื•ืช ื”ืคื•ืขืœื•ืช ืขืœ ืงื‘ืฆื™ื ื ื“ืจืฉื•ืช ืœืขื‘ื•ื“ ืขื ืžื‘ื ื™ ื”ื ืชื•ื ื™ื ื”ื—ื“ืฉื™ื.

ื›ืืฉืจ ื ื•ืฆืจ ืงื•ื‘ืฅ ื—ื“ืฉ, ื™ืฉ ืœืืชื—ืœ ืืช ืžืกืคืจ . ื›ืืฉืจ ืžื•ื—ืงื™ื 1 ื”ื—ื“ืฉ ืœ-dnodeื”ืœื™ื ืงื™ื ืฉืœ ื”-

ืงื•ื‘ืฅ ื™ืฉ ืœื”ืคื—ื™ืช ืืช ืžืกืคืจ ื”ืœื™ื ืงื™ื ืฉืœ ืฉืœื• ื‘ืื—ื“. ืจืง ืื ืžืกืคืจ ื–ื” ืžืชืืคืก, dnodeื”-

ื™ืฉ ืœืžื—ื•ืง ืืช ื”ืงื•ื‘ืฅ ืขืฆืžื•.

Page 23: Operating Systems ,  132

Question 3 (Moed b, 2007).NFSื ืชื•ืŸ ืฉืจื˜ื•ื˜ ืฉืœ ื™ื™ืฉื•ื ืฉื›ื‘ืชื™ ืฉืœ ืœืงื•ื— ื•ืฉืจืช ื‘ืžืขืจื›ืช

Page 24: Operating Systems ,  132

Question 3 (Moed b, 2007)

.a ื”ื•ืกื™ืคื• ืœืฉืจื˜ื•ื˜cache ื’ื ืืฆืœ ื”ืฉืจืช ื•ื’ื ืืฆืœ ื”ืœืงื•ื—. ืฆื™ื™ื ื• ืื™ื–ื” ืฉื›ื‘ื” ืžืฉืชืžืฉืช

. ื ืžืงื• ื‘ืžื“ื•ื™ืง ืืช ื‘ื—ื™ืจืชื›ื ืœืžื™ืงื•ื cacheื‘-.cacheื”-

.b ืžื” ื”ื”ื‘ื“ืœ ื‘ืžืขืจื›ืชNFS -ื‘ื™ืŸ ื” cache ืฉืœ ื”ืœืงื•ื— ื•ื”ืฉืจืช? ื”ืกื‘ื™ืจื• ืืช ื”ื”ื‘ื“ืœ ื‘ื™ืชืจื•ื ื•ืช

ืืฆืœ ื”ืœืงื•ื— ื•ืืฆืœ cacheื•ื‘ื—ืกืจื•ื ื•ืช ื‘ืฉื™ืžื•ืฉ ื‘-ื”ืฉืจืช.

Page 25: Operating Systems ,  132

Question 3a (Moed b, 2007)Client kernel Server kernel

System call layer

Virtual file system layer

v-nodes

Local FS1

Local FS2

NFS server

Buffer cache

Message to server

Virtual file system layer

Local FS1

Local FS2

NFS server

Buffer cache

Message from client

i-nodes

r-nodes

25

Page 26: Operating Systems ,  132

Question 3b (Moed b, 2007)

ื•ืœื›ืŸ ืœื ื ืฉืžืจ ืฉื•ื ืžื™ื“ืข. statelessื”ืฉืจืช ื”ื™ื ื• โ€ขื›ืœ ื’ื™ืฉื” ืœืฉืจืช ื”ื™ื ื—ื“ืฉื” ื•ื”ืฉืจืช ื ื™ื’ืฉ

ืœืžืขืจื›ืช ื”ืงื‘ืฆื™ื ื”ืจื’ื™ืœื”. NFS ื•ืœื›ืŸ ื’ื ื‘ืงืฉื•ืช ื”-statelessื”ืœืงื•ื— ืื™ื ื• โ€ข

.cacheื™ื›ื•ืœื•ืช ืœืขื‘ื•ืจ ื“ืจืš ื”-

Page 27: Operating Systems ,  132

Question 4 (Moed b 2006)

ื‘ื”ื ื—ื” ืฉื‘ืชื—ื™ืœืช ื-ื”ืชื›ื ื™ืช ื”ืงื•ื‘ืฅ

dugma1.txt ,ื”ื•ื ืจื™ืง ืžื” ืชื•ื›ื ื• ืœืื—ืจ ืกื™ื•ื

ื”ืชื›ื ื™ืช? ื”ืกื‘ื™ืจื•.

ื”ื—ืœืฃ ืืช ื”ืฉื•ืจื” ื‘-ื”ืื“ื•ืžื” ืขืœ ืžื ืช

ืœืงื‘ืœ ืชื•ืฆืื” ืื—ืจืช ื•ื”ืกื‘ืจ ืืช ื”ืฉื•ื ื™, ื‘ื”ืชื™ื™ื—ืก ืœืžื™ืžื•ืฉ

ืžืขืจื›ืช ื”ืงื‘ืฆื™ื ืฉืœ UNIX .

ื ืชื•ื ื” ื”ืชื•ื›ื ื™ืช:

int main(char ** argv, int argc){ int fd=open("dugma1.txt",O_WRONLY,0666); if (fork()==0){

int fd2=dup(fd);sleep(10); write (fd2,"I was here second",17);

}//if else{

write (fd,"I was here first",16); }}

Page 28: Operating Systems ,  132

Question 4 (Moed b 2006)

1. I was here firstI was here second

2. int fd2=open (โ€œdugma1.txtโ€,O_WRONLY,0666);output is : I was here second

Why?

Page 29: Operating Systems ,  132

Open File Description Table &File Descriptor Table

Parentโ€™s file

descriptors table

Childโ€™s file descriptors

table

Unrelated processโ€™s

file descriptors

table

File positionRW

pointer to i-node

File positionRW

pointer to i-node

File positionRW

pointer to i-nodei-nodes table

Open files description table

29

Page 30: Operating Systems ,  132

Locking Files

โ€ข Two or more processes accessing a file together can cause race conditions.

โ€ข Ability to lock any number of bytes of a file.โ€ข Two kinds of locks shared locks, exclusive locksโ€ข C Function is int flock(int fd, int operation); โ€ข flock is an advisory lock

Page 31: Operating Systems ,  132

Locking Files

โ€ข flock doesnโ€™t work over NFSโ€ข Locking a whole file is wasteful.โ€ข lockf(int fd, int cmd, off_t len)

โ€“ Allows locking over NFS by implementing another protocol (Network Lock Manager)

โ€“ No shared lockingโ€“ Also an advisory lockโ€“ Commands: F_ULOCK (unlock), F_LOCK (lock

[blocking]), F_TLOCK (test & lock [non-blocking]), F_TEST (test)

Page 32: Operating Systems ,  132

Question 4 (Moed b 2006) [revised]int main(char ** argv, int argc){

int stat;int fd=open("dugma1.txt",O_WRONLY,0666);if (fork()==0){ int fd2=open("dugma1.txt",O_WRONLY,0666); sleep(10); if (lockf(fd2,F_TLOCK,17)>=0){ write (fd2,"I was here second",17); }}//ifelse{

lockf(fd,F_TLOCK,16); write (fd,"I was here first",16); wait(&stat);

}

}

ืฉื™ื ื• ืืช ื”ืงื•ื“.ืžื” ื™ื”ื™ื” ื”ืคืœื˜ ืขื›ืฉื™ื•?

ื”ืกื‘ืจ.

Page 33: Operating Systems ,  132

Question 4 (Moed b 2006)

)ื”ืฉืชืžืฉื ื• non-blockingื‘ืฉื•ืจื” ืฉื”ื•ืกืคื ื• ื”ื•ืกืคื ื• ืžื ืขื•ืœ (. ื”ืงื•ื‘ืฅ ื™ื›ื™ืœ ืฉื•ืจื” ืื—ืช ื‘ืœื‘ื“:F_TLOCKื‘ืืจื’ื•ืžื ื˜

I was here firstื”ืกื™ื‘ื” ืœื›ืš ื”ื™ื ืžื›ื™ื•ื•ืŸ ืฉืœื ืžืชื‘ืฆืขืช ืกื’ื™ืจื” ืฉืœ ื”ืงื•ื‘ืฅ. ืื ื ืฉื ื”

)ื›ืœื•ืžืจ ื›ืฉื‘ื•ื“ืงื™ื ืืช ื”ืžื ืขื•ืœ ื ืžืชื™ืŸ ืขื“ F_LOCKืืช ื”ืืจื’ื•ืžื ื˜ ืœ .deadlockืฉื™ืฉืชื—ืจืจ(, ื ืงื‘ืœ

ื ื™ืชืŸ ืœืคืชื•ืจ ื–ืืช ื‘ืื—ืช ืžืฉืชื™ ื“ืจื›ื™ื: )ืกื’ื ื•ืŸ ืจืข(waitืœื•ื•ืชืจ ืขืœ 1.

)ื‘ืื•ืคืŸ ื›ืœืœื™ ืชืžื™ื“ ื ื“ืื’ ืœืกื’ื•ืจ ืงื‘ืฆื™ื ืฉืคืชื—ื ื•!(.closeืœื”ื•ืกื™ืฃ 2.

ื‘ืžืงืจื” ื–ื” ืชื•ื“ืคืก ืฉื•ืจื” ืื—ืช ื•ื‘ื”:

I was here second? DUPื•ืžื” ื™ืงืจื” ื›ืฉื ืฉืชืžืฉ ื‘-

Page 34: Operating Systems ,  132

Question 5 (Moed a 2009)(.NFSื ืชื•ื ื” ืžืขืจื›ืช ืงื‘ืฆื™ื ืฉืœ ืœื™ื ื•ืงืก ืืฉืจ ืชื•ืžื›ืช ื‘ืฉื™ืชื•ืฃ ืงื‘ืฆื™ื )

. ื”ืกื‘ื™ืจื• i-node, v-node, r-nodeื. ื‘ืžืขืจื›ืช ืงื™ื™ืžื™ื ืฉืœื•ืฉื” ืกื•ื’ื™ ืžื‘ื ื™ ื ืชื•ื ื™ื: ืืช ืชืคืงื™ื“ื• ืฉืœ ื›ืœ ืื—ื“ ืžืžื‘ื ื™ ื”ื ืชื•ื ื™ื ื”ืœืœื• ื•ืืช ื”ืงืฉืจื™ื ื‘ื™ื ื™ื”ื .

ื‘.

ื ืชื•ืŸ ืงื˜ืข ื”ืงื•ื“ ื”ื‘ื.

int fd=open("data.txt",O_RDONLY,0666);lseek(fd,50,SEEK_SET); // Sets the offset to 50write(fd,buf,150); // Write 150 bytes from buflseek(fd,50,SEEK_SET); // Sets the offset to 50read(fd,buf,150); // Read 150 bytes into buf

ื™ื•ืฉื‘ ืขืœ ื’ื‘ื™ ืฉืจืช ื—ื™ืฆื•ื ื™ )ื•ืœื ื‘ืžืขืจื›ืช ื”ืงื‘ืฆื™ื data.txtื™ื“ื•ืข ืฉื”ืงื•ื‘ืฅ ื”ืžืงื•ืžื™ืช(.

ื”ืกื‘ื™ืจื• ืื™ืœื• ืžืŸ ื”ืคืงื•ื“ื•ืช ืฉื‘ืงื•ื“ ื ืฉืœื—ื•ืช ืœืฉืจืช ื•ืื™ืœื• ืœื. ื ืžืงื• ืชืฉื•ื‘ืชื›ื ื ืง'(.6ื‘ืงืฆืจื” )

Page 35: Operating Systems ,  132

Question 5 (Moed a 2009)ื.

i-nodeืžื‘ื ื” ื ืชื•ื ื™ื ื”ืžืชืืจ ืงื•ื‘ืฅ ื‘ืžืขืจื›ืช ื”ืงื‘ืฆื™ื ื”ืžืงื•ืžื™ืช :

r-node ืžื‘ื ื” ื ืชื•ื ื™ื ืืฉืจ ื ืžืฆื ืืฆืœ ื”ืงืœื™ื™ื ื˜ ื•ืžืชืืจ ืงื•ื‘ืฅ ื‘ืžืขืจื›ืช ืงื‘ืฆื™ื : ืžืจื•ื—ืงืช

v-node -ื ืžืฆื ื‘ืฉื›ื‘ืช ื” : VFS -ื•ืžืฆื‘ื™ืข ืื• ืœ i-node -ืื• ืœ r-node

. ื›ืœื•ืžืจ, ื”"ื“ื™ื‘ื•ืจ" ืขื ืงื‘ืฆื™ื v-node"ืขื•ื‘ื“ืช" ืจืง ืžื•ืœ System Call Layerื”- ื™ืชื‘ืฆืข ื‘ืื•ืชื” "ืฉืคื”" ื‘ืœื™ ืงืฉืจ ืื ื”ืงื•ื‘ืฅ ืžืงื•ืžื™ ืื• ืžืจื•ื—ืง )ื”ืžืžืฉืง ื”ืžื•ื—ืฆืŸ

ื™ืชืจื’ื v-node ืืœื™ื• ืžืฆื‘ื™ืข r-nodeืข"ื™ ืฉื›ื‘ื” ื–ื• ืื™ื ื• ืžืชื™ื™ื—ืก ืœืžื™ืงื•ื ื”ืงื‘ืฆื™ื(. ืืฉืจ i-nodeืืช ื”ืคืงื•ื“ื•ืช ืœื”ื•ื“ืขื•ืช ืขื‘ื•ืจ ืฉืจืช ืžืจื•ื—ืง ืฉืขืœื™ื• ื™ื•ืฉื‘ ื”ืงื•ื‘ืฅ. ื•ืื™ืœื•

ื™ืชืจื’ื ืืช ื”ืคืงื•ื“ื•ืช ืœื”ื•ื“ืขื•ืช ืœืžืขืจื›ืช ื”ืงื‘ืฆื™ื v-nodeืืœื™ื• ืžืฆื‘ื™ืข ืื™ื–ืฉื”ื• ื”ืžืงื•ืžื™ืช ืฉืœ ืžืขืจื›ืช ื”ื”ืคืขืœื”.

Page 36: Operating Systems ,  132

Question 5 (Moed a 2009)ื‘.

ืืฉืจ ืชื—ื–ื™ืจ ืœืงืœื™ื™ื ื˜ lookup ืœืฉืจืช, ืืœื” ืคืงื•ื“ื•ืช open ืœื ืชืฉืœื— ืคืงื•ื“ืช :1ืฉื•ืจื” filehandle -ืœืื—ืจ ืงื‘ืœืช ื” .filehandle ื”ืงืœื™ื™ื ื˜ ื™ื™ืฆืจ v-node ื• r-node -ื›ืืฉืจ ื”

v-node -ืžืฆื‘ื™ืข ืœr-node -ื•ื” r-node -ืžื—ื–ื™ืง ืืช ื” filehandle ื”ืชืงื‘ืœื• 1. ืœืฉื•ืจื” ื•ื’ื FD ื ืฉืœื— ื•ื”ืฉืจืช ืžื—ื–ื™ืจ openื’ื ื”ืชืฉื•ื‘ื•ืช ืฉื‘ื”ืŸ ื”ื ื‘ื—ืŸ ืจืฉื ืฉื”-

stateless ืœื ื ืฉืœื— ื‘ื’ืœืœ ืฉื”ืฉืจืช ื”ื•ื openื”ืชืฉื•ื‘ื•ืช ืฉื‘ื”ืŸ ื”ื ื‘ื—ืŸ ืจืฉื ืฉื”- ืฉืœ ื”ืงื•ื‘ืฅ ื ืžืฆื ืืฆืœ offset ื•ืœื›ืŸ ืžื™ื“ืข ื›ื’ื•ืŸ ื” stateless ื”ืฉืจืช ื”ื•ื :2ืฉื•ืจื”

ืชืžื™ื“ ืชืชื‘ืฆืข ื‘ืฆื“ ืฉืœ ื”ืงืœื™ื™ื ื˜ ื‘ืœื‘ื“.lseekื”ืงืœื™ื™ื ื˜. ื›ืœื•ืžืจ ืคืงื•ื“ืช

ื•ืœื›ืŸ ืžื™ื“ืข ื›ื’ื•ืŸ ื”ืจืฉืื•ืช ื›ืชื™ื‘ื”/ืงืจื™ืื” ืฉืœ ื”ืงื•ื‘ืฅ stateless ื”ืฉืจืช ื”ื•ื :3ืฉื•ืจื” ื”ืงืœื™ื™ื ื˜ ื™ื–ื”ื” ืฉืืกื•ืจ RD_ONLYื ืžืฆืื•ืช ืืฆืœ ื”ืงืœื™ื™ื ื˜. ืžื›ื™ื•ื•ืŸ ืฉื”ืงื•ื‘ืฅ ื ืคืชื— ื›-

ืœื ืชืฉืœื— ืœืฉืจืช.writeืœื‘ืฆืข ืคืขื•ืœืช ื›ืชื™ื‘ื” ืœืงื•ื‘ืฅ. ื•ืœื›ืŸ ืคืงื•ื“ืช ื”-

.2 ืจืื” ื”ืกื‘ืจ ืœืฉื•ืจื” :4ืฉื•ืจื”

ืžื›ื™ื•ื•ืŸ ืฉื”ืชื•ื›ืŸ ืฉืœ ื”ืงื•ื‘ืฅ ื™ื•ืฉื‘ ืืฆืœ ื”ืฉืจืช )ื•ืœื ืืฆืœ ื”ืงืœื™ื™ื ื˜( ืคืงื•ื“ืช ื”- :5ืฉื•ืจื” read ื”ื‘ื™ื˜ื™ื ืž- 150 ืชืฉืœื— ืœืฉืจืช ื•ืชื‘ืงืฉ ืืช offset 100.

Page 37: Operating Systems ,  132

Question 5 (Moed a 2009)ื’. ื ืชื•ืŸ ืงื˜ืข ื”ืงื•ื“ ื”ื‘ื.

int fd=open("data.txt",O_RDONLY,0666);lseek(fd,0,SEEK_SET); // Sets the offset to 0read(fd,buf,500);read(fd,buf,500);read(fd,buf,1000);

ื•ื”ืฉืจืช ืฉื•ืœื— ืœืงืœื™ื™ื ื˜ ื‘ืœื•ืงื™ื ื‘ื’ื•ื“ืœ NFS cachingื ืชื•ืŸ ื’ื ื›ื™ ืœืงืœื™ื™ื ื˜ ื™ืฉ ืฉื›ื‘ืช .KB 1ืงื‘ื•ืข ืฉืœ

ื”ืกื‘ื™ืจื• ืื™ืœื• ืžืŸ ื”ืคืงื•ื“ื•ืช ื‘ืงื•ื“ ืฉืœืžืขืœื” ื ืฉืœื—ื•ืช ืœืฉืจืช ื•ืื™ืœื• ืœื. ื ืžืงื• ื ืง'(.6ืชืฉื•ื‘ืชื›ื ื‘ืงืฆืจื” )

Page 38: Operating Systems ,  132

Question 5 (Moed a 2009)ื’.

ื™ื›ื•ืœื” ืœื”ื—ื–ื™ืจ ื™ื•ืชืจ ืžื™ื“ืข ืžืžื” ืฉื”ืงืœื™ื™ื ื˜ ื‘ื™ืงืฉ. ื›ืืฉืจ readื”ืขืจื”: ื‘ืฉืืœื” ื–ื• ืคืงื•ื“ืช ื”- ื”ืžื•ื˜ื™ื‘ืฆื™ื” ืžืื—ื•ืจื™ ืงื‘ืœืช ืžื™ื“ืข "ื ื•ืกืฃ", ื”ื™ื ืฉืกื‘ื™ืจ ืœื”ื ื™ื— ืฉื‘ืื™ื–ืฉื”ื• ืฉืœื‘ ื”ืงืœื™ื™ื ื˜

ืืฉืจ ื‘ื” ื™ืื•ื—ืกืŸ ื”ืžื™ื“ืข ื™ื›ื•ืœื” cacheื™ืจืฆื” ืœืงืจื•ื ืืช ื”ืžื™ื“ืข ื”ื ื•ืกืฃ ื”ื–ื”. ืฉื›ื‘ืช ื”- ืœื—ืกื•ืš ื’ื™ืฉื•ืช ืœืฉืจืช.

ื‘ืกืขื™ืฃ ื‘'(1)ืจืื” ื”ืกื‘ืจ ืœืฉื•ืจื” : 1ืฉื•ืจื”

ื‘ืกืขื™ืฃ ื‘'(2)ืจืื” ื”ืกื‘ืจ ืœืฉื•ืจื” : 2ืฉื•ืจื”

ืจื™ืง ื•ืœื›ืŸ ืขืœื™ื• ืœืงื‘ืœ ืืช ืชื•ื›ืŸ ื”ืงื•ื‘ืฅ ืžื”ืฉืจืช. ืคืงื•ื“ืช cacheื”ืงืœื™ื™ื ื˜ ื™ืจืื” ืฉื”- : 3ืฉื•ืจื” ืืคืก. ืžื›ื™ื•ื•ืŸ ืฉื ืชื•ืŸ ืฉื”ืฉืจืช offset ื”ื‘ื™ื˜ื™ื ืž- 500 ืชืฉืœื— ืœืฉืจืช ื•ืชื‘ืงืฉ ืืช readื”-

1024 ืขื“ 0 ื”ืงืœื™ื™ื ื˜ ื™ืงื‘ืœ ื—ื–ืจื” ืืช ืชื•ื›ืŸ ื”ืงื•ื‘ืฅ ืž- KB 1ืžื—ื–ื™ืจ ืชืฉื•ื‘ื•ืช ื‘ื‘ืœื•ืงื™ื ืฉืœ cacheื‘ื™ื™ื˜ ื•ื™ืื›ืกืŸ ืืช ื”ืžื™ื“ืข ื‘-

ื‘ื™ื™ื˜. ื”ืคืขื ื”ืงืœื™ื™ื ื˜ ื™ื’ืœื” 1000 ืขื“ 500ื›ืขืช ื”ืงืœื™ื™ื ื˜ ืจื•ืฆื” ืืช ืชื•ื›ืŸ ื”ืงื•ื‘ืฅ ืž- : 4ืฉื•ืจื” ืœื ืชื™ืฉืœื— ืœืฉืจืช, ืืœื” read ื•ืœื›ืŸ ืคืงื•ื“ืช ื”- cacheืฉื”ืžื™ื“ืข ืฉื”ื•ื ืฆืจื™ืš ื™ื•ืฉื‘ ื‘-

cacheืชื™ืœืงื— ื”ืžื™ื“ืข ื™ืฉื™ืจื•ืช ืžื”-

ื™ืฉ cache ื‘ื™ื™ื˜. ืื•ืœื ืœ- 2000 ืขื“ 1000ื›ืขืช ื”ืงืœื™ื™ื ื˜ ืจื•ืฆื” ืืช ืชื•ื›ืŸ ื”ืงื•ื‘ืฅ ืž โ€“ : 5ืฉื•ืจื” ืชื™ืฉืœื— ืœืฉืจืช.read ื•ืœื›ืŸ ื”ืคืขื ืคืงื•ื“ืช ื”- 1024ืจืง ืืช ื”ืžื™ื“ืข ืขื“ ื”ื‘ื™ื™ื˜ ื”-

Page 39: Operating Systems ,  132

Question 5 (Moed a 2009) ืœื›ืชื™ื‘ื” data.txtื“. ื ืชื•ื ื™ื ืฉื ื™ ืชื”ืœื™ื›ื™ื. ื”ืชื”ืœื™ืš ื”ืจืืฉื•ืŸ ืคื•ืชื— ืืช ื”ืงื•ื‘ืฅ

ื•ืœืงืจื™ืื”. ื”ืชื”ืœื™ืš ื”ืฉื ื™ ืžื ืกื” ืœืžื—ื•ืง ืืช ื”ืงื•ื‘ืฅ ื‘ืขื•ื“ื• ืคืชื•ื— ืข"ื™ ื”ืชื”ืœื™ืš ื”ืจืืฉื•ืŸ.

. ื‘ื”ื ื—ื” ืฉืฉื ื™ ื”ืชื”ืœื™ื›ื™ื ืจืฆื™ื ืขืœ ืžื—ืฉื‘ื™ื ืฉื•ื ื™ื )ืฉื ื™ ืงืœื™ื™ื ื˜ื™ื ืฉื•ื ื™ื ืฉืœ 1ื”ืฉืจืช ื”ื—ื™ืฆื•ื ื™(. ื”ืื ื”ืžื—ื™ืงื” ืชืฆืœื™ื—? ื ืžืงื• ื‘ืงืฆืจื”.

. ื‘ื”ื ื—ื” ืฉืฉื ื™ ื”ืชื”ืœื™ื›ื™ื ืจืฆื™ื ืขืœ ืื•ืชื• ืžื—ืฉื‘ )ืงืœื™ื™ื ื˜ ืฉืœ ื”ืฉืจืช ื”ื—ื™ืฆื•ื ื™(. 2ื”ืื ื”ืžื—ื™ืงื” ืชืฆืœื™ื—? ื ืžืงื• ื‘ืงืฆืจื”

Page 40: Operating Systems ,  132

Question 5 (Moed a 2009)ื“.

ื•ืœื›ืŸ ืื™ืŸ ืœื• ืฉื•ื ืžื™ื“ืข ืœื’ื‘ื™ ืื™ื–ื” ืชื”ืœื™ื›ื™ื ืคืชื—ื• ืืช statelessื”ืฉืจืช ื”ื•ื 1.. ื‘ื”ื ื—ื” ืฉื™ืฉ ืœืงืœื™ื™ื ื˜ ื”ืจืฉืื•ืช ืžืชืื™ืžื•ืช, ื”ืžื—ื™ืงื” data.txtื”ืงื•ื‘ืฅ ืชืฆืœื™ื—.

ืžื›ื™ื•ื•ืŸ ืฉืฉื ื™ ื”ืชื”ืœื™ื›ื™ื ืจืฆื™ื ืขืœ ืื•ืชื” ืžืขืจื›ืช ื”ืคืขืœื” ื‘ืื•ืชื• ืžื—ืฉื‘, 2.ืžืขืจื›ืช ื”ื”ืคืขืœื” ืฉืœ ื”ืงืœื™ื™ื ื˜ ื™ื›ื•ืœื” ืœื–ื”ื•ืช ืฉื”ืงื•ื‘ืฅ ืคืชื•ื— )ื”ืงืœื™ื™ื ื˜ ื”ื•ื

statefull ื”ืื ื”ืžื—ื™ืงื” ืชืฆืœื™ื—? ืชืœื•ื™ ื‘ืžืขืจื›ืช ื”ื”ืคืขืœื”. ื ื‘ื—ืŸ ืฉื”ื‘ื™ืŸ )ืฉื”ืคืขื ื”ืžื—ื™ืงื” ืชืœื•ื™ื” ื‘ืงืœื™ื™ื ื˜ )ื•ืœื ื‘ืฉืจืช( ื•ืกื˜ื•ื“ื ื˜ ืฉื ืชืŸ ื”ืกื‘ืจ

ืžืกืคืง, ืงื™ื‘ืœ ืืช ืžืœื•ื ื”ื ืงื•ื“ื•ืช.