operating systems, 112 practical session 13 past exams 1

43
Operating Systems, 112 Practical Session 13 Past exams 1

Post on 19-Dec-2015

221 views

Category:

Documents


0 download

TRANSCRIPT

Operating Systems, 112

Practical Session 13Past exams

1

Locking Files

• Two or more processes accessing a file together can cause race conditions.

• Locking a whole file is wasteful.• Ability to lock any number of bytes of a file.• Two kinds of locks shared locks, exclusive locks• C Function is int flock(int fd, int operation); • flock is an advisory lock

Locking Files

• flock doesn’t work over NFS• lockf(int fd, int cmd, off_t len)

– Allows locking over NFS by implementing another protocol (Network Lock Manager)

– No shared locking– Can be advisory or mandatory

Question 1 (Moed b 2006)

בהנחה שבתחילת א-התכנית הקובץ

dugma1.txt ,הוא ריק מה תוכנו לאחר סיום

התכנית? הסבירו.

החלף את השורה ב-האדומה על מנת

לקבל תוצאה אחרת והסבר את השוני, בהתייחס למימוש

מערכת הקבצים של UNIX .

נתונה התוכנית:

int main(char ** argv, int argc){ char buf[100]; int fd=open("dugma1.txt",O_WRONLY,0666); if (fork()==0){

int fd2=dup(fd);sleep(1); write (fd2,"I was here second",17);

}//if else{

write (fd,"I was here first",16); }}

Question 1 (Moed b 2006)

1. I was here firstI was here second

2. int fd2=open (“dugma1.txt”,O_WRONLY,0666);output is : I was here second

Why?

Open File Description Table &File Descriptor Table

Parent’s file

descriptors table

Child’s file descriptors

table

Unrelated process’s

file descriptors

table

File positionRWpointer to i-node

File positionRWpointer to i-node

File positionRWpointer to i-node

i-nodes table

Open files description table

6

Question 1 (Moed b 2006) [revised]int main(char ** argv, int argc){

char buf[100];int stat;int fd=open("dugma1.txt",O_WRONLY,0666);if (fork()==0){ int fd2=open("dugma1.txt",O_WRONLY,0666); sleep(1); if (lockf(fd2,F_TLOCK,17)>=0){ write (fd2,"I was here second",17); }}//ifelse{

lockf(fd,F_TLOCK,16); write (fd,"I was here first",16); wait(&stat);

}

}

שינו את הקוד.מה יהיה הפלט עכשיו?

הסבר.

Question 1 (Moed b 2006)

)השתמשנו non-blockingבשורה שהוספנו הוספנו מנעול (. הקובץ יכיל שורה אחת בלבד:F_TLOCKבארגומנט

I was here firstהסיבה לכך היא מכיוון שלא מתבצעת סגירה של הקובץ. אם נשנה

)כלומר כשבודקים את המנעול נמתין עד F_LOCKאת הארגומנט ל .deadlockשישתחרר(, נקבל

ניתן לפתור זאת באחת משתי דרכים: )סגנון רע(waitלוותר על 1.

)באופן כללי תמיד נדאג לסגור קבצים שפתחנו!(.closeלהוסיף 2.

במקרה זה תודפס שורה אחת ובה:

I was here second? DUPומה יקרה כשנשתמש ב-

Exam 2006a, Q1Examine the following solution to the Sleeping barber problem:

#define CHAIRS 5Semaphore customers = 0; barbers = 0; exit = 0; mutex = 1;int waiting = 0;void barber(void) {

while(TRUE){ down(customers); L1

down(mutex); L2

waiting = waiting - 1; up(barbers); L3

up(mutex); L4

cut_hair(); down(exit);}

}

Exam 2006a, Q1void customer(void) {

down(mutex);if(waiting < CHAIRS) {

waiting = waiting + 1;up(customers);up(mutex);down(barbers);get_haircut(); up(exit);

} else

up(mutex); }

Exam 2006a, Q1

A.Specify two conditions required for the correctness of the program, and prove them.

