OS PyramidTechnion 234123 · Operating Systemsbasics → exam
Stage 7: Deadlock
2017B_Spring_AQuestion 2core25 pts

הסעיפים הבאים מתייחסים למצב מערכת התחלתי הנתון בתרשים הבא: R2 R1 P1 P2 P3 R3

Full original question text (raw OCR)

שאלה 2 - אלגוריתם הבנקאי (25 נק') הסעיפים הבאים מתייחסים למצב מערכת התחלתי הנתון בתרשים הבא: R2 R1 P1 P2 P3 R3

  1. א· calculation· 6 ptsDeadlock

    (6 נק') השלימו את הטבלה הבאה על-פי אלגוריתם הבנקאי בהנחה שתהליך P1 מבקש לנעול משאב אחד מסוג R1 R=)1,0,0( כלומר ,

    libc syscall wrapper caching pitfallsMutex correctness and deadlock avoidanceBanker's algorithm safety check
  2. ב· short_answer· 6 ptsDeadlock

    (6 נק') האם הדרישה של P1 תסופק על-ידי המערכת? אם כן, האם היא תסופק מיידית? הסבירו

    libc syscall wrapper caching pitfalls
  3. ג· calculation· 7 ptsDeadlock

    הסעיפים הבאים מתייחסים למצב מערכת התחלתי הנתון בתרשים הבא: (שימו לב לשינוי ב-R1, לעומת הסעיפים הקודמים) (7 נק') השלימו את הטבלה הבאה על-פי אלגוריתם הבנקאי בהנחה שתהליך P1 מבקש לנעול משאב אחד מסוג R1, R=)1,0,0( כלומר

    libc syscall wrapper caching pitfalls
  4. ד· short_answer· 6 ptsDeadlock

    (6 נק') האם הדרישה של P1 תסופק על-ידי המערכת? אם כן, האם היא תסופק מיידית? הסבירו

    libc syscall wrapper caching pitfalls

The exam question — original PDF

pages 5, 6

Exactly as it appears on the exam paper.

loading page 5
loading page 6

Built from these components

Ordered basic → advanced. Master the earlier ones first.

L3libc syscall wrapper caching pitfallsL3Mutex correctness and deadlock avoidanceL4Banker's algorithm safety check

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.

Lecture 7slide 36Banker’s algorithm

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

Lecture slide — text above is the material (no raster available).
Lecture 7slide 37Banker’s algorithm

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

Lecture slide — text above is the material (no raster available).
Lecture 7slide 41Summary: ways to deal with deadlocks

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

Lecture slide — text above is the material (no raster available).
Tutorial 2slide 18קריאת המערכת exit()שאלה: למה בכלל לקרוא ל-exit(status) , אם אפשר פשוט לרשום return status בסוף פונקציית ה-main?תשובה:...

קריאת המערכת exit()שאלה: למה בכלל לקרוא ל-exit(status) , אם אפשר פשוט לרשום return status בסוף פונקציית ה-main?תשובה: main היא לא באמת הפונקציה הראשית של התכנית...main() נקראת ע"י __libc_start_main() שאוספת את ערך החזרה של main() וקוראת ל-exit().int __libc_start_main(…) { …… exit(main(…));}מסקנה: הפונקציה exit תמיד נקראת לסיום סטנדרטי של התוכנית.מערכות הפעלה - תרגול 218

Tutorial 2slide 22קריאות המערכת getpid(), getppid()pid_t getpid();קריאת מערכת המחזירה לתהליך הקורא את ה-pid של עצמו

קריאות המערכת getpid(), getppid()pid_t getpid();קריאת מערכת המחזירה לתהליך הקורא את ה-pid של עצמו.pid_t getppid();קריאת מערכת המחזירה את ה-PID של תהליך האב של התהליך הקורא.שאלה: מה המשמעות של getppid() == 1 עבור תהליך משתמש טיפוסי?תשובה: תהליך האב הוא init. קורה למשל אם תהליך הבן יתום.מערכות הפעלה - תרגול 222

Tutorial 2slide 24דוגמת קודמסכמתprintf("pid = %d\n", getpid());pid_t pid = fork();if (pid == 0) { printf("child pid = %d\n", getpid());...

דוגמת קודמסכמתprintf("pid = %d\n", getpid());pid_t pid = fork();if (pid == 0) { printf("child pid = %d\n", getpid()); char* args[] = {"/bin/date", NULL}; execv(args[0], args); printf("This should not be printed\n");} else { wait(NULL); printf("parent pid = %d\n", getpid());}פלט לדוגמה:pid = 8919child pid = 8920Sun Oct 29 00:31:32 IDT 2017parent pid = 8919מערכות הפעלה - תרגול 224

Tutorial 8slide 6משתנה תנאי (condition variable)משתנה תנאי הוא אובייקט סנכרון המאפשר לחוט לצאת להמתנה בתוך קטע קריטי

משתנה תנאי (condition variable)משתנה תנאי הוא אובייקט סנכרון המאפשר לחוט לצאת להמתנה בתוך קטע קריטי.כלומר, לפנות את המעבד ולצאת לתור המתנה.ההמתנה תתבצע עד לקיום תנאי כלשהו.ההמתנה מאפשרת לאכוף סדר בביצוע של החוטים. שימוש תכנותי נכון במשתני תנאי מחייב להגדיר גם:משתנה מצב – החוט עובר להמתנה או חוזר מהמתנה בהתאם לערכו של משתנה המצב.מנעול mutex – מבטיח לנו אטומיות והגנה על הקטע הקריטי.מערכות הפעלה - תרגול 86

Tutorial 8slide 7סכימה כללית למשתני תנאיcond_t c; // should be initializedmutex_t m; // should be initializedint state_var = 0;החוט המ...

סכימה כללית למשתני תנאי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() מקבלתגם את המנעול?

Tutorial 8slide 11המתנה על משתני תנאיint pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);פעולה: משחררת את המנעול ומעביר...

המתנה על משתני תנאיint pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);פעולה: משחררת את המנעול ומעבירה את החוט להמתין על משתנה התנאי באופן אטומי (ראינו קודם מדוע זה הכרחי).החוט הממתין חייב להחזיק במנעול mutex לפני הקריאה.בחזרה מהמתנה על משתנה התנאי, החוט עובר להמתין על המנעול. החוט יחזור מהקריאה ל-pthread_cond_wait() רק לאחר שינעל מחדש את ה-mutex.ערך מוחזר: הפעולה תמיד מצליחה ומחזירה 0.11מערכות הפעלה - תרגול 8

Ask Gemini
2017B_Spring_A · Q2 — question + its material already loaded
Pick a shortcut above or ask anything about this question.
The exam text, the skills it tests, and the exact slides are already in context.