השאלה הבאה מתייחסת לאלגוריתם הבנקאי שנלמד בהרצאות.
Full original question text (raw OCR)
שאלה 2 - אלגוריתם הבנקאי (25 נק') השאלה הבאה מתייחסת לאלגוריתם הבנקאי שנלמד בהרצאות.
(5 נק') מה מטרתו של אלגוריתם הבנקאי? a. לסנכרן בין שני תהליכים או יותר. b. לפתור את בעיית ה-deadlock לאחר שכבר קרתה. c. לזהות את ה-deadlock שיכול להתרחש במקרה שבקשת קבלת המשאבים הנוכחית תענה. d. לענות האם ייתכן deadlock בהינתן גרף הקצאת המשאבים של התהליכים לפני ריצתם. e. מטרה זהה לזו של אלגוריתם קופת חולים. נימוק:
Mutex correctness and deadlock avoidanceBanker's algorithm safety check(5 נק') מהי ההגדרה של "מצב בטוח" (safe state) באלגוריתם הבנקאי? a. מצב בו תהליכים אינם ב-deadlock b. מצב בו תהליכים אינם ב-deadlock וגם כל בקשת משאבים עתידית לא תגרום ל-deadlock וכל הצרכים למשאבים יסופקו. c. מצב בו רק תהליך אחד משתמש בכל המשאבים שהוא צריך וכל השאר ממתינים. d. מצב בו תהליכים אינם ב-deadlock וקיימת סדרת בקשות משאבים עתידית שלא תגרום ל-deadlock וכל הצרכים למשאבים יסופקו. e. המצב ההתחלתי בלבד של אלגוריתם הבנקאי. נימוק:
Mutex correctness and deadlock avoidanceBanker's algorithm safety check(5 נק') מהי הסיבוכיות של אלגוריתם הבנקאי עבור בקשת משאבים אחת בהנחה שיש n תהליכים? מדוע זאת הסיבוכיות? a. O(n) כי עוברים על כל התהליכים פעם אחת. b. O(n) כי בכל איטרציה עוברים על כל התהליכים שנשארו בקבוצה ומורידים תהליך אחד מהקבוצה (או שעוצרים) עד שהקבוצה מתרוקנת. c. O(n) כי בשתי לולאות for מקוננות, עוברים על כל התהליכים בשביל כל תהליך. d. O(n!) כי עוברים על כל הסידורים האפשריים של n תהליכים. e. אף תשובה אינה נכונה. נימוק:
Mutex correctness and deadlock avoidanceBanker's algorithm safety check(5 נק') מבין שיטות הטיפול ב-deadlock (קיפאון), לאיזו קטגוריה משתייך אלגוריתם הבנקאי? a. Deadlock prevention מניעת קיפאון. b. Deadlock avoidance הימנעות מקיפאון. c. Deadlock detection & recovery זיהוי מצב קיפאון וחילוץ מהמצב. d. Deadlock protection שמירת מצב קיפאון. e. אף תשובה לא נכונה. נימוק:
Mutex correctness and deadlock avoidanceBanker's algorithm safety check(5 נק') ציינו יתרון אחד וחיסרון אחד של אלגוריתם הבנקאי לעומת טכניקת הקצאת משאבים לפי הסדר (circular wait no.). יתרון של אלגוריתם הבנקאי: חיסרון של אלגוריתם הבנקאי:
Mutex correctness and deadlock avoidanceBanker's algorithm safety check
The exam question — original PDF
pages 4, 5, 6Exactly as it appears on the exam paper.
Built from these components
Ordered basic → advanced. Master the earlier ones first.
Review the material
Read these before you answer — each verified slide teaches a component this question tests, and nothing from an unrelated topic is included. Tutorial slides show the actual slide image.
Banker’s algorithm • “Safe state” – System state whereby we’re sure that all processes can be executed, in a certain order, one after the other, such that each will obtain all the resources it needs to complete its execution – By ensuring such a sequence exists after each allocation => we avoid deadlock • Banker’s data structure – max[p] = (m_1,m_2, …, m_k) = max resource requirements for process p – cur[p] = (c_1,c_2, …, c_k) = current resource allocation for process p – R = (r_1, r_2, …, r_k) = the current resource request (for some process p) – avail = (a_1, a_2, …., a_k) = currently available (free) resources (global) • Example – max[p] = (3,0,1), cur[p] = (3,0,0) – Note that max[p] >= cur[p] always holds /* compare by coordinates */ OS (234123) - deadlocks 36
Banker’s algorithm • Tentatively assume that request R was granted to process q – cur[q] += R // vector addition – avail -= R // vector subtraction • Check if “safe state” (= can satisfy all processes in some order) – initialize P to hold all non-terminated process – while( P isn’t empty ) { found = false for each p in P { // find one p that can be satisfied if( max[p] – cur[p] <= avail ) // p’s biggest request avail += cur[p] // pretend p terminates P -= {p} found = true } if( ! found ) return FAILURE } return SUCCESS OS (234123) - deadlocks 37
Summary: ways to deal with deadlocks rpt 1. Deadlock “prevention” – Violate one of the 4 conditions necessary for deadlock – Deadlock can’t happen – E.g., by ordering resources & acquiring from smallest to biggest 2. Deadlock “avoidance” – Banker’s algorithm – Requires full knowledge about available & requested resources – System stays away from deadlocks by being careful on a per resource-allocation decision basis 3. Deadlock “detection & recovery” – Allow system to enter deadlock state, but put in place mechanisms that can detect, and then recover from this situation – Typically refer to the algorithmic resource-graph problem OS (234123) - deadlocks 41
משתנה תנאי (condition variable)משתנה תנאי הוא אובייקט סנכרון המאפשר לחוט לצאת להמתנה בתוך קטע קריטי.כלומר, לפנות את המעבד ולצאת לתור המתנה.ההמתנה תתבצע עד לקיום תנאי כלשהו.ההמתנה מאפשרת לאכוף סדר בביצוע של החוטים. שימוש תכנותי נכון במשתני תנאי מחייב להגדיר גם:משתנה מצב – החוט עובר להמתנה או חוזר מהמתנה בהתאם לערכו של משתנה המצב.מנעול mutex – מבטיח לנו אטומיות והגנה על הקטע הקריטי.מערכות הפעלה - תרגול 86
סכימה כללית למשתני תנאיcond_t c; // should be initializedmutex_t m; // should be initializedint state_var = 0;החוט הממתין לאירוע יקרא ל:while (!condition_holds(state_var)) cond_wait(&c, &m);החוט שמסמן לחוטים הממתינים להמשיך יקרא ל:if (condition_holds(state_var)) cond_signal(&c);מערכות הפעלה - תרגול 87מדוע cond_wait() מקבלתגם את המנעול?
המתנה על משתני תנאיint pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);פעולה: משחררת את המנעול ומעבירה את החוט להמתין על משתנה התנאי באופן אטומי (ראינו קודם מדוע זה הכרחי).החוט הממתין חייב להחזיק במנעול mutex לפני הקריאה.בחזרה מהמתנה על משתנה התנאי, החוט עובר להמתין על המנעול. החוט יחזור מהקריאה ל-pthread_cond_wait() רק לאחר שינעל מחדש את ה-mutex.ערך מוחזר: הפעולה תמיד מצליחה ומחזירה 0.11מערכות הפעלה - תרגול 8
The exam text, the skills it tests, and the exact slides are already in context.