B.What will happen if we replace L1 with L2?

C.What will happen if we replace L3 with L4?

D.Describe the behavior of the program when two barbers are present.

Exam 2006a, Q1

A.Two condition for the validity of the solution are:1. The Barber and a single Customer will execute cut_hair() and

get_haircut() at “the same time”:For a process to enter get_haircut() it must first grab the semaphore barbers. It may only do so after a barber releases it and mutex. Several customers may come and increment customers but they will all stop there. On the other hand the barber may only access cut_hair() after at least one customer came in (and finished incrementing the waiting count). Only then she is allowed to increment the barber semaphore. Since it is possible for many customers to be in the shop at the same time, a barber will not do another hair cut until after the customer left and release exit.

Exam 2006a, Q12. It is easy to see that there is no deadlock:

customer – may only be grabbed by the barber, when she owns no resources or the exit semaphore. Since this can not stop the customer process it may proceed as usual and raise it whenever she comes.Barbers – may only be used by a customer. Every time she will sleep on it, she will have no resources, thus she will not block either barber or another customer from proceeding. Furthermore a customer will sleep on it only after raising customer, thus letting the barber raise barbers at some point later.exit – when a barber sleeps on it, no customers are blocked. The barber will only sleep on it after raising barbers, so at least one customer may come in and raise exit at a later stage.mutex – no process sleeps while grabbing the mutex, so it will never prevent anyone from proceeding. Finally, the semaphores are initialized in such a manner, that a customer may come in and raise customer before sleeping on barbers.

Exam 2006a, Q1

B.If we replace L1 and L2 a deadlock may occur.Consider the following scenario: The barber starts working, it grabs mutex and then sleeps on customer. Now, a customer wants to go into the barber shop, so she tries to grab mutex as well. This fails, because mutex is currently 0 (held by the barber), and the customer sleeps as well.Both customer, and barber sleep forever – deadlock.

Exam 2006a, Q1

C.If we replace L3 and L4 the code will not be affected. It may only allow another customer to enter the shop, but that customer will also have to pass the barbers barrier (which was incremented by 1).

Exam 2006a, Q1

D.The only interesting case is when both barbers are active. In this case when two customers come in both barbers may wake up, and in turn allow the two customer to pass the barbers semaphore. This means that both will be able to enter the CS (get_haircut()).This creates only a minor problem – at this point some sort of function should be written which will let a customer “choose” its preferred barber.

Exam 2006b, Q1

The following is a solution to the dining philosophers problem (with N=5):void philosopher (int i){

while (TRUE){think();pick_stick(i);pick_stick((i+1) % 5);eat();put_stick(i);put_stick((i+1) % 5);

}}

Exam 2006b, Q1

A.Is this solution deadlock free? If it is prove it, otherwise, specify a scenario in which a deadlock occurs.

B.If it was possible to write different code for the different philosophers, is there a valid solution in which each chopstick has a binary semaphore representing it? Write the solution, or prove that it can’t be done.

C.What are the changes required to solve this problem with the banker’s algorithm?

Exam 2006b, Q1

A.This following scenario will result in a deadlock:Philosopher A grabs stick(1). Immediately after him, philosopher B grabs stick(2), philosopher C grabs stick (3), etc’… Now philosopher A attempts to grab stick(2) but fails because B already holds it, so B tries (to grab stick 3) and fails, and so does C, D and E. All philosophers are now waiting for someone to free a stick – deadlock.

(A “classic” circular wait)

Exam 2006b, Q1

B.As seen in class we can define two types of philosophers: “left handed” and “right handed”. A “left handed” philosopher will first grab the left stick, whereas a “right handed” philosopher will grab the right stick first. We can now seat the philosopher according to their type, and as shown in class this will be deadlock free.

Exam 2006b, Q1

B.As seen in class we can define two types of philosophers: “left handed” and “right handed”. A “left handed” philosopher will first grab the left stick, whereas a “right handed” philosopher will grab the right stick first. We can now seat the philosopher according to their type, and as shown in class this will be deadlock free.

