שאלה : (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.