operating systems, 122

42
Operating Systems, 122 Practical Session 11 File Systems & Midterm 2012 1

Upload: burke-sanford

Post on 31-Dec-2015

27 views

Category:

Documents


2 download

DESCRIPTION

Operating Systems, 122. Practical Session 11 File Systems & Midterm 2012. 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

TRANSCRIPT

Page 1: Operating Systems, 122

Operating Systems, 122

Practical Session 11 File Systems & Midterm 2012

1

Page 2: Operating Systems, 122

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

File system layout (Tanenbaum)

3

Page 4: Operating Systems, 122

Quick recap: Index-Nodes (i-nodes)

• The superblock object represents the entire file system and provides access to index-nodes.

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

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

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

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

Question 1: i-nodesA similar problem

8

Page 9: Operating Systems, 122

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

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

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

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

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)

13

Page 14: Operating Systems, 122

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

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

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

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

Operating SystemsMIDTERM 2012

Page 19: Operating Systems, 122

Question 1

(30( ) תזמון (schedulingנקודות

19

Page 20: Operating Systems, 122

Question 1. תארו בקצרה אך באופן מדוייק וברור את אופן נק'(5א. )•

Round robinפעולתם של שני האלגוריתמים הבאים לתזמון: ( fairness. בפרט, תארו איזו "הגינות" )Guaranteed schedulingו-

בתזמון מבטיחים שני האלגוריתמים.

20

פתרון:Round robin ' אלג – ה, preemptiveהינו משך את המגדיר פרמטר אשר time sliceבעל

' . במצב שהינם התהליכים מבין לריצה תהליכים בוחר האלג תהליך כל readyיקבל. מעגלי סדר פי על

במצב: לתהליכים שווה ריצה זמן לתת שואף ' readyהוגנות שאין. מבטיח האלגstarvation הממתינים התהליכים במספר חסום תהליך כל של ההמתנה שזמן מכוון

ה גודל .time sliceכפולGuaranteed scheduling ' אלג – לכל, preemptiveהינו שווה ריצה זמן לתת השואף

שהתהליך. הזמן בין היחס את המציין ערך של שמירה ידי על זאת עושה התהליכים ' . הקטן הערך עם התהליך את להריץ יבחר האלג לו שמגיע הזמן לבין בפועל קיבל

ביותר. , " "- ' יהיה: שלא מבטיח ובכך ביותר מקופח ה התהליך את שלב בכל בוחר האלג הוגנות

starvation .ל שיצאו בהתהליכים גם מתחשב היחס על I/Oחישוב אותם יפצה ובכך. שיאבדו הזמן

Page 21: Operating Systems, 122

Question 1

kernel. נניח כי במערכת קיימים מספר חוטי קרנל ) נק'(5ב. )•threads שהם )CPU-bound ומספר חוטי קרנל שהם I/O-bound .

איזה מבין שני האלגוריתמים מתאים יותר עבור מערכת כזו? נמקו בקיצור אך באופן מדוייק וברור.

21

פתרון:אלגוריתם את נעדיף זה מפני, Guaranteed schedulingבמקרה וזאת

ה שתהליכי יוודא בפחות IO-boundשהאלגוריתם ישתמשו לרוב אשר . כזה במובן מוכנים הם באשר לרוץ אפשרות יקבלו מעבד זמן

ל יותר קרוב יהיה שלנו אשר SJFהאלגוריתם תהליכים שיעדיף מכוון , מבחינת יותר הוגן יהיה ובנוסף יותר קצר מעבד לזמן זקוקים יהיו

. תחת תהליך כל שיקבל הכולל , R.Rהזמן זמן. לאורך זאת לעומת ) ה ) לתהליכי מעבד זמן מבחינת ברורה העדפה .CPU-boundתהיה

Page 22: Operating Systems, 122

Question 1

. כאשר עסקנו במוניטורים, תיארנו את המימושים נק'(8ג. )• )בו מובטח כי Hoare( מימוש מוניטור בסמנטיקה של 1הבאים.

