Top Banner
Operating Systems, 142 Practical Session 11 File Systems & Midterm 2014 1
39

Operating Systems, 142

Jan 01, 2016

Download

Documents

tara-nelson

Operating Systems, 142. Practical Session 11 File Systems & Midterm 2014. Quick recap. Files are an abstraction mechanism Several file types: User files (regular),Directory files, Special files (Block, Char) Access: sequentially (e.g. tapes) or random access (disk) - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Operating Systems, 142

Operating Systems, 142

Practical Session 11 File Systems & Midterm 2014

1

Page 2: Operating Systems, 142

Quick recap

• Files are an abstraction mechanism• Several file types: User files (regular),Directory

files, Special files (Block, Char)• Access: sequentially (e.g. tapes) or random

access (disk)• Data: structured (records) or unstructured (set

of bits and bytes)

2

Page 3: Operating Systems, 142

File system layout (Tanenbaum)

3

Page 4: Operating Systems, 142

Quick recap: Index-Nodes (i-nodes)

• The superblock object represents the entire file system.

• Each i-node is a data structure containing pointers to the disk blocks that contain the actual file contents.

• An i-node corresponds to a single file.• An i-node needs to be in the main memory only if

the correspondent file is open.• Besides the data blocks pointers, the i-node also

contains information on the file permissions, owner, etc

4

Page 5: Operating Systems, 142

Quick recap: i-Nodes

General file attributes

The number of hard-links to the file

Usually between 10

and 12

File Size

HardLink count

5

Page 6: Operating Systems, 142

Question 1: i-nodesHow many time will the disk be accessed when a user executes the following command:more /usr/tmp/a.txt

Assume that: 1. The size of 'a.txt' is 1 block. 2. The i-node of the root directory is not in the

memory. 3. Entries 'usr', 'tmp' and 'a.txt' are all located in

the first block of their directories.6

Page 7: Operating Systems, 142

Question 1: i-nodes

Accessing each directory requires at least 2 disk accesses: reading the i-node and the first block. In our case the entry we are looking for is always in the first block so we need exactly 2 disk accesses. According to assumption 2 the root directory's i-node is located on the disk so we need 6 disk accesses (3 directories) until we reach a.txt's i-node index. Since "more" displays the file's content, for a.txt we need its i-node + all the blocks of the file (1 block, according to assumption). Total disk accesses: 6 + 2 = 8.

7

Page 8: Operating Systems, 142

Question 1: i-nodesA similar problem

8

Page 9: Operating Systems, 142

Question 2: i-nodes

The Ofer2000 Operating Systems, based on UNIX, provides the following system call:rename(char *old, char *new)This call changes a file’s name from ‘old’ to ‘new’. What is the difference between using this call, and just copying ‘old’ to a new file, ‘new’, followed by deleting ‘old’? Answer in terms of disk access and allocation.

9

Page 10: Operating Systems, 142

Question 2: i-nodes

• rename - simply changes the file name in the entry of its directory.

• copy - will allocate a new i-node and the blocks for the new file, and copy the contents of the old file blocks to the new ones.

• delete - will release the i-node and blocks of the old file. • copy + delete - is a much more complicated operation for

the Operating System, note that you will not be able to execute it if you do not have enough free blocks or i-nodes left on your disk.

10

Page 11: Operating Systems, 142

Question 3: i-nodes

Write an implementation (pseudo code) of the system call: delete(i-node node)

Which deletes the file associated with node.Assume that: • node is associated with a regular file, and that delete is not

recursive. • The i-node has 10 direct block entries, 1 single indirect entry

and 1 double indirect entry. • You may use the system calls:

read_block(block b) which reads block b from the disk.free_block(block b) and free_i-node(i-node node).

11

Page 12: Operating Systems, 142

Question 3: i-nodesdelete(i-node node){

// remove the direct blocksfor each block b in node.direct do

free_block(b); // remove the single indirect blockssingle <-- read_block(node.single_indirect) for each entry e in single do

free_block(e); free_block(single); // remove the double indirect blocksdouble <-- read_block(node.double_indirect) for each entry e in double do

single <-- read_block(e) for each entry ee in single do

free_block(ee); free_block(single);

free_block(double); // remove the i-nodefree_i-node(node);

} 12

