Upload
others
View
3
Download
0
Embed Size (px)
Citation preview
Operating Systems, 142
Practical Session 11
File Systems & Midterm 2014
1
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
File system layout (Tanenbaum)
3
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
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
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:
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
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
Question 1: i-nodes A similar problem
8
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
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
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
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); }
12
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 1K
2. The block size is 4K
(The i-node has 10 direct block entries, one single, double & triple indirect)
13
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
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
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
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
17
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
Operating Systems
MIDTERM 2014
Question 1
מניעה הדדית ( נקודות 35)
(mutual exclusion)
20
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 Section y := 0; x := 0;
(.'נק 15) שורות 10-בלא יותר מהוכיחו או הפריכו ? האם האלגוריתם מקיים מניעה הדדית .א (.'נק 10) שורות 10-בלא יותר מהוכיחו או הפריכו ? deadlockהאם האלגוריתם מונע .ב ('נק 10)? האם תתכן הרעבה .ג
21
Question 1 בלא הוכיחו או הפריכו ? האם האלגוריתם מקיים מניעה הדדית. א
(.'נק 15) שורות 10-יותר מ
22
:פתרון
:הדדיתהאלגוריתם אינו מקיים מניעה
התוצאה . 0ראשון ואחריו תהליך 1עובר את שורה 1שתהליך נניח . x = 0 -היא ש
עדיין שווה y -כיוון ש, 2-4התהליכים עוברים לסירוגין את שורות שני
0 .
. ונכנס לקטע הקוד הקריטי 5עובר ראשון את שורה 0תהליך כעת
חוזר לשורה , 7-9נכנס לשורות ( אינו מתאים לו xשערך ) 1תהליך
.ואז נכנס גם הוא( y = 0 -כיוון ש)מבצע את כל הקוד , הראשונה
Question 1
-בלא יותר מהוכיחו או הפריכו ? deadlockהאם האלגוריתם מונע .ב
(.'נק 10) שורות 10
23
:פתרון
: .deadlock-freedomהאלגוריתם אינו מקיים
deadlock מבצע מספר לא חסום של פעמים את 0לקרות בתסריט שבו תהליך עשוי . 1-9מבצע מספר לא חסום של פעמים את שורות 1ותהליך 1-4שורות
:הואשנדרש כל
.2מבצע את שורה 0לפני שתהליך 5יעבור את שורה 1שתהליך . א
. 6מבצע את שורה 1לפני שתהליך 1יבצע את שורה 0שתהליך . ב
( xכי הוא מאפס את )לחזור להתחלה 1מאפשר לתהליך 0תהליך , דינאמי בתאורבמלים אחרות . 2עבר את שורה 0רק אחרי שתהליך yמאפס את 1ואילו תהליך
.5-9אף פעם לא יגיע לשורות 0תהליך
Question 1
('נק 10)? האם תתכן הרעבה . ג
24
:פתרון
חופש מקיים אינו שהאלגוריתם מכך ישירות נובע .הרעבה תתכן .מקיפאון
Question 2
XV6( נקודות 40)
25
Question 2
XV6 –מערכת התרגילים המעשיים ( 'נק 40) :spinlock.cלפניכם קוד מתוך
1469 // Acquire the lock.
1470 //
1471 //
1472 //
1473 void
1474 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
Question 2
27
הדגימו מקרים של בעיות סנכרון . שורות את מטרת הקוד 2-3 -הסבירו ב. אבדוגמאות הסבירו מדוע יש צורך . 1483 -ו 1476שדורשות את השורות
('נק 10. )לבצע את השורות האלה
:פתרון
. בצורה בטוחה MEמ להשיג "נועד לצורך הגנה של מבנים משותפים ע spinlock -ה
מקרים בהם יכולה 2נבחין בין spinlock -כדי להבין יותר טוב את המימוש של ה
:MEלהתבצע הפרה של
כדי לפתור בעיה זו קיים –מעבד שונה מזה אשר ביצע נעילה \קוד על ליבהריצת •
(.1483שורה ) xchgשימוש בפעולה אטומית
לדוגמא – interruptמעבר אסינכרוני בעזרת )אחר על אותו המעבד לקוד מעבר •
I/O interrupt .) בעיה אפשרית הנובעת מכך היא ביצועacquire על אותו המנעול
כדי למנוע מצב כזה קיים (. אפשר כמובן לתת דוגמאות אחרות)י אותו המעבד "ע .)1476שורה ) interruptsשימוש בביטול
Question 2
יהיה kernel -מ לשפר את ביצועי מערכת ההפעלה היינו מעוניינים כי ה"ע. ב
האם ניתן לשפר , בלבד kernelנועד לצרכי spinlock -מאחר ו. יעיל ככל הניתן
:במקרים הבאים spinlock -את ביצועי ה
(יחידהעם ליבה )רץ על מעבד יחיד kernel -בה הבמערכת •
.non preemptive scheduler -משתמש ב kernel -בה הבמערכת •
במידה ( שינוי של הקוד/ הורדה )עבור כל אחד מהמקרים הציעו שיפורים
שורות 2-3 -נמקו את תשובתכם ב. הסבירו מדוע, במידה ולא, ושיפור אפשרי
(. 'נק 7)
28
Question 2
29
:פתרון
:בהתאם למקרים אשר נותחו בסעיף הקודם
ניתן לבטל , יחיד( מעבד)רץ על מחשב עם ליבה kernel -כאשר ה•
אפשר כמובן לבטל את השורות . 1519ו 1483את שורות .בהתאם holdingולתקן את ( 1508, 1487)הקשורות למעבד
אשר interruptsלא ניתן לבצע שינויים במערכת הזו מכיוון שקיימים •
.לתזמון התהליכיםלא קשורים
Question 2
חוזרים handlers -כל ה execלאחר ביצוע , signals -במערכת הפעלה התומכת ב. ג
(.'נק 8)הסבירו מדוע . default handlers -להיות ה
30
:פתרון
, כל הזיכרון של התהליך מתחלף exec system callביצוע במהלך
לא יוחזרו להיות signal handlersאם ה . code segmentבפרט גם ה
אזי המצביעים הישנים ( שינויים של מצביעי הפונקציות)ברירת מחדל
יכולים להצביע על מקומות בזיכרון אשר לא שייכים לתהליך או לא (. הישנים signal handlersבמילים אחרות לא יצביעו ל )מכילים קוד
Question 2
בשתי שורות . default signal handlersתארו בשתי שורות בעיה במימוש של . ד
(.'נק 8)נוספות תארו כיצד התגברתם על הבעיה במימוש שלכם בעבודה
31
:פתרון
kernelב הוא קוד משתמש ולא ידוע signal handler -הבעיה היא ש
(. מצביע לפונקציה)שלו כתובת ההתחלה מה
entry pointאפשרי במימוש העבודה השנייה הוא הגדרה של פתרון
את " עוטף"לתוכנית אשר ( linker -על ידי שינוי ההגדרות של ה)חדש
. signal handlersומבצע רישום של main -ה -קריאה ל)והסבירו פתרון אפשרי kernel space -ל user spaceגם תשובות שדנו בבעיה בין התקבלו •
default ישירות מתוך ה- kernel וקריאה ל- non default באמצעות יצירתframe על מחסנית של ה-
user.)
Question 2
אשר לא signal handlerהניח 2מימוש הסיגנלים שהוגדר בעבודה . ה
סטודנט חרוץ ניסה לקרב את המימוש של הסיגנלים . מקבל ארגומנטים signal -על ידי שינוי החתימה של ה linux -למימוש ב xv6 -ב
handler .במימוש שלו ה- handler קיבל ארגומנט יחיד המייצג את
signal -הסבירו בפירוט אלו שינויים במנגנון הפעלת ה. מספר הסיגנלhandler (.'נק 7)הסטודנט ביצע כדי שהמנגנון יעבוד
32
Question 2
33
:פתרון
.user -הכנסתו למחסנית ה -signal handler -העברת הארגומנט ל•
במציאת הוא signal handler -הבעיתי בהעברת ארגומנט להחלק •
מי , לפי הקונבנציה)מהמחסנית " ייצא"דרך לגרום לכך שהארגומנט
-כלומר בזמן הכנת ה(. שמבצע קריאה לפונקציה אחראי על כךframe להרצתhandler בתור כתובת חזרה כתובת צריכים להכניס
(.בנוסף לארגומנט עצמו)פונקציה אשר תדע לבצע ניקוי ארגומנט
kernel -לנגרום לחזרה . kernel -הפונקציה יכולה להיות גם ב•
.kernel -את ניקוי הארגומנט מהונבצע handler -בסיום ריצת ה
Question 3
system calls( נקודות 25)
34
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. }
Question 3
ציין את הפלטים האפשריים של התוכנית והסבר במדויק (. 'נק 15). א 10ניתן להניח ש. )ובקצרה מדוע אלו הפלטים העשויים לנבוע מריצת התכנית
שניות שינה הן זמן מספיק עבור (.mainהראשי לבצע את כל הפקודות ב thread-ה
36
Question 3
37
:פתרון
SIGINTעבור הסיגנל signal_handler' הפוננרשמת •
(p1 נסמנו)עבור התהליך t1ראשון threadנוצר •
forkהנוכחי מכיוון ש p2אינו קיים ב p1של thread t1ה) p2 נסמנותהליך חדש נוצר •
(posixים בthreadאינו משכפל
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הסדר בין הטיפול ב•
Question 3
-אאו סינכרוני הינו 10האם הסיגנל שנשלח בשורה ( 'נק 5). ב
אם ענית , כלומר. )תן דוגמא לקריאה לסיגנל מהסוג השני? סינכרוני (.ולהיפך, סינכרוניסינכרוני תן דוגמא לסיגנל -שהסיגנל הוא א
38
:פתרון
הינה קריאת מערכת killסנכרוני מכיוון שהקריאה להטיפול הינו •ובו מסומן על התהליך שקרא kernelאפשר מעבירה אותנו למצב
כשתסתיים הקריאה ונרצה לחזור למצב , שהתקבל סיגנל .משתמש נראה שישנו סיגנל ונחזור ישר לטיפול בו
על מנת לשלוח killדוגמא לטיפול לא סינכרוני הוא שימוש ב• .סיגנל לתהליך אחר
Question 3
: איך ישתנה הפלט אם נוריד את השורה האחרונה( 'נק 5). גpthread_exit(NULL)?
39
:פתרון main' הפונהראשי יסיים את threadה pthread_exitאם נוריד את
יסגרוהתהליך ()exitמה שיגרום לקריאה בלתי מפורשת לפונקציה
.סיגנלמבלי לאפשר לחוטים להדפיס את ההודעות שלהם או לשלוח