Exam 2006b, Q1

C.The dining philosophers is a (classic) case of a deadlock problem.We will need to add a central storage resource with all data, protected by a mutex to solve this problem with the Banker’s algorithm. Each chopstick will receive a resource type, and the requirements of each philosopher will be the two resource type of the chopsticks next to her.Prior to any pick_stick() move by a philosopher, she will run the Banker’s algorithm and verify that this will result in a safe state. If the result is positive she will go on with the move, otherwise, she will release all resources and retry.

Exam 2004a, Q2The following is a solution to the bounded buffer problem with a monitor:

monitor ProducedrConsumercondition full, empty;int count = 0;procedure enter:

if (count == N) thenwait(full);enter_item();count = count + 1;if (count == 1) then signal(empty);

Exam 2004a, Q2

procedure remove:if (count == 0) then

wait(empty);remove_item();count = count – 1;if (count = N-1) then

signal(full);end monitor;

Examine this solution and give a scenario in which the effective buffer size will be 1 and not N.Note: Following a signal, the process will leave the monitor.

Exam 2004a, Q2

Consider the case in which there are N producers, where each one would like to add 2 items to the buffer. First all producers fill the buffer with their first item. Next, each one will sleep on full, because the buffer is filled up.Now the consumer makes a move and empties one item. As a result, a signal is sent and the consumer must leave the monitor.It must now reenter the monitor but has competition from a producer. The producer will add an item and sleep, followed by a consumer consuming an item and exiting, and a producer, etc’…

Exam 2004a, Q5Given the reference string:

1,2,3,4,2,1,5,6,2,1,2,3,7,6,3,2answer the following:a) Write the distance string when using the LRU algorithm. Use the

distance string to determine the amount of page faults when using a physical memory of 4 frames. Should the owner of the system consider enhancing its physical memory to 5 frames?

b) Assume that LRU is approximated with Shift Registers (aging) of 4 bits. These registers are updated once every 4 time units. Show the register state for each frame for time units 1 to 8. Will this implementation’s result it the same as LRU’s? in every case?

c) Describe the implementation of fork() and execvp() in UNIX, which tries to save the maximum amount of free disk space (in the page file).

Exam 2004a, Q5a) The distance string:

PF4 = 7 + 2 = 9PF5 = 7 + 1 = 8

1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2

1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2

1 2 3 4 2 1 5 6 2 1 2 3 7 6 3

1 2 3 4 2 1 5 6 6 1 2 3 7 6

1 1 3 4 2 1 5 5 6 1 2 2 7

3 4 4 4 4 5 6 1 1 1

3 3 3 3 4 5 5 5 5

4 4 4 4

∞ ∞ ∞ ∞ 3 4 ∞ ∞ 4 4 2 6 ∞ 5 3 4

Exam 2004a, Q5b) We will consider the case of “pure aging”, i.e. we ignore page faults, and

whenever a PF occurs we add the relevant information to the first column.

What happens in the first swap PF (requesting page 5)? According to LRU, page 3 should be taken out, however, in our pages 1-4 are suitable candidate!

Page number 0 4 8

1 0000 1000 1100

2 0000 1000 1100

3 0000 1000 0100

4 0000 1000 0100

5 0000 0000 1000

6 0000 0000 1000

Exam 2004a, Q5

c) fork/execvp• fork() – we use COW (Copy On Write) and try to avoid

unnecessary duplication of information. That is, we attempt to avoid creating a bloated memory image from the parent process.

• execvp() – we will examine if the process attempts to run a library program (usually mapped to memory/disk during boot) , and if it is, we use the existing mapping.

Exam 2007a, information systems

The following Fetch-and-Add operation was defined as an atomic operation:

FAA(&s, v){temp = s;s = s+v;return temp;}

Where s is a global variable, defined for all processes, and passed by reference and v is local to the process variable.

Exam 2007a, information systems

Based on this operation the following mutual exclusion code was suggested:

int s = 0, turn = 0; // global varsEnter:

int i = FAA(s,1);while (turn < i);<Critical Section>