Page 13: Operating Systems, 142

Question 4: i-nodes

What would be the maximal size of a file in a UNIX system with an address size of 32 bits if :

1. The block size is 1K2. The block size is 4K

(The i-node has 10 direct block entries, one single, double & triple indirect)

13

Page 14: Operating Systems, 142

Question 4: i-nodes

1. Block size: 1K– Direct: 10·1K– Single indirect: each address is 32 bit = 4 byte then

we have 256 pointers to blocks of size 1K (i.e. 256·1K)

– The same idea is applied for double and triple indirect.

In total: 10·1K+256·1K+256·256·1K+256·256·256·1K

14

Page 15: Operating Systems, 142

Question 4: i-nodes

1. Block size: 4K– Direct: 10·4K– Single indirect: each address is 32 bit = 4 byte then

we have 1024 pointers to blocks of size 4K (i.e. 1024·4K)

– The same idea is applied for double and triple indirect

In total: 10·4K+1024·4K+1024·1024·4K+1024·1024·1024·4K

15

Page 16: Operating Systems, 142

Question 5: i-nodes

Assuming that the size of each block is 1K and the address size is 32 bits (4 bytes). Convert byte address (offset) 1,515,000 in our file to the physical address.

16

Page 17: Operating Systems, 142

Question 5: I-Nodes

Byte number 1,515,000 is calculated as follows:– 1st byte of the double indirect block is 10k+256k =

272,384– byte number 1,515,000 is number 1,242,616 in the

double indirect block– every single indirect block has 256k bytes --> byte

1,242,616 is in the 5th single indirect block (4*256k = 1,048,576)

– Every entry is 1k, so byte 194,040 is in the 189th block – assume that it points to block 123 on the disk

– within block 123 , it is byte #50417

Page 18: Operating Systems, 142

18

They could say "the connection is probably lost," but it's more fun to do naive time-averaging to give you hope that if you wait around for 1,163 hours, it will finally finish.

ESTIMATION

Page 19: Operating Systems, 142

Operating SystemsMIDTERM 2014

Page 20: Operating Systems, 142

Question 1

(35 ) הדדית מניעה נקודות(mutual exclusion)

20

Page 21: Operating Systems, 142

Question 1( לשני תהליכים המשתמש בשני ביטים. כל תהליך כותב mutual exclusionנתון אלגוריתם למניעה הדדית )•

והקוד המופיע למטה 1 או 0. התהליכים מסומנים 0וקורא את שני הביטים. שני הביטים מאותחלים לערך לפי התהליך המריץ את הקוד. 1 או 0 שמקבל את הערך iכתוב עבור תהליך

 1 start: x := i;2 if y ≠ 0 then 3 await y = 0;4 goto start;5 y := 1;6 if x ≠ i then 7 y := 0;8 await x = 0;9 goto start;

Critical Sectiony := 0;x := 0;

