+ All Categories
Home > Documents > Operating Systems, 112

Operating Systems, 112

Date post: 23-Feb-2016
Category:
Upload: zaza
View: 37 times
Download: 0 times
Share this document with a friend
Description:
Operating Systems, 112. Practical Session 11 File Systems & Midterm 2011. 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
Popular Tags:
40
Operating Systems, 112 Practical Session 11 File Systems & Midterm 2011 1
Transcript
Page 1: Operating Systems, 112

Operating Systems, 112

Practical Session 11 File Systems & Midterm 2011

1

Page 2: Operating Systems, 112

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

File system layout (Tanenbaum)

3

Page 4: Operating Systems, 112

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

Quick recap: i-Nodes

General file attributes

The number of hard-links to the file

Usually between 10

and 12

File SizeHardLink count

5

Page 6: Operating Systems, 112

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

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

Question 1: i-nodesA similar problem

8

Page 9: Operating Systems, 112

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

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

Question 3: i-nodesWrite 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, 112

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

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

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

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

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

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,515,000 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

Page 18: Operating Systems, 112

Operating SystemsMIDTERM 2011

Page 19: Operating Systems, 112

Question 1A תהליכים שיבצע עבורם n>1. כתבי קוד עבור נ'( 25א. )•

Barrier Synchronization כלומר, כל התהליכים מגיעים עד .( ומחכים לתהליך האחרון. כאשר מגיע Barrierלמחסום )

, כולם עוברים. כתבי את הקוד כך שלאחר nהתהליך ה-השלמת המעבר של כל התהליכים מתאפשרת הצבת אותו מחסום במקום הבא בקוד לכל אחד מן התהליכים. במלים

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

מסוג counter ובמשתנה out ו- inבשני סמפורים בינאריים integerכל התהליכים יודעים את מספרם המשמש כמונה ..nהכולל

19

Page 20: Operating Systems, 112

Question 1A כבמחסום הכניסה ל- 1 המאותחל ל-inהשתמשי בסמפור הדרכה:

Barrier ובסמפור out-כבמחסום המעבר ל"צד 0 המאותחל ל השני" של המחסום. הניחי כי הקוד מתחיל בשורות הבאות:

 down(in);counter++;........

שורות נוספות. כמובן שנדרש 8 השלימי את הקוד בלא יותר מ-.n להשתמש במספר התהליכים

20

Page 21: Operating Systems, 112

Solution 1Adown(in);counter++;If(counter < n) then

up(in)else

up(out); down(out);counter--;if(counter > 0) then

up(out)else

up(in);

21

Page 22: Operating Systems, 112

Question 1B אשר ראינו בכיתה:Bakeryלהלן הקוד של אלגוריתם ה-

22

int numer[n] initially {0,…, 0}boolean choosing[n] initially {false,…,false}

Code for procss i{1,…,n} 1 choosing[i]:=true2 number[i]:=1+max{number[j] | 1 ≤ j ≤ n}3 choosing[i]:=false4 for j:=1 to n do5 await choosing[j]=false6 await (number[j]=0 OR (number[j], j) (number[i],i))7 od8 Critical Section9 number[i]:=0

Page 23: Operating Systems, 112

Question 1B בקוד לעיל בשורה הבאה:5נניח כי נחליף את שורה

 5 if j<i then await choosing[j]=false

 

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

23

Page 24: Operating Systems, 112

Solution 1B

24

int numer[n] initially {0,…, 0}boolean choosing[n] initially {false,…,false}

Code for procss i{1,…,n} 1 choosing[i]:=true2 number[i]:=1+max{number[j] | 1 ≤ j ≤ n}3 choosing[i]:=false4 for j:=1 to n do5 if j<i then await choosing[j]=false6 await (number[j]=0 OR (number[j], j) (number[i],i))7 od8 Critical Section9 number[i]:=0

max = 0, number[3] = 0P3 :

Page 25: Operating Systems, 112

int numer[n] initially {0,…, 0}boolean choosing[n] initially {false,…,false}

Code for procss i{1,…,n} 1 choosing[i]:=true2 number[i]:=1+max{number[j] | 1 ≤ j ≤ n}3 choosing[i]:=false4 for j:=1 to n do5 if j<i then await choosing[j]=false6 await (number[j]=0 OR (number[j], j) (number[i],i))7 od8 Critical Section9 number[i]:=0

Solution 1B

25

P3 : number[3] = 0, max = 0

P2 : number[2] = 1

Page 26: Operating Systems, 112

int numer[n] initially {0,…, 0}boolean choosing[n] initially {false,…,false}