( 2החוט המקבל את הסיגנל הוא הבא לרוץ במוניטור(, ו-, בו לא מתקיימת הבטחה שכזו ובכל פעם Javaמוניטור של

( המעוניינים להיכנס אליו ונמצאים במצב threadsהחוטים )ready.מתחרים על הכניסה

נניח כי במערכת ישנם מספר חוטים המשתמשים במוניטור. האם הבחירה בין שני האלגוריתמים הנ"ל לתזמון תשפיע יותר

או על מוניטור Hoareעל מימוש המשתמש במוניטור מטיפוס ? נמקו תשובתכם בקיצור אך באופן מדוייק וברור.Javaשל

22

Page 23: Operating Systems, 122

Question 1

פתרון:

, וזאת מכוון Javaהבחירה תשפיע יותר על מוניטור של שבמצב זה אלגוריתם התזמון יחליט למעשה מי

, אז Round robinהתהליך שיכנס למוניטור. אם יהיה זה יהיה זה התהליך המעוניין הבא שניתקל בו במעבר

, אזי Guaranteed schedulingהמעגלי. אם יהיה זה התהליך שיכנס למוניטור יהיה התהליך המעוניין שקיבל

, Hoareאת יחס הזמן הנמוך ביותר. במוניטור מטיפוס המוניטור למעשה יבחר מי התהליך הבא שיוכל להכנס,

ולכן לאלג' התזמון לא תהיה השפעה רבה על הריצה.

23

Page 24: Operating Systems, 122

Question 1

( יחיד ישנה CPU). נניח כי במערכת בעלת מעבד נק'(12ד. )• לפני Tקבוצת חוטים אשר כל אחד מהם רץ תמיד משך זמן

. נניח S אורך זמן context switch וכי I/Oשהוא עובר להמתנה ל-גם כי מערכת

באורך time-quantum עם round robinמופעל אלגוריתם תזמון Q ניתן להניח כי תמיד ישנם במערכת חוטים המוכנים לרוץ .

במערכת CPU(. חשבו את ניצולת ה-I/O)אינם ממתינים ל-כפונקציה של פרמטרים אלו עבור המקרים הבאים:

.IQ>T.IIS<Q<T

.IIIQשואף לאפס

24

Page 25: Operating Systems, 122

Question 1

פתרון:.I במקרה זהQ חסר משמעות. כל תהליך ירוץ זמן T ואז נבזבז S זמן ב

C.S :לכן ניצולת המעבד תהיה .

.II:התקבלו תשובות בהן הונח ש . במצב כזה נקבל ניצולת מעבד

.III:כאשר נקבל על פי הנוסחה מהסעיף הקודם

25

Page 26: Operating Systems, 122

Question 2

(35 ) וחוטים תהליכים נקודות

26

Page 27: Operating Systems, 122

Question 2

. מהם הפלטים האפשריים של התוכנית הבאה, על נק'(15א. )•? עבור כל פלט, ציינו האם הוא מודפס על a.txtהמסך ובקובץ

ידי התהליך האב, תהליך בן או חוט. הניחו כי התוכנית רצה על מערכת עם מעבד יחיד, וכי פתיחת הקובץ מצליחה. הניחו כי

בכל תהליך המזהים של החוטים נקבעים בסדר עולה, החל . בנוסף, הניחו כי כאשר תכנית מרובת חוטים מבצעת 1מ-

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

(.context switchבאמצע על ידי

27

Page 28: Operating Systems, 122

Question 2int main() { int fd; pthread_t tid[2]; printf("STARTED\n"); fd = dup(STDOUT_FILENO); close(STDOUT_FILENO); open("a.txt", O_WRONLY | O_CREAT | O_TRUNC);

pthread_create(&tid[0], NULL, foo, NULL); fork(); pthread_create(&tid[1], NULL, foo, NULL); sleep(10); // sleep for 10 seconds printf("after fork x=%d\n", x);

close(STDOUT_FILENO); dup(fd); printf("FINISHED\n");

return 0;}

28

int x = -1;

void foo() { printf("x=%d\n", x); x = pthread_self(); // get thread ID}

Page 29: Operating Systems, 122

Question 2On screen: STARTEDFINISHEDFINISHED

a.txt: x = -1 (parent)x = -1/1/2 (parent)x = -1/1 (child)after fork x = 1/2 (parent)after fork x = 1 (child) * first 3 lines can come in any order* last 2 lines could come in any order

29

פתרון:

Page 30: Operating Systems, 122

Question 2. הניחו כי ברשותכם שרת אינטרנט המספק בקשות נק'(10ב. )•

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

(. הניחו כי לשרת מעבד יחיד. כמו I/Oמתבצע תהליך אחזור מתוך דיסק ) את h לדיסק במקביל. נסמן ב-I/Oכן הניחו כי לא ניתן לבצע שתי פעולות

את הזמן הנדרש cההסתברות למציאת המידע הנדרש במבנה הנתונים, ב- את משך הזמן הנדרש לאחזור מידע tלאחזור מידע ממבנה הנתונים וב-

(. בפרט, כאשר המידע הנדרש לא נמצא במבנה t>cמהדיסק )ברור כי )ראשית יש לקרוא את המידע מן t+cהנתונים, משך האיחזור הכולל הוא

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

מהו משך הזמן הצפוי עד להשלמת הבקשות במערכת מבוססת חוטי משתמש–(user space threads?)

מהו משך הזמן הצפוי עד להשלמת הבקשות במערכת מבוססת חוטי קרנל –(kernel threads?)

30

Page 31: Operating Systems, 122

Question 2

פתרון:חוטי משתמש:–

2ch^2+2(c+t)(1-h)^2+h(1-h)(4c+2t)

חוטי קרנל: –2ch^2+ (c+2t)(1-h)^2+h(1-h)(2c+t+c+t)

31

מתחלק לשני מקרים סימטריים כל אחד 2c+t

2c+tגם מתחלק לשני מקרים: הצלחה + כישלון נותן c<t כי c+t כישלון + הצלחה נותן

c+tפעמיים גישה של

c השני מתחיל cשאחד מסיים של הראשוןtבזמן ה

Page 32: Operating Systems, 122

Question 2. נתון קטע הקוד הבא: נק'(10ג. )•

void sigchld_handler(int s) { printf(“S”);}int main(){ signal(SIGCHLD, sigchld_handler); signal_block(SIGCHLD); if (fork() != 0) { printf(“A”); signal_unblock(SIGCHLD); printf(“B”); wait (); printf(“C”); } else { printf(“D”); }}

32

Page 33: Operating Systems, 122

Question 2

חוסמות signal_unblock וכן signal_blockידוע כי הפקודות ומשחררות חסימה לסיגנלים. שרטטו גרף מכוון המתאר את

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

" X" וכי ההדפסה של "Z" ו- "X", "Yעפ"י קוד מסוים ידוע כי יודפסו "" יכול להופיע לפני או Z" )אך "Yתופיע בהכרח לפני ההדפסה של "

אחרי כל אחת מן ההדפסות האחרות(, יתקבל הגרף הבא:

33

YX Z

Page 34: Operating Systems, 122

Question 2

34

A

S

B

C

D

פתרון:

void sigchld_handler(int s) { printf(“S”);}int main(){ signal(SIGCHLD, sigchld_handler); signal_block(SIGCHLD); if (fork() != 0) { printf(“A”); signal_unblock(SIGCHLD); printf(“B”); wait (); printf(“C”); } else { printf(“D”); }}

Page 35: Operating Systems, 122

Question 3

(35 ) סינכרוניזציה נקודותהדדית מניעה

35

Page 36: Operating Systems, 122

Question 3

. בכיתה הגדרנו את התנאים חופש מקיפאון נק'(5א. )•(deadlock freedom( וחופש מהרעבה )starvation freedom )

עבור אלגוריתמים למניעה הדדית. כתבו את ההגדרות של תנאים אלו.המדוייקות

36

פתרון:: מקיפאון , חופש הקריטי לקטע להיכנס מנסה תהליכים קבוצת אם

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

. אליו להיכנס יצליח הוא צעדים של סופי מספר לאחר

Page 37: Operating Systems, 122

Question 3

( עבור בעיית fairness). בכיתה הגדרנו תנאי הוגנות נק'(10ב. )•.first-in-first-out (FIFO)המניעה ההדדית הקרוי

של תנאי זה.המדוייקתכתבו את ההגדרה –האלגוריתם למניעה הדדית של פטרסון לשני תהליכים מובא בסוף –

? נמקו בקצרה אך FIFOשאלה זו. האם האלגוריתם מקיים תנאי במדוייק.

37

פתרון:תהליך – -Aאם ב ) נמצא שתהליך( waiting sectionממתין התחיל Bלפני

- ה את לפני Bאזי doorwayלבצע יכנס Aלאאת. – לבצע החל שהשני לפני להמתנה נכנס התהליכים אחד אם כן

, הקריטי לקטע ראשון יכנס הראשון התהלך אזי הכניסה קטע

Page 38: Operating Systems, 122

Question 3. בשאלה זו עליכם לממש אלגוריתם למניעה הדדית עבור נק'(10ג. )•

. על האלגוריתם לקיים מניעה הדדית r ו-p, qשלושה תהליכים – וחופש מקיפאון. בנוסף, עליו לקיים את הדרישה הבאה המבטיחה

: pעדיפות לתהליך מתחיל להמתין למנעול טרם שהתהליך המחזיק במנעול pאם

הוא התהליך p( שלו, אזי exit sectionהחל לבצע את קוד היציאה )הבא שיכנס לקטע הקריטי.

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

(. ניתן גם test-and-setלהשתמש בסמפורים או בפעולות כגון להשתמש באלגוריתם של פטרסון למניעה הדדית עבור שני תהליכים כאבן בניין. הסבירו בקצרה את האלגוריתם שכתבתם ונמקו בקצרה

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

38

Page 39: Operating Systems, 122

Question 3

למניעה הדדית עבור שני האלגוריתם של פטרסון להלן . false מאותחלים לערך b[1] ו-b[0]תהליכים. שני הדגלים

)אין זה משנה מבחינת 1 או 0 מאותחל לערך turnהמשתנה נכונות האלגוריתם(.

39

Algorithm for p0

b[0]=trueturn=0await (b[1]=false OR turn=1)CSb[0]=false

Algorithm for p1

b[1]=trueturn=1await (b[0]=false OR turn=0)CSb[1]=false

Page 40: Operating Systems, 122

Question 3

פתרון זה עושה שימוש באבן בניין מסוג פטרסון:פתרון:

בקטע הקריטי אזי התהליך השני לא r או qאם אחד מהתהליכים כבר p טרם שהראשון יצא ואזי אם Peterson(p,qr)יחל לבצע את

הוא הבא שיכנס. אופן אחר להסתכל על כך Peterson(p,qr)מחכה ב –p .מתחרה" תמיד רק עם אחד משני התהליכים האחרים"

האלגוריתם הוא גם חסר הרעבה.40

Algorithm for q

Peterson)q,r(.lock // as p0Peterson)p,qr(.lock // as p1CSPeterson)p,qr(.unlock // as p1Peterson)q,r(.unlock // as p0

Algorithm for r

Peterson)q,r(.lock // as p1Peterson)p,qr(.lock // as p1CSPeterson)p,qr(.unlock // as p1Peterson)q,r(.unlock // as p1

Algorithm for p

Peterson)p,qr(.lock // as p0CSPeterson)p,qr(.unlock // as p0

Page 41: Operating Systems, 122

Question 3

הפעם מימוש ללא אבני בניין:פתרון:

41

void enter)i( { // for i = 0 || 1 intrested[i] = true; turn[0] = 1- i; while )intrested[1-i] = true && turn[0] = 1-i(; turn[1] = 2; while )Intrested[2] = true && turn[1] = 2(; }

void enterForProcess2)( { intrested[2] = true; turn[1] = 0; while ))intrested[0] = true || intrested[1] = true( && turn[1] = 0(; }

void leave)i( { intrested[i] = false;}

// We set for each process an id. q as 0, r as 1, and p as 2.intrested[1..3] = {false, false, false}turn[1..2] // init values are not important

Page 42: Operating Systems, 122

Question 3

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

(starvation freedom האם טענתה צודקת? נמקו תשובתכם .)בקצרה, אך באופן מדוייק וברור.

42

פתרון: , . ג, סעיף של הדרישות מקום ומכל לעיל האלגוריתם ולראיה לא

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