(. נק'15 ) שורות10בלא יותר מ- האם האלגוריתם מקיים מניעה הדדית? הוכיחו או הפריכו א.

(. נק'10 ) שורות10בלא יותר מ- ? הוכיחו או הפריכו deadlock האם האלגוריתם מונע ב. ( נק'10 האם תתכן הרעבה ? )ג.

21

Page 22: Operating Systems, 142

Question 1בלא א. האם האלגוריתם מקיים מניעה הדדית? הוכיחו או הפריכו

(. נק'15 ) שורות10יותר מ-

22

פתרון:: הדדית מניעה מקיים אינו האלגוריתם

שתהליך שורה 1נניח את תהליך 1עובר ואחריו . 0ראשון - ש היא . x = 0התוצאה

שורות את לסירוגין עוברים התהליכים - 2-4שני ש, עדיין yכיוון. 0שווה

תהליך שורה 0כעת את ראשון הקוד 5עובר לקטע ונכנסתהליך. ( xשערך ) 1הקריטי לשורות נכנס לו מתאים , 7-9אינו

- ( , ש כיוון הקוד כל את מבצע הראשונה לשורה ואז( y = 0חוזר. הוא גם נכנס

Page 23: Operating Systems, 142

Question 1

בלא ? הוכיחו או הפריכו deadlock האם האלגוריתם מונע ב.(. נק'10 ) שורות10יותר מ-

23

פתרון:מקיים אינו . : deadlock-freedomהאלגוריתם

deadlock תהליך שבו בתסריט לקרות פעמים 0עשוי של חסום לא מספר מבצעשורות שורות 1ותהליך 1-4את את פעמים של חסום לא מספר . 1-9מבצע

: הוא שנדרש כלשתהליך. שורה 1א את שתהליך 5יעבור שורה 0לפני את .2מבצעשתהליך. שורה 0ב את שתהליך 1יבצע שורה 1לפני את . 6מבצע

, תהליך דינאמי לתהליך 0בתאור את ) 1מאפשר מאפס הוא כי להתחלה לחזורx )תהליך את 1ואילו שתהליך yמאפס אחרי שורה 0רק את במלים. 2עבר

תהליך לשורות 0אחרות יגיע לא פעם .5-9אף

Page 24: Operating Systems, 142

Question 1

( נק'10האם תתכן הרעבה ? )ג.

24

פתרון: . חופש מקיים אינו שהאלגוריתם מכך ישירות נובע הרעבה תתכן

מקיפאון.

Page 25: Operating Systems, 142

Question 2

(40 ) XV6נקודות

25

Page 26: Operating Systems, 142

Question 2XV6מערכת התרגילים המעשיים – נק'( 40)

:spinlock.cלפניכם קוד מתוך 1469 // Acquire the lock.1470 // 1471 // 1472 // 1473 void1474 acquire(struct spinlock *lk)1475 {1476 pushcli();1477 if(holding(lk))1478 panic("acquire");1479 1480 // The xchg is atomic.1481 // It also serializes, so that reads after acquire are 1482 // not reordered before it.1483 while(xchg(&lk->locked, 1) != 0)1484 ;1485 1486 // Record info about lock acquisition for debugging.1487 lk->cpu = cpu;1488 getcallerpcs(&lk, lk->pcs);1489 }

26

Page 27: Operating Systems, 142

Question 2

27

שורות את מטרת הקוד. הדגימו מקרים של בעיות סנכרון 2-3הסבירו ב- א. . בדוגמאות הסבירו מדוע יש צורך לבצע 1483 ו- 1476שדורשות את השורות

( נק'10את השורות האלה. )

פתרון:" spinlockה- להשיג מ ע משותפים מבנים של הגנה לצורך בצורה MEנועד

- ה. של המימוש את טוב יותר להבין כדי בין spinlockבטוחה מקרים 2נבחיןשל הפרה להתבצע יכולה :MEבהם

• – \ בעיה לפתור כדי נעילה ביצע אשר מזה שונה מעבד ליבה על קוד ריצתאטומית בפעולה שימוש קיים (.1483שורה ) xchgזו

בעזרת ) • אסינכרוני מעבר המעבד אותו על אחר לקוד – interruptמעברביצוע(. I/O interruptלדוגמא היא מכך הנובעת אפשרית על acquireבעיה

.) ( " כדי אחרות דוגמאות לתת כמובן אפשר המעבד אותו י ע המנעול אותובביטול שימוש קיים כזה מצב ).1476שורה ) interruptsלמנוע

Page 28: Operating Systems, 142

Question 2 kernelב. ע"מ לשפר את ביצועי מערכת ההפעלה היינו מעוניינים כי ה-

בלבד, האם kernel נועד לצרכי spinlockיהיה יעיל ככל הניתן. מאחר ו- במקרים הבאים:spinlockניתן לשפר את ביצועי ה-

רץ על מעבד יחיד )עם ליבה יחידה(kernelבמערכת בה ה- •.non preemptive scheduler משתמש ב- kernelבמערכת בה ה- •

עבור כל אחד מהמקרים הציעו שיפורים )הורדה / שינוי של הקוד( במידה ושיפור אפשרי, במידה ולא, הסבירו מדוע. נמקו את תשובתכם

נק'(. 7 שורות )2-3ב-

28

Page 29: Operating Systems, 142

Question 2

29

פתרון:: הקודם בסעיף נותחו אשר למקרים בהתאם

• - ה , kernelכאשר ) ניתן ) יחיד מעבד ליבה עם מחשב על רץשורות את את. 1519ו 1483לבטל לבטל כמובן אפשר

למעבד ) הקשורות את( 1508, 1487השורות holdingולתקןבהתאם.

שקיימים • מכיוון הזו במערכת שינויים לבצע ניתן interruptsלא. התהליכים לתזמון קשורים לא אשר

Page 30: Operating Systems, 142

Question 2 handlers כל ה- exec, לאחר ביצוע signalsבמערכת הפעלה התומכת ב- ג.

(. נק'8. הסבירו מדוע )default handlersחוזרים להיות ה-

30

פתרון:ביצוע , exec system callבמהלך מתחלף התהליך של הזיכרון כל

ה גם ה. code segmentבפרט יוחזרו signal handlersאם לא ) אזי ) הפונקציות מצביעי של שינויים מחדל ברירת להיות

לא אשר בזיכרון מקומות על להצביע יכולים הישנים המצביעיםל ) יצביעו לא אחרות במילים קוד מכילים לא או לתהליך שייכים

signal handlers .) הישנים