Code for procss i{1,…,n} 1 choosing[i]:=true2 number[i]:=1+max{number[j] | 1 ≤ j ≤ n}3 choosing[i]:=false4 for j:=1 to n do5 if j<i then await choosing[j]=false6 await (number[j]=0 OR (number[j], j) (number[i],i))7 od8 Critical Section9 number[i]:=0

Solution 1B

26

P3 : number[3] = 0, max = 0

P2 : number[2] = 1

P1 : number[1] = 2

Page 27: Operating Systems, 112

int numer[n] initially {0,…, 0}boolean choosing[n] initially {false,…,false}

Code for procss i{1,…,n} 1 choosing[i]:=true2 number[i]:=1+max{number[j] | 1 ≤ j ≤ n}3 choosing[i]:=false4 for j:=1 to n do5 if j<i then await choosing[j]=false6 await (number[j]=0 OR (number[j], j) (number[i],i))7 od8 Critical Section9 number[i]:=0

Solution 1B

27

P3 : number[3] = 0, max = 0

P2 : number[2] = 0

P1 : number[1] = 2

Page 28: Operating Systems, 112

int numer[n] initially {0,…, 0}boolean choosing[n] initially {false,…,false}

Code for procss i{1,…,n} 1 choosing[i]:=true2 number[i]:=1+max{number[j] | 1 ≤ j ≤ n}3 choosing[i]:=false4 for j:=1 to n do5 if j<i then await choosing[j]=false6 await (number[j]=0 OR (number[j], j) (number[i],i))7 od8 Critical Section9 number[i]:=0

Solution 1B

28

P3 : number[3] = 1

P2 : number[2] = 0

P1 : number[1] = 2

Mutual Exc

lusion

Violation

Page 29: Operating Systems, 112

Question 2

29

