מצליחה. 1. int fork-bomb() 2. { 3. fork(); 4. fork() && fork() fork(); 5. fork(); printf("bla\n"); 8. return 0; 9. }

Similar documents
Fall 2004 CS414 Prelim 1

מבוא למדעי המחשב תרגול 13: עצים בינאריים

רזח יליגרתו םי יראני ב ם

Lecture. DM510 - Operating Systems, Weekly Notes, Week 11/12, 2018

Pre- and post- CS protocols. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 7. Other requirements for a mutual exclusion algorithm

EECE.4810/EECE.5730: Operating Systems Spring 2017 Homework 2 Solution

CS153: Midterm (Winter 19)

CS4411 Intro. to Operating Systems Final Fall points 10 pages

CS4411 Intro. to Operating Systems Exam 1 Fall points 10 pages

PESIT Bangalore South Campus

Part II Process Management Chapter 6: Process Synchronization

שאלה 1, סעיף ב )11 נק'(

Process Synchronization. Mehdi Kargahi School of ECE University of Tehran Spring 2008

CS604 - Operating System Solved Subjective Midterm Papers For Midterm Exam Preparation

Chapter 6: Process Synchronization

Part III Synchronization Software and Hardware Solutions

מבוא לתכנות ב- JAVA תרגול 7

Process Synchronization

CoSc 450: Programming Paradigms. The Critical Section Problem

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

There are 10 total numbered pages, 5 Questions. You have 60 minutes. Budget your time carefully!

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

אוניברסיטת בן-גוריון בהצלחה! מספר נבחן: מדור בחינות רשמו תשובותיכם בשאלון זה בלבד ובמקום המוקצה לכך בלבד! תשובות מחוץ לשאלון לא יבדקו.

Algorithms. Intro2CS week 5

Mutual Exclusion. 1 Formal problem definitions. Time notion CSE /17/2015. Outline of this lecture:

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Dr. Rafiq Zakaria Campus. Maulana Azad College of Arts, Science & Commerce, Aurangabad. Department of Computer Science. Academic Year

Process/Thread Synchronization

Safety and liveness for critical sections

CS110D: PROGRAMMING LANGUAGE I

CS3331 Concurrent Computing Exam 1 Solutions Fall 2018

OS 1 st Exam Name Solution St # (Q1) (19 points) True/False. Circle the appropriate choice (there are no trick questions).

הנכות 1 םוכיס לוגרת 14 1

Process Synchronization. CISC3595, Spring 2015 Dr. Zhang

CSCC 69H3 2/1/13. Today: Deadlock. Remember example from last week? Example of Deadlock. Example: dining philosophers: Not just an OS Problem!

Chapters 5 and 6 Concurrency

Synchronization Spinlocks - Semaphores

Thread Synchronization: Too Much Milk

Chapter 5: Process Synchronization. Operating System Concepts Essentials 2 nd Edition

Synchronization. Before We Begin. Synchronization. Credit/Debit Problem: Race Condition. CSE 120: Principles of Operating Systems.

Synchronization. Before We Begin. Synchronization. Example of a Race Condition. CSE 120: Principles of Operating Systems. Lecture 4.

Memory system behavior: volatile variables. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 6. Volatile variables and concurrency

Mutual Exclusion: Classical Algorithms for Locks

CPS 310 first midterm exam, 2/26/2014

( B ) 4. Which is not able to solve the race condition? (A) Test and Set Lock (B) Shared memory (C) Semaphore (D) Monitor

CSE 120: Principles of Operating Systems. Lecture 4. Synchronization. October 7, Prof. Joe Pasquale

Do not start the test until instructed to do so!

גיליון תשובות מספר נבחן:

Chapter 6: Synchronization. Operating System Concepts 8 th Edition,

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Concurrency: Mutual Exclusion and Synchronization

Chapter 6: Process Synchronization. Operating System Concepts 8 th Edition,

CSCI 447 Operating Systems Filip Jagodzinski

ב ה צ ל ח ה! אוניברסיטת בן גוריון בנגב מספר נבחן : תאריך המבחן: כ"ה תשרי תשע"ח 15/10/17 שמות המורים: ציון סיקסיק מיועד לתלמידי : א'

Operating Systems CMPSC 473. Synchronization February 21, Lecture 11 Instructor: Trent Jaeger

Process Synchronization

Synchronisation algorithms

STUDENT NAME: STUDENT ID: Problem 1 Problem 2 Problem 3 Problem 4 Problem 5 Total

Computing with Infinitely Many Processes under assumptions on concurrency and participation -M.Merritt&G.Taubenfeld. Dean Christakos & Deva Seetharam

Last class: Today: CPU Scheduling. Start synchronization

Chapter 7: Process Synchronization!

CS153: Midterm (Fall 16)

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

CS 361 Concurrent programming Drexel University Fall 2004 Lecture 8. Proof by contradiction. Proof of correctness. Proof of mutual exclusion property