Page 31: Operating Systems, 142

Question 2 . בשתי שורות default signal handlersתארו בשתי שורות בעיה במימוש של ד.

(. נק'8נוספות תארו כיצד התגברתם על הבעיה במימוש שלכם בעבודה )

31

פתרון: - ש היא ב signal handlerהבעיה ידוע ולא משתמש קוד הוא

kernel .) לפונקציה ) מצביע שלו ההתחלה כתובת מהשל הגדרה הוא השנייה העבודה במימוש אפשרי entryפתרון

point - ה ) של ההגדרות שינוי ידי על אשר( linkerחדש לתוכנית - " ה" את של mainעוטף רישום . signal handlersומבצע

בין • בבעיה שדנו תשובות גם - user spaceהתקבלו אפשרי kernel spaceל פתרון והסבירו - ל) - defaultקריאה ה מתוך - kernelישירות ל יצירת non defaultוקריאה על frameבאמצעות

- ה של (.userמחסנית

Page 32: Operating Systems, 142

Question 2

אשר signal handler הניח 2מימוש הסיגנלים שהוגדר בעבודה ה. לא מקבל ארגומנטים. סטודנט חרוץ ניסה לקרב את המימוש של

על ידי שינוי החתימה של ה- linux למימוש ב- xv6הסיגנלים ב- signal handler -במימוש שלו ה .handler קיבל ארגומנט יחיד

המייצג את מספר הסיגנל. הסבירו בפירוט אלו שינויים במנגנון 7) הסטודנט ביצע כדי שהמנגנון יעבוד signal handlerהפעלת ה-

(.נק'

32

Page 33: Operating Systems, 142

Question 2

33

פתרון:• - ל הארגומנט - signal handlerהעברת ה- למחסנית הכנסתו

user.• - ל ארגומנט בהעברת הבעיתי הוא signal handlerהחלק

( " לפי " מהמחסנית ייצא שהארגומנט לכך לגרום דרך במציאת .) כך, על אחראי לפונקציה קריאה שמבצע מי הקונבנציה

- ה הכנת בזמן להכניס handlerלהרצת frameכלומר צריכיםניקוי לבצע תדע אשר פונקציה כתובת חזרה כתובת בתור

.) עצמו ) לארגומנט בנוסף ארגומנט• - ב גם להיות יכולה - kernelהפונקציה ל. לחזרה kernelנגרום

- ה ריצת - handlerבסיום מה הארגומנט ניקוי את ונבצעkernel.

