Upload
phillip-whitfield
View
23
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Operating Systems, 132. Practical Session 11 File Systems & Midterm 201 3. 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
Citation preview
Operating Systems, 132
Practical Session 11 File Systems & Midterm 2013
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 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
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-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
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-nodesA 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-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
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
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 #50417
Operating SystemsMIDTERM 2013
Question 1
(35 ) הדדית מניעה נקודות(mutual exclusion)
19
Question 1
( לשני תהליכים mutual exclusionנתון אלגוריתם למניעה הדדית )המשתמש בשני ביטים, אחד לכל תהליך. כל תהליך כותב רק על שני השני. התהליך של הביט את גם לקרוא ויכול שלו הביט
לערך מאותחלים מסומנים 0הביטים התהליכים או 0. והקוד 1 לפי 1 או 0 שמקבל את הערך iהמופיע למטה כתוב עבור תהליך
התהליך המריץ את הקוד.
20
b[i] := i;while b[0] = i do
b[i] := 1;while b[1] = 1-i do b[i] := 0;
Critical Sectionb[i] := 0;
Question 1א. האם האלגוריתם מקיים מניעה הדדית לשני תהליכים ? הוכיחו
(. נק'15 ) שורות10בלא יותר מ-או הפריכו
21
פתרון:שני בין אפשריים ריצה סדרי של בלבד אפשרויות בשתי נדון
שתהליך : התהליכים שורה 1או את שתהליך 1מבצע לפני 0שלושורה את . 4מבצע שלא, או
• , תהליך לפני שורה 0אם את יעבור שתהליך 4לא יאפס 1עד. b[1]את לקוד מחוץ יקרה וזה
• , תהליך אחרי שורות 1אם את שיתאפס 2-3יבצע וזה b[0]עדכשתהליך .0יקרה לקוד מחוץ יהיה
Question 1
בלא ? הוכיחו או הפריכו deadlockהאם האלגוריתם מונע ב. (. נק'10 ) שורות10יותר מ-
22
פתרון:יתכן שלא שתהליך. deadlockנוכיח היחידה הקוד עלול 1שורת
" שבשורה" הלולאה היא עליה היעצר קודם 2ל כבר הראינו כי . שורה אבל מבצע אינו הוא הרביעית השורה על 2שאת מתבצעת
תהליך אם 1ידי שתהליך, b[0] == 1רק השורה 0כלומר את ביצעתהליך. ידי על השלישית השורה ביצוע שלאחר אלא 0השלישית
תהליך שורה 1ואם של בלולאה את 0תהליך, 2תקוע יבצעשורה. 4שורה על שלו תהליך 4תקיעה את שהנחנו 1תשחרר
. . לסתירה הגענו כאן גם השניה שבשורה הלולאה על תקוע שהוא
Question 1
( נק'10האם תתכן הרעבה ? )ג.
23
פתרון: : . תהליך הבא במקרה נסתכל בהחלט תיתכן בקטע 1הרעבה
ותהליך בשורה 0הקריטי הלולאה על תהליך. 4ממתין 1כאשרמתקיים הקריטי תהליך. b[1] == 1בקטע הביט 1ביציאה את מאפס
- ל ערכו את ומחליף הראשונה לשורה ממשיך מיד אך . 1שלותהליך כזה ותהליך 0במקרה שלו בלולאה מבצע 1ממשיך אינו
שורה . 2את הרעבה הקריטי הקוד לקטע נוספת פעם ונכנס כללתיתכן !!
Question 2
(40 ) XV6נקודות
24
Question 2
.XV6 שבמערכת proc.c בקובץ sleepלהלן הקוד של פונקציית ה-
25
1. void sleep(void *chan, struct spinlock *lk)
2. {3. if(proc == 0)4. panic("sleep");5. 6. if(lk == 0)7. panic("sleep without lk");8. 9. if(lk != &ptable.lock){ 10. acquire(&ptable.lock); 11. release(lk);12. }13.
14. proc->chan = chan;15. proc->state = SLEEPING;16. sched();17.18. proc->chan = 0;19. 20. if(lk != &ptable.lock){ 21. release(&ptable.lock);22. acquire(lk);23. }24. }
Question 2
26
יחיד spinlock( על יותר מ- acquire יבצע נעילה )processהאם יש אפשרות ש- א. ? אם כן, האם זה יפגע בריצה התקינה של מערכת sleepלפני קריאה לפונקציית
(. נק'15ההפעלה? אם לא, מדוע? נמקו את תשובתכם )
פתרון:•XV6 ה כמות מבחינת מגביל לבצע spinlocksלא ניתן ולכן זמנית בו הנעולים
- מ יותר על לפונקציית spinlockנעילה קריאה לפני .sleepיחידפונקצית • לפונקציית sleepבתיך קריאה ) schedקיימת (. 16שורה)(
שה מוודה זו ה ptable.lock spinlockפונקציה וזהו אשר spinlockנעול היחיד ( ה כמות לוודא ניתן " spinlockנעול פעולות ספירת י ע ו acquireהנעולים
release .)בגלל תעצור הפעלה המערכת מתקיימים אינם התנאים אםפונקציית - panicהפעלת מ. יותר על נעילה הייתה אם יחיד spinlockלכן
לפונקציית קריאה ) sleepלפני שלפחות מכיוון תתקע spinlockהמערכת.) ישוחרר לא אחד
Question 2
27
:) אלטרנטיבי ) פתרוןל • אפשרות - deadlockקיימת מ יותר על נעילה הייתה במידה
spinlock לפונקציית קריאה לפני התהליך. sleepיחיד כי נניחלפונקציית קריאה על P1הוא sleepשביצע נעילה ביצע והוא
2 spinlocks A וB .שפונקציית היותר sleepמכיוון לכל משחררתspinlock " כי כ בה נניח . spinlock Bאחד כן גם נניח שוחרר לא
תהליך " P2כי תהליך " את להעיר זאת, P1צריך לבצע כדי אבלחייב ל. spinlock Bהוא תרחיש תיארנו תהליך ) deadlockכרגע
P1 שתהליך לכך , P2מחכה ותהליך אותו לכך P2יעיר מחכהה P1שתהליך את (spinlock Bישחרר
Question 2
ה- ב. • לשחרור הסיבה )spinlockמהי הישן lk ונעילת )ptable.lock (? הסבירו בשתי שורות כל פעולה 11 ו- 10 )שורות
(. נק'15בנפרד )
28
פתרון:• - ה לשחרור למנוע( lkהישן )spinlockהסיבה \deadlockהיא
) א ) בסעיף הסבר ראה המערכת של נפילה• - ה ב ptable.lockנעילת נתונים עדכון לצורך ptableנועדה
הקריאה( ) 14,15שורות) כלומר המתזמן לכך )(( schedבנוסףזמן מקבל שהוא שברגע הנעילה cpuמצפה את ברשותו יש
- ה .ptable.lockשל spinlockעל
Question 2ה- ג. של בהתנהגות נפגע השורות kernelהאם בין נחליף אם ו- 10 11
יתבצע קודם כאשר יתבצע release(lk))כלומר, מכן ולאחר ,(acquire(&ptable.lock) ידי )על תפגע הנכונות איך בפירוט הסבירו כן, אם ?
תיאור תרחיש שכעת יתבצע בצורה שגויה, או בדרך מפורטת אחרת(. אם לא, (. נק'10הסבירו מדוע ריצת מערכת ההפעלה תישאר תקינה )
29
פתרון: . היא, הסיבה נכונה להיות לא עלולה המערכת התנהגות כן
מבטלת מנעול של : Interruptsשנעילה שהתהליך. אחרי תרחיש- releaseביצע ה את interruptהגיע acquireולפני להעיר שאמור
, מתרחש. לכאורה ההערה של הביצוע ביטול שאין כיוון התהליךבמצב ישאר לעולם התהליך .sleepingאבל
Question 3
(25 ) system callsנקודות
30
Question 3
:נתונה התוכנית הבאה
31
1. #include <stdio.h>2. #include <sys/types.h>3. #include <unistd.h>4. #include <signal.h>5.6. int tst = 0;7.8. int signal_handler(int s)9. {10. signal(SIGINT,signal_handler);12. tst++;13. }14.15. int main()16. {17. int i;18. int p = getpid();19. int l = 0;
20.21. signal(SIGINT,signal_handler);22.23. for (i = 0; i < 4; i++)24. if(fork()==0)25. l++;26.27. if (l == 2)28. kill(p,SIGINT);29.30. while (wait(NULL) > 0);31.32. if (l == 0)33. printf("%d\n", tst);34.35. return 0;36. }
Question 3
ציירו את עץ התהליכים שנוצרים כתוצאה מהרצת הקוד. ציינו א. בכל אחד מהתהליכים בתרשים i; p; lבציור את ערכי המשתנים
(. נק'15שלכם )
32
L=0
L=1
L=2
L=3
L=4
P=1I=4
Question 3
לאי הסיבה מה התוכנית? של האפשריים הפלטים מהם ב. נק'(5הדטרמיניסטיות של הפלטים? )
33
פתרון:{ : האפשריים (. 1,2,3,4,5,6הפלטים ( כלל{ פלט יהיה לא או
, יחיד ביט רק יש אלא נצברים אינם שסיגנלים היא לכך הסיבה , . / / זמן בהקצאת בתלות לכן ים סיגנל של ם הגעת את שמציין ) ( סיגנל שיקבל יתכן העץ שורש הראשון התהליך של הריצה
. יותר או אחד
Question 3
יהיה ד. יגרום לכך שהפלט הציעו שינוי במערכת ההפעלה, אשר ( נק'5דטרמיניסטי? )
34
פתרון:של ברשימה תהליך כל עבור תמיכה שמחכים signalsנוסיף
- ל. ) בדומה (real time signalsלטיפול