operating systems, 122

37
Operating Systems, 122 Practical Session 12 File Systems, part 2 1

Upload: tarika

Post on 05-Jan-2016

25 views

Category:

Documents


0 download

DESCRIPTION

Operating Systems, 122. 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

Page 1: Operating Systems, 122

Operating Systems, 122

Practical Session 12 File Systems, part 2

1

Page 2: Operating Systems, 122

File system layout (Tanenbaum)

2

Page 3: Operating Systems, 122

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, 122

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, 122

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, 122

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, 122

MFT records

Page 8: Operating Systems, 122

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, 122

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, 122

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 11: Operating Systems, 122

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 12: Operating Systems, 122

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 13: Operating Systems, 122

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 14: Operating Systems, 122

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 15: Operating Systems, 122

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 16: Operating Systems, 122

Question 2: FAT (2007a) תמיכה MS-DOS נקודות( כעת נוסיף למערכת הקבצים של 12)ב.)הניחו כי FATמבלי לבצע שינוי כלשהוא בטבלת ה-, hard linksב-

ניתן להיעזר במבני נתונים נוספים(.a תארו בפירוט אילו מבני נתונים של )MS-DOS ישתנו וכיצד, ואילו

מבני נתונים יתווספו.

. Unix ב-inodesנוסיף טבלה הדומה מבחינות מסוימות לטבלת ה- directory. שינוי נוסף הוא במבנה של dnodesנקרא לה טבלת

entries כל .directory entry-יצביע לכניסה בטבלת ה dnodes במקום להצביע לבלוק הראשון של הקובץ. בכל כניסה כזו יהיו שני

שדות. השדה הראשון מונה את מספר ההצבעות לרשומת FATה- והשדה השני מצביע לבלוק הראשון של הקובץ. dnodeה-

.נשאר ללא שינוי

Page 17: Operating Systems, 122

Question 2: FAT (2007a)

Page 18: Operating Systems, 122

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 19: Operating Systems, 122

Question 2: FAT (2007a)

c) האם נדרשים שינויים נוספים בקוד שלMS-DOS ?.אם כן, ציינו אותם בקצרה

מובן כי כעת כל הפרוצדורות הפועלות על קבצים נדרשות לעבוד עם מבני הנתונים החדשים.

כאשר נוצר קובץ חדש, יש לאתחל את מספר . כאשר מוחקים 1 החדש ל-dnodeהלינקים של ה-

קובץ יש להפחית את מספר הלינקים של שלו באחד. רק אם מספר זה מתאפס, dnodeה-

יש למחוק את הקובץ עצמו.

Page 20: Operating Systems, 122

Question 3 (Moed b, 2007).NFSנתון שרטוט של יישום שכבתי של לקוח ושרת במערכת

Page 21: Operating Systems, 122

Question 3 (Moed b, 2007)

.a הוסיפו לשרטוטcache גם אצל השרת וגם אצל הלקוח. ציינו איזה שכבה משתמשת

. נמקו במדויק את בחירתכם למיקום cacheב-.cacheה-

.b מה ההבדל במערכתNFS -בין ה cache של הלקוח והשרת? הסבירו את ההבדל ביתרונות

אצל הלקוח ואצל cacheובחסרונות בשימוש ב-השרת.

Page 22: Operating Systems, 122

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

22

Page 23: Operating Systems, 122

Question 3b (Moed b, 2007)

ולכן לא נשמר שום מידע. statelessהשרת הינו •כל גישה לשרת היא חדשה והשרת ניגש

למערכת הקבצים הרגילה. NFS ולכן גם בקשות ה-statelessהלקוח אינו •

.cacheיכולות לעבור דרך ה-

Page 24: Operating Systems, 122

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 25: Operating Systems, 122

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 26: Operating Systems, 122

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

26

Page 27: Operating Systems, 122

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 28: Operating Systems, 122

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 29: Operating Systems, 122

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 30: Operating Systems, 122

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 31: Operating Systems, 122

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 32: Operating Systems, 122

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 33: Operating Systems, 122

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 34: Operating Systems, 122

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 35: Operating Systems, 122

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 36: Operating Systems, 122

Question 5 (Moed a 2009) לכתיבה data.txtד. נתונים שני תהליכים. התהליך הראשון פותח את הקובץ

ולקריאה. התהליך השני מנסה למחוק את הקובץ בעודו פתוח ע"י התהליך הראשון.

. בהנחה ששני התהליכים רצים על מחשבים שונים )שני קליינטים שונים של 1השרת החיצוני(. האם המחיקה תצליח? נמקו בקצרה.

. בהנחה ששני התהליכים רצים על אותו מחשב )קליינט של השרת החיצוני(. 2האם המחיקה תצליח? נמקו בקצרה

Page 37: Operating Systems, 122

Question 5 (Moed a 2009)ד.

ולכן אין לו שום מידע לגבי איזה תהליכים פתחו את statelessהשרת הוא 1.. בהנחה שיש לקליינט הרשאות מתאימות, המחיקה data.txtהקובץ תצליח.

מכיוון ששני התהליכים רצים על אותה מערכת הפעלה באותו מחשב, 2.מערכת ההפעלה של הקליינט יכולה לזהות שהקובץ פתוח )הקליינט הוא

statefull האם המחיקה תצליח? תלוי במערכת ההפעלה. נבחן שהבין )שהפעם המחיקה תלויה בקליינט )ולא בשרת( וסטודנט שנתן הסבר

מספק, קיבל את מלוא הנקודות.