Page 34: Operating Systems, 142

Question 3

(25 ) system callsנקודות

34

Page 35: Operating Systems, 142

Question 3:נתונה התוכנית הבאה

35

1. #include <stdio.h>2. #include <pthread.h>3. #include <unistd.h>4. #include <sys/types.h>5. #include <signal.h>6. void thread_action)( {7. sleep)10(;8. pid_t pid = getpid)(;9. pthread_t tid = pthread_self)(;10. kill)pid, SIGINT(;11. printf)"Pid = %u, Tid = %u\n",)uint(pid,)uint(tid(;12. }

13. void sigint_handler)( {14. printf)"Received SIGINT\n"(;15. }

16. int main)( {17. signal)SIGINT, sigint_handler(;18. pthread_t thread1, thread2;19. pthread_create)&thread1, NULL, )void *( &thread_action, NULL(;20. fork)(;21. pthread_create)&thread1, NULL, )void *( &thread_action, NULL(;22. pid_t pid = getpid)(;23. printf)"Pid = %d Finished run!\n",pid(;24. pthread_exit)NULL(;25. }

Page 36: Operating Systems, 142

Question 3(. ציין את הפלטים האפשריים של התוכנית והסבר במדויק ובקצרה נק'15)א.

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

(.main הראשי לבצע את כל הפקודות בthreadה-

36

Page 37: Operating Systems, 142

Question 3

37

פתרון:

SIGINT עבור הסיגנל signal_handlerנרשמת הפונ' •(p1 עבור התהליך )נסמנו t1 ראשון threadנוצר • הנוכחי מכיוון p2 אינו קיים בp1 של thread t1 )הp2נוצר תהליך חדש נסמנו •

(posixים בthread אינו משכפל forkשp2 עבור t1 חדש thread וp1 עבור t2 חדש threadנוצר • )הסדר לא משנה(sleepים נכנסים למצב threadכל ה••p1ו p2מדפיסים את ההודעה שלהם ”pid = 2, tid = 1 ולאחר מכן “received sigint יודפס בהכרח p2כעת עבור • p1עבור •

או שלא יודפס כלום •” pid = 1,tid = 1 | 2 ולאחריו “recived sigintאו שיודפס • received sigintאו רק • ההדפסות בלי יציאת התוכנית 2 ולאחר מכן recived sigintאו שיודפס •

הינו חד פעמי signalים מכיוון שרישום הפונ ע"י threadהדבר תלוי בתזמון של הים thread הסיגנלים התקבלו באותו הזמן )לפני חזרה של אחר מה2אך אם

ים יאסוף את הסיגנל מכיוון שסיגנלים אינם threadלמצב משתמש( רק אחד מהמצטברים.

אינו מובטח p2 וp1הסדר בין הטיפול ב•

Page 38: Operating Systems, 142

Question 3

הינו סינכרוני או 10( האם הסיגנל שנשלח בשורה נק'5ב. )א-סינכרוני? תן דוגמא לקריאה לסיגנל מהסוג השני. )כלומר, אם

ענית שהסיגנל הוא א-סינכרוני תן דוגמא לסיגנל סינכרוני, ולהיפך(.

38

:פתרון

הינה קריאת מערכת killהטיפול הינו סנכרוני מכיוון שהקריאה ל• ובו מסומן על התהליך שקרא kernelאפשר מעבירה אותנו למצב

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

על מנת לשלוח killדוגמא לטיפול לא סינכרוני הוא שימוש ב•סיגנל לתהליך אחר.

Page 39: Operating Systems, 142

Question 3

( איך ישתנה הפלט אם נוריד את השורה האחרונה: נק'5)ג. pthread_exit(NULL)?

39

פתרון:את נוריד ' threadה pthread_exitאם הפונ את יסיים mainהראשי

לפונקציה מפורשת בלתי לקריאה שיגרום והתהליך)( exitמהאו שלהם ההודעות את להדפיס לחוטים לאפשר מבלי יסגר

. סיגנל לשלוח