int main(){ pthread_t t1, t2;6. int x1 = 1;7. int x2 = 2;8. a++;9. printf("Parent says a:

%d\n", a);10 pthread_create(&t1, NULL, funcZ, (void *)&x1);11 pthread_create(&t2, NULL, funcZ, (void *)&x2);12 pthread_join(t1, NULL);13 pthread_join(t2, NULL);14 printf("Parent says: all done\n");}

Aint a = 0; void* funcZ(void* p) {

1. int i = *(int*)p;2. int b = 0;3. a++; b++;4. printf("child says: id = %d a = %d b = %d\n",i,a,b); // endless loop5. while (1);}

Page 30: Operating Systems, 112

Question 2

30

int main(){6. int x1 = 1;7. int x2 = 2;8. int pid1, pid2;9. a++;10. printf("Parent says a: %d\n“, a);11 if (0==(pid1=fork())) {12 funcZ((void *)&x1); }13 if (0==(pid2=fork())) {14 funcZ((void *)&x2); }15 wait(&status);17 wait(&status);18 printf("Parent says: all done\n");}

B

int a = 0;

void* funcZ(void* p) {1. int i = *(int*)p;2. int b = 0;3. a++; b++;4. printf(“child says: id = %d a = %d b = %d\n”,i,a,b);

// endless loop5. while (1); }

Page 31: Operating Systems, 112

Question 2

31

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

. קיימים – אם הפלט את כתבו אחד אפשרי פלט רק קיים . את – הסבירו מהם שניים כתבו אפשריים פלטים שני לפחות

תשובתכם.

יש במערכת כי אב. preemtionהניחו תהליך כאשר כי הניחו– , ל נכנס הבן תהליך בן תהליך תהליך ready queueיוצר ואילו

, , . החוט חדש חוט יוצר חוט כאשר כי הניחו בדומה ממשיך האב- ל נכנס .ready queueהחדש

.1 ' מסוג תזמון באלג ידוע round robinמשתמשים כאשרשל הקוד שורות כל להרצת מספיקה הזמן שפרוסת

funcZ הוא התהליכים תזמון שסדר נתון כן 'FIFO .(15)וכמו נ 2 ' ' השורות. ובנוסף א מסעיף תזמון באלג משתמשים"while(1) "

. מהתוכניות '(15)נמחקו נ

Page 32: Operating Systems, 112

Solution 2.a

32

פלט יחיד:Aא. עבור תוכנית Parent says a: 1child says: id = 1 a = 2 b = 1child says: id = 2 a = 3 b = 1

פלט יחיד:B עבור תוכנית

Parent says a: 1child says: id = 1 a = 2 b = 1child says: id = 2 a = 2 b = 1

Page 33: Operating Systems, 112

Solution 2.b

33

פלט יחיד:Aב. עבור תוכנית Parent says a: 1child says: id = 1 a = 2 b = 1child says: id = 2 a = 3 b = 1Parent says: all done

Page 34: Operating Systems, 112

Solution 2.b

34

תוכנית :Bעבור אפשריים פלטים מספר Parent says a: 1

child says: id = 1 a = 2 b = 1 child says: id = 2 a = 3 b = 1 Parent says: all done

Parent says: all done child says: id = 2 a = 2 b = 1

Parent says: all done Parent says: all done

Parent says a: 1 child says: id = 1 a = 2 b = 1 child says: id = 2 a = 3 b = 1

child says: id = 2 a = 2 b = 1 Parent says: all done

Parent says: all done Parent says: all done

Parent says: all done

Page 35: Operating Systems, 112

Solution 2.b

35

Parent says a: 1child says: id = 1 a = 2 b = 1child says: id = 2 a = 2 b = 1child says: id = 2 a = 3 b = 1Parent says: all doneParent says: all doneParent says: all doneParent says: all done

Page 36: Operating Systems, 112

Question 3

36

:Xv6) של scheduler פונקציות מרכזיות הלקוחות מתוך קוד המתזמן (3להלן 01 void scheduler(void){02 struct proc *p;03 for(;;){04 sti();05 acquire(&ptable.lock);06 for(p = ptable.proc; p <&ptable.proc[NPROC]; p++){07 if(p->state != RUNNABLE)08 continue;09 proc = p;10 switchuvm(p);11 p->state = RUNNING;12 swtch(&cpu->scheduler, proc->context);13 switchkvm();14 proc = 0;15 } // for16 release(&ptable.lock);17 } // for18 }

Page 37: Operating Systems, 112

Question 3

37

:Xv6) של scheduler פונקציות מרכזיות הלקוחות מתוך קוד המתזמן (3להלן 19 void sched(void){20 int intena;21 if(!holding(&ptable.lock))22 panic("sched ptable.lock");23 if(cpu->ncli != 1)24 panic("sched locks");25 if(proc->state == RUNNING)26 panic("sched running");27 if(readeflags()&FL_IF)28 panic("sched interruptible");29 intena = cpu->intena;30 swtch(&proc->context, cpu->scheduler);31 cpu->intena = intena;32 }33 void yield(void){34 acquire(&ptable.lock); 35 proc->state = RUNNABLE;36 sched();37 release(&ptable.lock);38 }

Page 38: Operating Systems, 112

Question 3

38

?Xv6 איזה אלגוריתם תזמון משמש את נ'(3. )1

-non ומהו אלגוריתם תזמון preemptive הסבירו מהו אלגוריתם תזמון נ'(6. )2preemprtive האם האלגוריתם המשמש את .Xv6 הוא אלגוריתם preemptive נמקו ?

במדויק את תשובתכם.

1. Xv6 uses Round Robin )note that xv6 does not maintain a proper queue and blindly follows array cells. As a result a newly added process is not inserted to the end of the queue and may run before other, already existing processes(.

2. Preemptive scheduling is a scheduling system in which the kernel may interrupt a task and reschedule it for a later time. In non preemptive scheduling switching is done by either explicitly calling a yield function, a blocking operation or when a task is terminated.xv6 uses a preemptive round robin scheduling where each process receives a time quantum of a single tick )defined as 100 msec in timer.c, timerinit)((. The timer interrupt is handled by the kerenel and results in the execution of the yield function described in the question.

Page 39: Operating Systems, 112

Question 3

39

בשאלה הבאה הניחו כי המערכת כבר רצה עם מספר תהליכים בה נ'(16. )3)כלומר, כל תהליך קיבל זמן ריצה לפחות פעם אחת(.

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

לאחר ההחלפה(. האם צדק סטודנט א'? במידה וכן רשמו מהי השורה . אחרת רשמו איזה חלק )או חלקים( בקוד 12שתרוץ לאחר שורה

נ'(8)אחראים על החלפת התהליך. סטודנט ב' חושד שהמימוש הנתון אינו יעיל. לטענתו, המנעול הנתפס 2.

, כלומר רק לאחר שהתהליך 16 משתחרר רק בשורה 5בשורה המתוזמן מסיים את פרוסת הזמן שלו. משמעות הדבר היא שבמערכת

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

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

נ'(

Page 40: Operating Systems, 112

Solution 3.3

40

1. Student A is correct. After line 12 is executed a different process’ state )instruction pointer( is loaded and the next line will be 31.

2. Student B is wrong. After lines 12 is executed, a new process is loaded, and that process continues with its previous flow of execution. That is, execution resumes to lines 31-32 and from there to line 37 which is in charge of releasing the lock )the lock is released by a “different process” than the one acquiring it(.


Recommended