1 / 59

הכנה למבחן

הכנה למבחן. סינכרוניזציה. Test-and-set (w) do atomically prev :=w w:=1 return prev. Question Q.5b – midterm 2010. Consider the following simple algorithm similar to that shown in class:

Download Presentation

הכנה למבחן

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. הכנה למבחן

  2. סינכרוניזציה

  3. Test-and-set(w) do atomically prev:=w w:=1 return prev Question Q.5b – midterm 2010 Consider the following simple algorithm similar to that shown in class: Is this algorithm deadlock free? Prove or disprove by providing an accurate scenario which leads to a deadlock. 1 intlock initially 0 2 boolean interested[n] initially {false,…,false} Code for procss i{0,…,n-1} 3 interested[i]:=true 4 await ((test-and-set(lock)=0) OR (interested[i] =false)) 5 Critical Section 6 j:=(i+1) modulo n 7 while (j ≠ i) AND (interested[j] = false) 8 j:=(j+1) modulo n 9 if (j=i) 10 lock:=0 11 else 12 interested[j]:=false 13 interested[i]=false

  4. Answer Q.5b The following scenario results in a deadlock: • A process p0 is executed and executes all lines up until the beginning of line 13.(Note that ‘lock’ is freed) • Another process p1 is executed and allowed to run all the way through the CS. At this point p1 iterates through the other processes and identifies that ‘interested[0]=true’. • As a result p1 resets the value ‘interested[0]:=false’ (line 12) and does not free the lock. It then proceeds to line 13 and exits. • p0 runs line 13. • At this point the system is in a state of deadlock. Any process attempting to enter the CS will be stopped by line 4.

  5. The one-way tunnel problem • One-way tunnel • Allows any number of processes in the same direction • If there is traffic in the opposite direction – have to wait • A special case of readers/writers

  6. The one way tunnel (exam 2004) The one way tunnel solution:int count[2];Semaphore busy=1, mutex=1; Semaphore waiting[2]={1,1}; void arrive(intdirection){ down(&waiting[direction]); down(&mutex); count[direction]+=1; if (count[direction]==1){ up(&mutex); down(&busy); } else up(&mutex); up(&waiting[direction]); } void leave(int direction){ down(&mutex); count[direction]-=1; if (count[direction]==0){ up(&busy); } up(&mutex); }

  7. The one way tunnel (exam 2004) Add changes to the one way tunnel solution so that there will be no starvation. If vehicles are present on both “0” and “1” they will take alternate turns in entering the tunnel. When there are vehicles coming from only one direction, they can pass through with no limitations. Notes: • you may only use integers and binary semaphores (can assume fairness).

  8. The one way tunnel (exam 2004) int count[2]; Semaphore busy = 1, mutex = 1, new_mutex = 1; Semaphore waiting[2] = {1,1};void arrive(int direction) { down(&waiting[direction]); down(&new_mutex);down(&mutex); count[direction] += 1;if(count[direction] == 1) { up(&mutex); down(&busy);{else up(&mutex); up(&new_mutex); up(&waiting[direction]); } void leave(int direction){ down(&mutex); count[direction]-=1; if (count[direction]==0){ up(&busy); } up(&mutex);

  9. One way, convoy (midterm 2008) In the following question you must implement a solution to the convoy problem using only semaphores (and regular variables). In this problem, each thread represent a vehicle. The vehicles must go through a one way tunnel, but unlike the tunnel problem, here vehicles may only cross the tunnel in groups of exactly 5 (all in the same direction). A group of another 5 vehicles (from the same or opposite direction) may cross the tunnel again, only after the previous group of 5 vehicles comes out of it. The general code structure is as follows: • Variable Declaration • PrepareToCross(int direction) • CROSS • DoneWithCrossing(int direction)

  10. One way, convoy (midterm 2008) Implement PrepareToCross() and DoneWithCrossing(). For your implementation you may only use semaphores (counting or binary) and regular variables. No busy-waiting is allowed. We say a thread is passing the tunnel if it completed its call to PrepareToCross and haven’t called DoneWithCrossing or if it is still in PrepareToCross but is no longer waiting on a semaphore, and when it will receive CPU time it may complete the procedure without waiting. Your implementation must satisfy the following conditions: • Mutual Exclusion – threads from different direction may never be in the tunnel at the same time. • Threads may only cross in groups of 5. When the first is leaving PrepareToCross, there are exactly 4 others which are passing the tunnel as well. • Progress – Whenever there are 5 (or more) threads from the same direction waiting to cross the tunnel, then eventually, 5 will.

  11. One way, convoy (midterm 2008) We will use the following: Counting Semaphore waitingToCross[]={5,5} Counting Semaphore barrier[]={0,0} Binary Semaphore busy=1 Binary Semaphore mutex=1 int waitingCount[]={0,0} int passed=0

  12. One way, convoy (midterm 2008) PrepareToCross(int i){ down(&waitingToCross[i]); down(&mutex); waitingCount[i]++; If (waitingCount[i]<5){ up(&mutex); down(&barrier[i]); } else { waitingCount[i]=0; up(&mutex); down(&busy); for (int k=0; k<4; k++) up(&barrier[i]); } up(&waitingToCross[i]); } DoneWithCrossing(int i){ down(&mutex); passed++; if (passed==5){ passed=0; up(&busy); } up(&mutex); }

  13. Java and monitors – Exercise Write a code snippet in Java which will enforce a FIFO waking order (i.e., create a class in Java that will allow a programmer fair synchronization)

  14. Java and monitors – Solution classSafeMonitor {boolean released = false; // this flag avoids race!!!synchronized void await() throws InterruptedException { while (! released) { wait(); }}synchronized void signal(){if (! released){ released = true; notify(); }} }

  15. Java and monitors – Solution public synchronized void leave() { if (!waiting.isEmpty()) { waiting.remove().signal(); } else { busy = false; } } } class CriticalSection { private List<SafeMonitor> waiting; private boolean busy; public CriticalSection() { waiting = new LinkedList<>(); busy = false; } public void enter() { SafeMonitormyLock = null; synchronized(this) { if (! busy) { busy = true; return; } else { myLock =new SafeMonitor(); waiting.add(myLock); } } myLock.await(); }

  16. Java and monitors – Solution public synchronized void leave() { if (!waiting.isEmpty()) { waiting.remove().signal(); } else { busy = false; } } } class CriticalSection { private List<SafeMonitor> waiting; private boolean busy; public CriticalSection() { waiting = new LinkedList<>(); busy = false; } public void enter() { SafeMonitormyLock = null; synchronized(this) { if (! busy) { busy = true; return; } else { myLock =new SafeMonitor(); waiting.add(myLock); } } myLock.await(); } Does this code guarantee a FIFO waking order which is equivalent to the order in which threads reached the critical section entrance? What happens when multiple threads attempt to enter at the same time?

  17. ניהול זיכרון

  18. Question 1 • Assume a 32 bit system, with 2-level page table (page size is 4KB, |p1|=|p2|=10bits, |offset|=12bits). Program “A” on this system requires 12 MB of memory. The bottom 4MB of memory are used by the program text segment, followed by 4MB for data and lastly, the top 4MB for stack. • How many page table pages are actually required for this process. • Describe the lookup within the page tables of address 0x00403004.

  19. Question 1 • We use the following scheme: • The 12 least significant digits in this address, allow access for 212 bytes – 4 KB. • These are pointed to by any of the 210 entries of p2. In total, a second level page table can point to 222 bytes – 4 MB. • Each such page table is pointed to by a first level table entry. • In our case – we require 4 page table pages: a single first level page table (also known as the “directory”), which points to 3 second level page tables. page offset p2 p1 d 10 10 12

  20. Question 1 1023 Top-level page table 1023 4095 page offset page number 4 3 p2 p1 d 2 4 4 1 10 10 12 3 3 0 2 2 1023 1 1 0 0 32 bit virtual address, 4K pages, lookup of 0x00403004 (4,206,596(dec)) 4 3 2 Binary: 0000000001 = 1(dec) 0000000011 = 3(dec) 000000000100 = 4(dec) 1 0 4 – 8 MB 12288 – 16383 Byte

  21. Question 2 Consider a paged memory system with a two-level page table. If the reference time to access the physical memory takes 20 nanoseconds (ns), how long does a paged memory reference take? Assume that the second-level page table is always in memory, and: • There is no TLB, and the needed page is in main memory. • There is a TLB, with access speed of 0.05 ns, the needed page is in main memory and • The TLB does not contain information on this page. • The TLB contains information on this page.

  22. Question 2 • We will need to access the memory three times: in the first and second accesses we will get the first and second level page table entry. This will point us to the physical address we will access next.Total time: 3x20 = 60ns. • Remember we first access the TLB: • Since this entry is not located in the TLB, after examining it, we will revert to the regular lookup scheme (as before). Total time: 0.05+3x20 = 60.05ns. • If the entry is in the TLB, after examining it we will know the location of the exact page frame and access it directly.Total time: 0.05+20 = 20.05ns. Note that the use of virtual memory may significantly reduce memory performance. The TLB provides a mechanism to overcome this problem.

  23. Question 3 Consider a computer with an address space of 32 bits, and a 2KB page size. • What is the size of the page table (single level)? What is the maximal size of a program’s memory? Does it depends on the size of the pages? • Assume a two level page table, in which 8 bits are dedicated for the first level table. What is the size of the 2nd table (per first level entry)? Can we run larger programs now? • Assume that the first level table is always in memory and a page fault occurs with a 4% chance when attempting to access the pages it points to. When retrieving pages pointed to by the second level table a PF occurs in 1% of the attempts. Calculate the average access time to a page, if disk access time is 30x10-6sec, and memory access time is 100x10-9sec.

  24. Question 3 • The virtual memory size is 232 bytes, and the size of each page is 2KB (211 bytes). Total number of pages is therefore 232/211=221 pages. Since each address is 4 bytes long (32 bit machine), we require 4x221 = 223 bytes = 8 MB to hold this table.Maximal program size is 4 GB (size of virtual memory), regardless of the page size.

  25. Question 3 • Using 8 bits for the first level page table, leaves us with 13 bits for the second level page table.The size of the second table is 4x213 = 32KB. The size of the virtual memory stays the same, and we can’t run bigger programs.

  26. Question 4 • What is the size of a single level page table on a 32 bit machine, with 4KB pages? • What is the size of a 64 bit machine’s page table with 4KB pages?How many layers will we need if we want to ensure that each page table entry will require only a single page?

  27. Question 4 • If the address space consists of 232 bytes, with 4KB pages, we have 232/212=220 entries (over 1 million). Using 4 bytes per entry, we get a 4MB page table. • With a 64 bit machine, we need 252 entries. Each entry being 8 bytes long results in a 32 PetaBytes (Peta > Tera > Giga) page table.Limiting page table parts to fit the size of a page means that we can only use 212/23=29 addresses in each table segment. This corresponds to 52/9≈6 levels in this page table. That is, the memory is accessed 6 times to retrieve each virtual address.

  28. Question 5.2 The time required to read an entry from the TLB in a given system is 10 nsec. Access time to a single level page table entry is 10 times slower, and requires 100 nsec. What should be the TLB hit rate so that the average time to find a virtual address will be 30 nsec? Explain your calculation

  29. Question 5.2 The TLB hit rate provides a measure of successful TLB hits when seeking a virtual address. When a successful TLB hit occurs the virtual address is translated directly from the TLB. In contrast, when the page is not in the TLB one has to access the page table. Let p denote the TLB hit rate. We know that: p∙10 + (1-p) ∙(10+100) = 30 Thus, the TLB hit rate should be: p=0.8

  30. קבצים

  31. 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. • Ignore the disk access required in order to load more

  32. Question 1: answer 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.

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

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

  35. Question 3: 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, • single, double & triple indirect

  36. Question 3: 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 • ~= 16G

  37. Question 3: 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 • ~= 4T

  38. וירטואליזציה

  39. Question (Moed B 2017) במערכת וירטואלית ישנו hypervisor התומך בshadow page tables. • תאר בקצרה תהליך חיפוש כתובת וירטואלית במערכת • מה היתרון של מערכת כזו על פני מערכת המשתמשת ב brute force?

  40. Question (Moed B 2017) Guest OS Hypervisor Page table VMM SW Shadow page table Page dir. Interrupt & VMM corrects page table. G-CR3 TLB CPU CR3 HW

  41. Question (Moed B 2017) • במערכת כזו ישנו רגיסטר בCPU המצביע על הShadow page table. בהינתן כתובת וירטואלית של לקוח במערכת כזו ישנן 2 אופציות: • טבלאות הshadow ממפות את הכתובת והתהליך זהה למערכת רגילה • הטבלאות shadow אינן ממפות את הכתובת, יתקבל interruptמסוג pagefault שיעביר אותנו לקוד בhypervisor אשר יבדוק האם הכתובת ממופה בטבלאות הלקוח (ע"י משתנה שישמור את כתובת הטבלה הראשית של הלקוח), אם הכתובת לא ממופה נחזיר page faultללקוח, אחרת נמפה את הדף בטבלאות הshadow כדי שימפה ישירות לזכרון המערכת ונחזור למערכת האורחת (כעת חזרנו למקרה (1)

  42. Question (Moed B 2017) Define these pages as not R/W Guest OS Hypervisor Page table VMM SW VM memory layout Page dir. TLB CPU CR3 HW

  43. Question (Moed B 2017) • במערכת המבצעת גישות brute force סימנו את כל דפי המיפוי כnon read non write ולכן על כל גישה יתקבל interrupt. במערכת מסוג shadow page table נקבל interrupt רק על הגישה הראשונה לדף כלשהו, מהרגע שמיפינו אותו המערכת האורחת יכולה להמשיך לעבוד כרגיל

  44. מועד א' 2018

  45. תהליכים, חוטים ומערכות קבצים (25 נקודות). • בשאלה זו 5 סעיפים. • בסעיפים א-ג נתון כי תוכן הקובץfile.txt הוא ”abcd”. עבור כל אחד מסעיפים אלו ,יש לכתוב במחברת את הפלט של התוכנית הנתונה. אם יוחזר קוד שגיאה כתוצאה מאחת ה-system calls,יש לציין זאת (יש להניח שקיימות הרשאות מתאימות לפתוח את הקובץ ולפיכך הוא ייפתח בהצלחה). • נמקו בקצרה.

  46. תודפס המחרוזת ”ab” ואחריה המחרוזת ”cd”. תהליכים אב ובן חולקים file position משותף של קובץ שנפתח לפני ה- fork.

  47. תודפס המחרוזת ”ab” ואחריה המחרוזת ”ab”. שני התהליכים פותחים את הקובץ לאחר ה- fork ולפיכך אינם חולקים את ה- file position.

  48. תודפס המחרוזת ”ab” (ע"י החוט הבן) ואחריה, כאשר החוט האב ינסה לקרוא, הקריאה תחזיר error code בשל הניסיון של החוט האב לקרוא מן הקובץ לאחר שהחוט הבן כבר סגר אותו. יחד עם זה, הריצה תמשיך ואז גם החוט האב ידפיס ”ab” משום שהוא מדפיס ממשתנים c1, c2 (המשותפים עם החוט הבן) אליהם כבר נכתבו הערכים ‘a’ ו- ‘b’.

  49. ד. (5 נק') במערכת קבצים של UNIX, נמחק בשל תקלה תוכן הקובץ של הספריה/usr/ast/work. ספריה זו אינה מכילה תתי-ספריות, אלא רק 100 קבצים רגילים. נניח שידועים מספרי ה- inodes של הקבצים בספריה. בנוסף, נניח שלכל קובץ בספריה בוצע לפני התקלהsoftlink מקובץ שאינו בספריה. • ענו במחברת על השאלות הבאות ונמקו בקצרה: • האם ניתן לשחזר את הרשימה של שמות כל הקבצים בספריה? • האם בהינתן שם של קובץ כלשהו בספריה, ניתן לשייך לו את מספר ה- inode המתאים באופן ודאי? • ניתן לשחזר את שמות הקבצים משום שה- path של קובץ היעד נשמר כאשר יוצרים אליו softlink (כתוכנו של קובץ ה- softlink). לפיכך ניתן לסרוק את עץ המחיצות ולמצוא softlinks אליו. • לא ניתן לשייך לשמות את ה- inodes בתיקייה שנמחקה, משום שה-softlink אינו שומר את ה- target inode.

  50. ה. (5נק') בהמשך לסעיף ד, נניח שבמקום softlink, לכל קובץ בספריה בוצע לפני התקלה hardlink מקובץ שאינו בספריה. ענו שוב על שתי השאלות, כלומר: • האם ניתן לשחזר את הרשימה של שמות כל הקבצים בספריה? • האם בהינתן שם של קובץ כלשהו בספריה, ניתן לשייך לו את מספר ה- inode המתאים באופן ודאי? • לא ניתן לשחזר לא את שמות הקבצים ולא את ה inodes משום שאין דרך לדעת אילו באילו כניסות של מחיצות אחרות יש hardlinks לקבצים בתיקייה שנמחקה.

More Related