C ONTROL AND H IGHER O RDER F UNCTIONS

PROCESS SYNCHRONIZATION

sample exam Concurrent Programming tda383/dit390 Sample exam March 2016 Time:?? Place: Johanneberg

Suggested Solutions (Midterm Exam October 27, 2005)

Operating Systems. Sina Meraji U of T

מבוא לתכנות ב- JAVA תרגול 5. Ipc161- practical session 5

CROWDMARK. Examination Midterm. Spring 2017 CS 350. Closed Book. Page 1 of 30. University of Waterloo CS350 Midterm Examination.

Introduction to OS Synchronization MOS 2.3

Synchronization. Race Condition. The Critical-Section Problem Solution. The Synchronization Problem. Typical Process P i. Peterson s Solution

Process/Thread Synchronization

Announcements. Office hours. Reading. W office hour will be not starting next week. Chapter 7 (this whole week) CMSC 412 S02 (lect 7)

עמוד 1 (תאריך ( âùéä ער äìàù בכל השאלות ניתן להניח שהקלט תקין. 100 íåëñ חורף :

Synchronization: Semaphores

Synchronization Principles

Process Synchronization

Chapter 6: Synchronization. Chapter 6: Synchronization. 6.1 Background. Part Three - Process Coordination. Consumer. Producer. 6.

Chapter 6: Process Synchronization

Eastern Mediterranean University School of Computing and Technology Department of Information Technology. ITEC202 Operating Systems

Synchronization API of Pthread Mutex: lock, unlock, try_lock CondVar: wait, signal, signal_broadcast. Synchronization

G52CON: Concepts of Concurrency

Lesson 6: Process Synchronization

Chapter 6: Process [& Thread] Synchronization. CSCI [4 6] 730 Operating Systems. Why does cooperation require synchronization?

סעיף ב )11 נקודות( public synchronized void advance(staircase staircase) throws InterruptedException staircase!

Lecture 4: Inter-Process Communication (Jan 27, 2005)

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Repetition Structures

EECE.4810/EECE.5730: Operating Systems Spring Midterm Exam March 8, Name: Section: EECE.4810 (undergraduate) EECE.

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

1. Suppose you are given a magic black box that somehow answers the following decision problem in polynomial time:

Thread Synchronization: Foundations. Properties. Safety properties. Edsger s perspective. Nothing bad happens

Operating Systems Comprehensive Exam. Spring Student ID # 3/16/2006

CS-537: Midterm Exam (Spring 2001)

Control and Environments Fall 2017 Discussion 1: August 30, Control. If statements. Boolean Operators

Find the flaw. Also show a timing diagram that indicates the sequence of actions for several interleavings.

CMSC 330: Organization of Programming Languages. Threads Classic Concurrency Problems

CS370 Operating Systems

Transcription:

שאלה : (4 נקודות) א. ב. ג. (5 נקודות) הגדירו את המונח race-condition במדוייק לא להשמיט פרטים. ספקו דוגמא. (5 נקודות) מהו? Monitor נא לספק הגדרה מלאה. ( נקודות) ( נקודות) ציינו כמה תהליכים יווצרו בקוד הבא כולל התהליך הראשי שאנו מניחים שזה מבצע קריאה ראשונית לפונקציה. במילים אחרות, כמה פעמים תודפס המילה fork() ספקו ניתוח מדוייק של השורות 3,4,5 בקטע הקוד. ניתן להניח שכל קריאה ל? Bla מצליחה.. int fork-bomb() 2. { 3. fork(); 4. fork() && fork() fork(); 5. fork(); 6. 7. printf("bla\n"); 8. return ; 9. שאלה 2: (3 נקודות) כתבו תוכנית ב Java שבה Threads בעלי השמות,,2 כך שסדר ההרצה שלהם בתוכנית הינו: סדר זה חוזר על עצמו לנצח. תשובה שבה critical section שבו thread מדפיס בלולאה את שמו לא תתקבל. שכן סדר הרצה וסדר הדפסה זה לא אותו דבר. בשביל סדר הדפסה מספיק Thread אחד שמדפיס בלולאה את התבנית. אך זה לא מה שנדרש בשאלה. אין להשתמש ב synchronized של Java לשם סינכרון. 2 2 2..

שאלה 3: (3 נקודות) להלן קוד עבור זוג תהליכים. //Shared data boolean flag[2] = {false; integer next = ;// can be or //Code for process ENTRY CODE. flag[] = true; 2 while (flag[]){ 3. if (next == ){ 4. flag[] = false; 5. await(next == ); 6. flag[] = true; <CS> EXIT CODE 7.next = ; 8.flag[] =false; //Code for process ENTRY CODE. flag[] = true; 2. while (flag[]){ 3. if (next == ){ 4. flag[] = false; 5. await(next == ); 6. flag[] = true; <CS> EXIT CODE 7.next = ; 8.flag[] =false; א. ב. ג. ( נקודות) הוכיחו כי האלגוריתם לעיל מספק.mutual exclusion ( נקודות) הוכיחו כי האלגוריתם לעיל הינו.deadlock-free ( נקודות) הוכיחו כי האלגוריתם לעיל הינו.stravation-free בהצלחה! Solution sketch

Question - Part C: The word Bla will be printed 2 times, i.e., this code generates 2 processes in total including the main process. As Bla appears after all fork calls every process generated will print it. Prior to the answer we should recall how a sentence like <condition > && <condition 2> <condition 3> is evaluated in the C programming language (or in any language that you the students have come across so far for that matter). First, let us recall that in a sentence like <condition > && <condition 2> the second condition is evaluated only if the first condition evaluated to true. Next recall that in a sentence like <condition > <condition 2> the second condition is evaluated only if the first one was evaluated to false. Also note that && has precedence to. Now let us combine the two: <condition > && <condition 2> <condition 3> Due to operator precedence C breaks this line as (<condition > && <condition 2> ) <condition 3> Here, we have that if <condition > && <condition 2> evaluates to true then <condition 3> is not evaluated otherwise <condition 3> is evaluated. The term <condition > && <condition 2> is evaluated as we mentioned above. Recall also that fork() essentially returns two values. The parent process gets the PID of the child process while the child process gets. This will result in not all fork() calls to be invoked by each process in accordance to the rules we spoke of above. Let us now rewrite the code as follows int fork-bomb() { fork(); /* A */ (( fork() /* B */ && fork() /* C */ ) fork()); /* D */ fork(); /* E */

printf("bla\n"); return ; The figure on the next page illustrates all the processes that are generated thorough this code. Question 2: The solution in the exam is supposed to be in Java. Here we give pseudo-code. It is your job to translate it into Java. In your solution proper coding in Java is expected, i.e. proper usage of finally clauses and so on. Shared: int turn[3] = {,,; int coins[3] = {,,; int done[3] = {,,; Condition cond[3] = {new Condition(),new Condition(),new Condition(); Code for process i:

int local_cnt = ; while(true){ while(turn[i] == ) cond[i].wait(); if (coins[i] > ){ if (done[i-%3]){ print( Bla ); local_cnt += ; coins[i] -= ; if (coins[i] == ){ coins[i+ % 3] = local_cnt+; local_cnt = ; done[i] = ; done[i+%3] = ; //In each iteration we pass the turn after ONE PRINT ONLY. turn[i] = ; turn[i+ % 3] =; // GIVE UP TURN AND PASS cond[i+ % 3].signal(); Question 3: a) We start with the observation that (*)if process i is in the CS then its flag[i] = true. This is trivial if a process does not enter its inner while loop. On the other hand if it does enter then flag[i] is set to true in line of each process. Assume now towards a contradiction that the claim is false and that the algorithm does not provide mutual exclusion so that there is a point in time in which both and are in the CS. Let t_ be the time in which process reads flag[] == false in the condition of its inner while loop just before entering the CS and let t_ be the point in time in which process reads flag[] == false in the condition of its inner while loop just before entering the CS. As and both are assumed to be in the CS, it follows that t_ and t_ exist. Assume without loss of generality that t_ < t_, i.e., entered the CS before.

From the assumption that t_ < t_, two cases are now possible:. At time t_ process has already executed line 9 in its code and is yet to execute line 2 in its code. In which case next =, by line 8 of process s code. Hence, upon attempt to enter the CS again process will execute the inner while loop as next == it will enter the if clause and will await until next will be changed to by process which only happens when leaves the CS. Hence, and cannot be in the CS at the same time. 2. At time t_ process is in line 6 as it must be after it has set flag[] = and flag[] = true which occur in lines 5 and 7. If process is at line 6 then this is because next ==. Process awaits for next to change to which only happens when leaves the critical section. We again see that cannot be in the CS together with. b) Assume towards contradiction that both and are both found in the entry code forever. In which case both must be inside their inner while loop as the external loop consists of an assignment line and a while loop. As both are found inside their inner while loops the value of the variable is fixed and does not change forever. Assume without loss of generality that next == forever. In which case there exists a point in time in which process enters the if clause found in its inner loop set flag[] == false and waits until next changes to. As a result there exists a point in time in which process is able to pass its inner while loop and enter the CS; contradiction. c) Assume towards a contradiction that process is unable to enter the CS and is consequently stuck forever in its inner while loop. We consider the possible locations for process. As we proved that the algorithm is deadlock free only two cases are possible.. Process is in the remainder code forever. In which case flag[] == false and process will eventually pass its inner while loop. 2. Process is quicker than process - The variable next forces struct alternation between and in case both of them seek to enter the CS. More generally, the last time leave the CS it sets next =. This will cause it to block in line 6 after setting flag[] = false in line 5 which will allow process to pass its inner while loop.