Exit:FAA(turn, 1);

Exam 2007a, information systems

1.Does this code guarantee Mutual Exclusion?2.Is it possible to use this code with more than two processes (no

matter if this code is correct or incorrect)?3.What is the state of a process waiting to enter the CS?4.A student suggested replacing FAA with (atomic) TSL defines in

the following way:TSL(&s){

temp = s;s = 1;return temp;

}Does the suggested code still guarantee ME?

Exam 2007a, information systems

1.Yes, it is easy to see that this code guarantees Mutual Exclusion:The first process to run FAA will change the value of s to 1, and have his local variable i filled with 0. Any other process attempting to enter the CS will increment s and its i value will be greater than 1 (s>1). Hence, it will not pass the while statement.Once the first process leaves the CS, the process which incremented s from 1 to 2 (and got i=1) will pass the while loop (because turn=1 is not smaller than i=1).

Exam 2007a, information systems

2.What happens when more than two processes are involved? One might accidently think that this code will not work because the value turn is global (this is not similar to Dekker’s turn variable). However, since each process increments s by 1, all processes have different i values after their initial call to FAA! Which means that only a single process will be “freed” of the while statement whenever another process exits the CS. Hence, this code will work for many processes as well.

Exam 2007a, information systems

3.We note that the while statement provides waiting via busy-wait. That means that a process is not BLOCKED but is still kept in a RUNNING state.

Exam 2007a, information systems

4.Replacing each FAA(s,1) with TSL(s) and FAA(turn, 1) with TSL(turn) can possibly lead to a state where two processes or more are in the CS at the same time: imagine a process entering and leaving the CS. As a result all processes waiting in the while statement, will be able to enter the CS at the same time.The suggestion made by the student is a bad one…

Exam 2009a(.NFSנתונה מערכת קבצים של לינוקס אשר תומכת בשיתוף קבצים )

. הסבירו i-node, v-node, r-nodeא. במערכת קיימים שלושה סוגי מבני נתונים: את תפקידו של כל אחד ממבני הנתונים הללו ואת הקשרים ביניהם .

ב.

נתון קטע הקוד הבא.

int fd=open("data.txt",O_RDONLY,0666);lseek(fd,50,SEEK_SET); // Sets the offset to 50write(fd,buf,150); // Write 150 bytes from buflseek(fd,50,SEEK_SET); // Sets the offset to 50read(fd,buf,150); // Read 150 bytes into buf

יושב על גבי שרת חיצוני )ולא במערכת הקבצים data.txtידוע שהקובץ המקומית(.

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

Exam 2009a - Answersא.

i-nodeמבנה נתונים המתאר קובץ במערכת הקבצים המקומית :

r-node מבנה נתונים אשר נמצא אצל הקליינט ומתאר קובץ במערכת קבצים : מרוחקת

v-node -נמצא בשכבת ה : VFS -ומצביע או ל i-node -או ל r-node

. כלומר, ה"דיבור" עם קבצים v-node"עובדת" רק מול System Call Layerה- יתבצע באותה "שפה" בלי קשר אם הקובץ מקומי או מרוחק )הממשק המוחצן

יתרגם v-node אליו מצביע r-nodeע"י שכבה זו אינו מתייחס למיקום הקבצים(. אשר i-nodeאת הפקודות להודעות עבור שרת מרוחק שעליו יושב הקובץ. ואילו

יתרגם את הפקודות להודעות למערכת הקבצים v-nodeאליו מצביע איזשהו המקומית של מערכת ההפעלה.

Exam 2009a - Answersב.

אשר תחזיר לקליינט lookup לשרת, אלא פקודות open לא תשלח פקודת :1שורה filehandle -לאחר קבלת ה .filehandle הקליינט ייצר v-node ו r-node -כאשר ה

v-node -מצביע לr-node -וה r-node -מחזיק את ה filehandle התקבלו 1. לשורה וגם FD נשלח והשרת מחזיר openגם התשובות שבהן הנבחן רשם שה-

