1 / 42

Operating Systems, 122

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)

Download Presentation

Operating Systems, 122

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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)

  3. File system layout (Tanenbaum)

  4. 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

  5. Quick recap: i-Nodes General file attributes File Size HardLink count The number of hard-links to the file Usually between 10 and 12

  6. Question 1: i-nodes How many time will the disk be accessed when a user executes the following command: more /usr/tmp/a.txt Assume that: • The size of 'a.txt' is 1 block. • The i-node of the root directory is not in the memory. • Entries 'usr', 'tmp' and 'a.txt' are all located in the first block of their directories.

  7. 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'si-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.

  8. Question 1: i-nodes A similar problem

  9. 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.

  10. 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.

  11. 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: • nodeis 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) andfree_i-node(i-node node).

  12. Question 3: i-nodes delete(i-node node){ // remove the direct blocks for each block b in node.direct do free_block(b); // remove the single indirect blocks single <-- read_block(node.single_indirect) for each entry e in single do free_block(e); free_block(single); // remove the double indirect blocks double <-- 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-node free_i-node(node); }

  13. 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 : • The block size is 1K • The block size is 4K (The i-node has 10 direct block entries)

  14. Question 4: i-nodes • 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

  15. Question 4: i-nodes • 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

  16. 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.

  17. 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 #504

  18. Operating SystemsMIDTERM 2012

  19. Question 1 (30 נקודות) תזמון (scheduling)

  20. Question 1 • א. (5 נק'). תארו בקצרה אך באופן מדוייקוברור את אופן פעולתם של שני האלגוריתמים הבאים לתזמון: Round robin ו-Guaranteed scheduling. בפרט, תארו איזו "הגינות" (fairness)בתזמון מבטיחים שני האלגוריתמים. פתרון: Round robin – הינו אלג' preemptive, בעל פרמטר המגדיר את משך ה time slice אשר יקבל כל תהליך. האלג' בוחר תהליכים לריצה מבין התהליכים שהינם במצב ready על פי סדר מעגלי. הוגנות: שואף לתת זמן ריצה שווה לתהליכים במצב ready. האלג' מבטיח שאין starvation מכוון שזמן ההמתנה של כל תהליך חסום במספר התהליכים הממתינים כפול גודל ה time slice. Guaranteed scheduling – הינו אלג' preemptive, השואף לתת זמן ריצה שווה לכל התהליכים. עושה זאת על ידי שמירה של ערך המציין את היחס בין הזמן שהתהליך קיבל בפועל לבין הזמן שמגיע לו. האלג' יבחר להריץ את התהליך עם הערך הקטן ביותר. הוגנות: האלג' בוחר בכל שלב את התהליך ה-"מקופח" ביותר, ובכך מבטיח שלא יהיה starvation. חישוב היחס מתחשב גם בהתהליכים שיצאו ל I/O ובכך יפצה אותם על הזמן שיאבדו.

  21. Question 1 • ב. (5 נק'). נניח כי במערכת קיימים מספר חוטי קרנל (kernel threads) שהם CPU-bound ומספר חוטיקרנלשהם I/O-bound. איזה מבין שני האלגוריתמים מתאים יותר עבור מערכת כזו? נמקו בקיצור אך באופן מדוייק וברור. פתרון: במקרה זה נעדיף את אלגוריתם Guaranteed scheduling, וזאת מפני שהאלגוריתם יוודא שתהליכי ה IO-bound אשר לרוב ישתמשו בפחות זמן מעבד יקבלו אפשרות לרוץ באשר הם מוכנים. במובן כזה האלגוריתם שלנו יהיה קרוב יותר ל SJF מכוון שיעדיף תהליכים אשר יהיו זקוקים לזמן מעבד קצר יותר, ובנוסף יהיה הוגן יותר מבחינת הזמן הכולל שיקבל כל תהליך. תחת R.R. לעומת זאת, לאורך זמן תהיה העדפה ברורה (מבחינת זמן מעבד) לתהליכי ה CPU-bound.

  22. Question 1 • ג. (8 נק'). כאשר עסקנו במוניטורים, תיארנו את המימושים הבאים. 1) מימוש מוניטור בסמנטיקה של Hoare (בו מובטח כי החוט המקבל את הסיגנל הוא הבא לרוץ במוניטור), ו-2) מוניטור של Java,בו לא מתקיימת הבטחה שכזו ובכל פעם החוטים (threads) המעוניינים להיכנס אליו ונמצאים במצב ready מתחרים על הכניסה.נניח כי במערכת ישנם מספר חוטים המשתמשים במוניטור. האם הבחירה בין שני האלגוריתמים הנ"ל לתזמון תשפיע יותר על מימוש המשתמש במוניטור מטיפוס Hoare או על מוניטור של Java? נמקו תשובתכם בקיצור אך באופן מדוייק וברור.

  23. Question 1 פתרון: הבחירה תשפיע יותר על מוניטור של Java, וזאת מכוון שבמצב זה אלגוריתם התזמון יחליט למעשה מי התהליך שיכנס למוניטור. אם יהיה זה Round robin, אז יהיה זה התהליך המעוניין הבא שניתקל בו במעבר המעגלי. אם יהיה זה Guaranteed scheduling, אזי התהליך שיכנס למוניטור יהיה התהליך המעוניין שקיבל את יחס הזמן הנמוך ביותר. במוניטור מטיפוס Hoare, המוניטור למעשה יבחר מי התהליך הבא שיוכל להכנס, ולכן לאלג' התזמון לא תהיה השפעה רבה על הריצה.

  24. Question 1 • ד. (12 נק'). נניח כי במערכת בעלת מעבד (CPU) יחיד ישנה קבוצת חוטים אשר כל אחד מהם רץ תמיד משך זמן T לפני שהוא עובר להמתנה ל-I/O וכי context switch אורך זמן S. נניח גם כי מערכת מופעל אלגוריתם תזמון round robin עם time-quantum באורך Q. ניתן להניח כי תמיד ישנם במערכת חוטים המוכנים לרוץ (אינם ממתינים ל-I/O). חשבו את ניצולת ה-CPU במערכת כפונקציה של פרמטרים אלו עבור המקרים הבאים: • Q>T • S<Q<T • Q שואף לאפס

  25. Question 1 פתרון: • במקרה זה Q חסר משמעות. כל תהליך ירוץ זמן T ואז נבזבז S זמן ב C.S. לכן ניצולת המעבד תהיה: • התקבלו תשובות בהן הונח ש . במצב כזה נקבל ניצולת מעבד: • כאשר נקבל על פי הנוסחה מהסעיף הקודם:

  26. Question 2 (35 נקודות) תהליכים וחוטים

  27. Question 2 • א. (15 נק'). מהם הפלטים האפשריים של התוכנית הבאה, על המסך ובקובץ a.txt? עבור כל פלט, ציינו האם הוא מודפס על ידי התהליך האב, תהליך בן או חוט. הניחו כי התוכנית רצה על מערכת עם מעבד יחיד, וכי פתיחת הקובץ מצליחה. הניחו כי בכל תהליך המזהים של החוטים נקבעים בסדר עולה, החל מ-1. בנוסף, הניחו כי כאשר תכנית מרובת חוטים מבצעת פעולת fork רק החוט אשר ביצע את הקריאה משתכפל. הניחו גם כי כל פעולת הדפסה היא אטומית (כלומר, היא לא תופרע באמצע על ידי context switch).

  28. Question 2 intx = -1; void foo() { printf("x=%d\n", x); x = pthread_self(); // get thread ID } intmain() { intfd; pthread_ttid[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; }

  29. Question 2 פתרון:

  30. Question 2 • ב. (10 נק'). הניחו כי ברשותכם שרת אינטרנט המספק בקשות משתמשים. לשרת מבנה נתונים בו הוא שומר ערכים שהובאו עבור בקשות שהתבצעו לאחרונה. בכל פעם שמתקבלת בקשה, מתבצעת פעולת חיפוש מהירה במבנה הנתונים. במידה והמידע הנדרש אינו מצוי במבנה הנתונים, מתבצע תהליך אחזור מתוך דיסק (I/O). הניחו כי לשרת מעבד יחיד. כמו כן הניחו כי לא ניתן לבצע שתי פעולות I/O לדיסק במקביל. נסמן ב-h את ההסתברות למציאת המידע הנדרש במבנה הנתונים, ב-c את הזמן הנדרש לאחזור מידע ממבנה הנתונים וב-t את משך הזמן הנדרש לאחזור מידע מהדיסק (ברור כי t>c). בפרט, כאשר המידע הנדרש לא נמצא במבנה הנתונים, משך האיחזור הכולל הוא t+c (ראשית יש לקרוא את המידע מן הדיסק למבנה הנתונים ואז לקראו ממבנה הנתונים). נתון כי שתי בקשות בלתי תלויות זו בזו, כל אחת מהן למידע שונה, מגיעות בו זמנית למערכת. • מהו משך הזמן הצפוי עד להשלמת הבקשות במערכת מבוססת חוטי משתמש(user space threads)? • מהו משך הזמן הצפוי עד להשלמת הבקשות במערכת מבוססת חוטי קרנל (kernel threads)?

  31. 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) • פעמיים גישה של c+t • מתחלק לשני מקרים סימטריים כל אחד 2c+t • שאחד מסיים c השני מתחיל c בזמן ה t של הראשון • גם מתחלק לשני מקרים: הצלחה + כישלון נותן 2c+t כישלון +הצלחה נותן c+t כי c<t

  32. 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”); } }

  33. Question 2 ידוע כי הפקודות signal_block וכן signal_unblockחוסמות ומשחררות חסימה לסיגנלים. שרטטו גרף מכוון המתאר את הפלטים האפשריים לקוד זה. כל צומת בגרף תסמל הדפסה וכל קשת מכוונת תייצג יחס סדר מתחייב בין הדפסות. לדוגמא, אם עפ"י קוד מסוים ידוע כי יודפסו "X", "Y" ו- "Z" וכי ההדפסה של "X" תופיע בהכרח לפני ההדפסה של "Y" (אך "Z" יכול להופיע לפני או אחרי כל אחת מן ההדפסות האחרות), יתקבל הגרף הבא: X Y Z

  34. Question 2 פתרון: 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”); } } A D B S C

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

  36. Question 3 • א. (5 נק'). בכיתה הגדרנו את התנאים חופש מקיפאון(deadlock freedom) וחופש מהרעבה (starvation freedom) עבור אלגוריתמים למניעה הדדית. כתבו את ההגדרות המדוייקותשל תנאים אלו. פתרון:חופש מקיפאון: אם קבוצת תהליכים מנסה להיכנס לקטע הקריטי, אזי לאחר מספר סופי של צעדים אחד התהליכים יצליח להיכנס אליו. חופש מהרעבה: אם תהליך מנסה להיכנס לקטע הקריטי, אזי לאחר מספר סופי של צעדים הוא יצליח להיכנס אליו.

  37. Question 3 • ב. (10 נק'). בכיתה הגדרנו תנאי הוגנות (fairness) עבור בעיית המניעה ההדדית הקרוי first-in-first-out (FIFO). • כתבו את ההגדרה המדוייקת של תנאי זה. • האלגוריתם למניעה הדדית של פטרסון לשני תהליכים מובא בסוף שאלה זו. האם האלגוריתם מקיים תנאי FIFO? נמקו בקצרה אך במדוייק. פתרון: • אם תהליך A ממתין (נמצא ב-waiting section) לפני שתהליך B התחיל לבצע את ה-doorway אזי B לא יכנס לפני A • כן. אם אחד התהליכים נכנס להמתנה לפני שהשני החל לבצע את קטע הכניסה, אזי התהלך הראשון יכנס ראשון לקטע הקריטי

  38. Question 3 • ג. (10 נק'). בשאלה זו עליכם לממש אלגוריתם למניעה הדדית עבור שלושה תהליכים – p, q ו-r. על האלגוריתם לקיים מניעה הדדית וחופש מקיפאון. בנוסף, עליו לקיים את הדרישה הבאה המבטיחה עדיפות לתהליך p: אם p מתחיל להמתין למנעול טרם שהתהליך המחזיק במנעול החל לבצע את קוד היציאה (exit section) שלו, אזי p הוא התהליך הבא שיכנס לקטע הקריטי.לצורך המימוש מותר להשתמש אך ורק במשתנים התומכים בקריאות ובכתיבות. אין להשתמש בסוגים אחרים של משתנים (בפרט, אין להשתמש בסמפורים או בפעולות כגון test-and-set). ניתן גם להשתמש באלגוריתם של פטרסון למניעה הדדית עבור שני תהליכים כאבן בניין. הסבירו בקצרה את האלגוריתם שכתבתם ונמקו בקצרה מדוע הוא מקיים את שנדרש (אין צורך בהוכחה פורמלית).

  39. Question 3 להלן האלגוריתם של פטרסון למניעה הדדית עבור שני תהליכים. שני הדגלים b[0] ו-b[1]מאותחלים לערך false. המשתנה turn מאותחל לערך 0 או 1 (אין זה משנה מבחינת נכונות האלגוריתם). 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

  40. Question 3 פתרון: פתרון זה עושה שימוש באבן בניין מסוג פטרסון: אם אחד מהתהליכים q או r בקטע הקריטי אזי התהליך השני לא יחל לבצע את Peterson(p,qr) טרם שהראשון יצא ואזי אם p כבר מחכה ב Peterson(p,qr) הוא הבא שיכנס. אופן אחר להסתכל על כך – p "מתחרה" תמיד רק עם אחד משני התהליכים האחרים. האלגוריתם הוא גם חסר הרעבה. Algorithm for q • Peterson(q,r).lock // as p0 • Peterson(p,qr).lock // as p1 • CS • Peterson(p,qr).unlock // as p1 • Peterson(q,r).unlock // as p0 Algorithm for r • Peterson(q,r).lock // as p1 • Peterson(p,qr).lock // as p1 • CS • Peterson(p,qr).unlock // as p1 • Peterson(q,r).unlock // as p1 Algorithm for p • Peterson(p,qr).lock // as p0 • CS • Peterson(p,qr).unlock // as p0

  41. Question 3 פתרון: הפעם מימוש ללא אבני בניין: // 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 • 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; }

  42. Question 3 • ד. (10 נק'). שלומציונה טוענת כי אלגוריתם המקיים את הדרישות של סעיף ג. בהכרח אינו מקיים חופש מהרעבה(starvation freedom). האם טענתה צודקת? נמקו תשובתכם בקצרה, אך באופן מדוייק וברור. פתרון:לא, ולראיה האלגוריתם לעיל. ומכל מקום, הדרישות של סעיף גאינן גוררות שלא מתקיים חופש מהרעבה.

More Related