stateless לא נשלח בגלל שהשרת הוא openהתשובות שבהן הנבחן רשם שה- של הקובץ נמצא אצל offset ולכן מידע כגון ה stateless השרת הוא :2שורה

תמיד תתבצע בצד של הקליינט בלבד.lseekהקליינט. כלומר פקודת

ולכן מידע כגון הרשאות כתיבה/קריאה של הקובץ stateless השרת הוא :3שורה הקליינט יזהה שאסור RD_ONLYנמצאות אצל הקליינט. מכיוון שהקובץ נפתח כ-

לא תשלח לשרת.writeלבצע פעולת כתיבה לקובץ. ולכן פקודת ה-

.2 ראה הסבר לשורה :4שורה

מכיוון שהתוכן של הקובץ יושב אצל השרת )ולא אצל הקליינט( פקודת ה- :5שורה read הביטים מ- 150 תשלח לשרת ותבקש את offset 50.

Exam 2009aג. נתון קטע הקוד הבא.

int fd=open("data.txt",O_RDONLY,0666);lseek(fd,0,SEEK_SET); // Sets the offset to 0read(fd,buf,500);read(fd,buf,500);read(fd,buf,1000);

והשרת שולח לקליינט בלוקים בגודל NFS cachingנתון גם כי לקליינט יש שכבת .KB 1קבוע של

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

Exam 2009a - Answersג.

יכולה להחזיר יותר מידע ממה שהקליינט ביקש. כאשר readהערה: בשאלה זו פקודת ה- המוטיבציה מאחורי קבלת מידע "נוסף", היא שסביר להניח שבאיזשהו שלב הקליינט

אשר בה יאוחסן המידע יכולה cacheירצה לקרוא את המידע הנוסף הזה. שכבת ה- לחסוך גישות לשרת.

בסעיף ב'(1)ראה הסבר לשורה : 1שורה

בסעיף ב'(2)ראה הסבר לשורה : 2שורה

ריק ולכן עליו לקבל את תוכן הקובץ מהשרת. פקודת cacheהקליינט יראה שה- : 3שורה אפס. מכיוון שנתון שהשרת offset הביטים מ- 500 תשלח לשרת ותבקש את readה-

1024 עד 0 הקליינט יקבל חזרה את תוכן הקובץ מ- KB 1מחזיר תשובות בבלוקים של cacheבייט ויאכסן את המידע ב-

בייט. הפעם הקליינט יגלה 1000 עד 500כעת הקליינט רוצה את תוכן הקובץ מ- : 4שורה לא תישלח לשרת, אלה read ולכן פקודת ה- cacheשהמידע שהוא צריך יושב ב-

cacheתילקח המידע ישירות מה-

יש cache בייט. אולם ל- 2000 עד 1000כעת הקליינט רוצה את תוכן הקובץ מ – : 5שורה תישלח לשרת.read ולכן הפעם פקודת ה- 1024רק את המידע עד הבייט ה-

Exam 2009a לכתיבה data.txtד. נתונים שני תהליכים. התהליך הראשון פותח את הקובץ

ולקריאה. התהליך השני מנסה למחוק את הקובץ בעודו פתוח ע"י התהליך הראשון.

. בהנחה ששני התהליכים רצים על מחשבים שונים )שני קליינטים שונים של 1השרת החיצוני(. האם המחיקה תצליח? נמקו בקצרה.

. בהנחה ששני התהליכים רצים על אותו מחשב )קליינט של השרת החיצוני(. 2האם המחיקה תצליח? נמקו בקצרה

Exam 2009a - Answersד.

ולכן אין לו שום מידע לגבי איזה תהליכים פתחו את statelessהשרת הוא 1.. בהנחה שיש לקליינט הרשאות מתאימות, המחיקה data.txtהקובץ תצליח.

מכיוון ששני התהליכים רצים על אותה מערכת הפעלה באותו מחשב, 2.מערכת ההפעלה של הקליינט יכולה לזהות שהקובץ פתוח )הקליינט הוא

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

מספק, קיבל את מלוא הנקודות.