Solutions to Second 4330/6310 Quiz Spring 2014. First question Which of the following statements are true or false (2 points) and why? (3 points)

Slides:



Advertisements
Similar presentations
1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
Advertisements

Concurrency: Mutual Exclusion and Synchronization Chapter 5.
CSC321 Concurrent Programming: §3 The Mutual Exclusion Problem 1 Section 3 The Mutual Exclusion Problem.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Previously… Processes –Process States –Context Switching –Process Queues Threads –Thread Mappings Scheduling –FCFS –SJF –Priority scheduling –Round Robin.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
1 Operating Systems, 122 Practical Session 5, Synchronization 1.
Mutual Exclusion.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
Interprocess Communication
SOLUTIONS FOR THE SECOND 4330 QUIZ Jehan-Francois Paris Summer 2014.
Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 91 Today’s class Mutual exclusion and synchronization 
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Enforcing Mutual Exclusion Message Passing. Peterson’s Algorithm for Processes P0 and P1 void P0() { while( true ) { flag[ 0 ] = false; /* remainder */
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Shared Memory Coordination We will be looking at process coordination using shared memory and busy waiting. –So we don't send messages but read and write.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Synchronization Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
02/17/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
1 Outline Processes Threads Inter-process communication (IPC) Classical IPC problems Scheduling.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Synchronization CSCI 444/544 Operating Systems Fall 2008.
02/19/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
CS4231 Parallel and Distributed Algorithms AY 2006/2007 Semester 2 Lecture 2 (19/01/2006) Instructor: Haifeng YU.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion.
Mutual Exclusion. Readings r Silbershatz: Chapter 6.
6.3 Peterson’s Solution The two processes share two variables: Int turn; Boolean flag[2] The variable turn indicates whose turn it is to enter the critical.
Review Questions on Chapter IV—IPC COSC 4330/6310 Summer 2013.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
More review questions for the second midterm COSC 4330/6310.
CY2003 Computer Systems Lecture 04 Interprocess Communication.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
Thread Implementations; MUTEX Reference on thread implementation –text: Tanenbaum ch. 2.2 Reference on mutual exclusion (MUTEX) –text: Tanenbaum ch
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 11: October 5, 2010 Instructor: Bhuvan Urgaonkar.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
- Manvitha Potluri. Client-Server Communication It can be performed in two ways 1. Client-server communication using TCP 2. Client-server communication.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-5 Process Synchronization Department of Computer Science and Software.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
Operating Systems CMPSC 473 Signals, Introduction to mutual exclusion September 28, Lecture 9 Instructor: Bhuvan Urgaonkar.
1 Processes and Threads Part II Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
1 Critical Section Problem CIS 450 Winter 2003 Professor Jinhua Guo.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
Solutions to the second midterm COSC 4330/6310 Summer 2013.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
“Language Mechanism for Synchronization”
Peculiarity of Peterson’s Solution to Process Synchronization
Concurrency: Mutual Exclusion and Synchronization
Lecture 2 Part 2 Process Synchronization
Critical section problem
Fast Communication and User Level Parallelism
Concurrency: Mutual Exclusion and Process Synchronization
Problems discussed in the review session for the second midterm
First slide Rest of project 2 due next Friday Today:
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
Chapter 6: Synchronization Tools
CSE 542: Operating Systems
Presentation transcript:

Solutions to Second 4330/6310 Quiz Spring 2014

First question Which of the following statements are true or false (2 points) and why? (3 points)

True or false? You cannot combine non-blocking sends with blocking receives.

Answer You cannot combine non-blocking sends with blocking receives.  False, this is the default for BSD socket calls send() and receive().

True or false? Peterson’s algorithm for mutual exclusion assumes the existence of a test-and-set instruction.

Answer Peterson’s algorithm for mutual exclusion assumes the existence of a test-and-set instruction.  False, Peterson’s algorithm makes no assumption about the hardware on which it will run.

True or false? Good programmers always put their notify operations at the end of their monitor procedures.

Answer Good programmers always put their notify operations at the end of their monitor procedures.  False, notify operations can be put anywhere in your code as they never release the monitor.

True or false? In a RPC package, one of the tasks of the user stub is to exchange messages with the user program.

Answer In a RPC package, one of the tasks of the user stub is to exchange messages with the user program.  False, the user stub exchanges messages with the server stub. User Program User Stub Server Stub Server Function Messages

True or false? Messages are reusable resources since they can be forwarded to other computers.

Answer Messages are reusable resources since they can be forwarded to other computers.  False, they are consumed as soon as they have been received.

True or false? Datagrams preserve message boundaries.

Answer Datagrams preserve message boundaries.  True, messages are sent and received individually.

Second question Consider the following solution to the mutual exclusion problem and explain when it fails (5 points) and what happens then. (5 points)

The code shared int reserved[2] = {0, 0};void enter_region(int pid) { int other; other = 1 - pid; // pid of other process reserved[pid] = 1; // reserve while (reserved[other] && reserved[pid]); // busy wait } // enter_region void leave_region(int pid) { reserved[pid] = 0; } // leave_region

The code analyzed shared int reserved[2] = {0, 0};void enter_region(int pid) { int other; other = 1 - pid; // pid of other process reserved[pid] = 1; // reserve while (reserved[other] && reserved[pid]); // busy wait } // enter_region void leave_region(int pid) { reserved[pid] = 0; } // leave_region Reserve first then check Where is the tiebreaker?

Answer When two processes try to enter the critical section in lockstep, a deadlock occurs

Third question Consider the function  void doublesquare(int *pa, int *pb) { *pa *= *pa; *pb *= *pb; } // doublesquare and assume the following calling sequence: alpha = 3; doublesquare (&alpha, &alpha);

Third question (I) Consider the function  void doublesquare(int *pa, int *pb) { *pa *= *pa; *pb *= *pb; } // doublesquare and assume the following calling sequence: alpha = 3; doublesquare (&alpha, &alpha);

Third question (II) What will be the value of alpha after the call assuming that  the call was a conventional procedure call? (5 points)  the call was a remote procedure call? (5 points)

Answer What will be the value of alpha after the call assuming that  the call was a conventional procedure call? (5 points) Answer: alpha = eighty-one  the call was a remote procedure call? (5 points) Answer: alpha = nine

Explanation In a conventional procedure call, the calling program passes to the function doublesquare the two addresses &alpha and &alpha  The function executes in sequence: *(&alpha) *= *(&alpha) // from 3 to 9 *(&alpha) *= *(&alpha) // from 9 to 81  As a result, alpha is squared twice

Explanation In a remote procedure call, the calling program passes to the remove function transfer the two values 3 and 3  The function executes in sequence: 3*3 = 9 3*3 = 9  These two values are returned to the calling program and assigned to alpha in an unspecified order. As a result, alpha is only squared once

Fourth question Give an example of an application where  Datagrams are more indicated than streams. (5 points)  Streams are more indicated than datagrams. (5 points)

Answer Give an example of an application where  Datagrams are more indicated than streams. (5 points) Short requests from a client to a server  Streams are more indicated than datagrams. (5 points) Transferring large files, HTTP (the web)

Fifth question What is the major disadvantage of busy waits? (5 points) What can we do to eliminate them? (5 points) Are there cases where it is better to keep them? (5 points)

Answers  Busy waits waste CPU cycles and cause unnecessary context switches.  We should use instead kernel supported solutions that move the waiting processes to the waiting state.  It is better to keep them in multicore architectures for short waits.

Sixth question A classroom has two doors and one switch at each door to turn the lights on and off. Complete the following template to ensure that a)the room will never contain more than 40 people and b) the light switches will always be on when there are people in the room and off when the room is empty. (5 points per correct line for a total of 25 points)

The function template shared int people_count = 0; semaphore mutex = 1; semaphore room = ______; room(){ _________________________________; people_count++; if(people_count == 1) toggle_switch(); ___________________________________; stay_in_room(); ___________________________________; people_count--; if(people_count == 0) toggle_switch(); ___________________________________ ; } // room

The main idea The problem is a variant of the readers and writers problem discussed in class. We use a mutex to ensure that people enter or leave the classroom one by one. The big difference is that we do here a P(&room) each time a person enter and a V() each time a person leaves the room.

The answer shared int people_count = 0; semaphore mutex = 1; semaphore room = 40 ; room(){ P(&room); P(&mutex); // order does matter people_count++; if(people_count == 1) toggle_switch(); V(&mutex) ; // could have put P(&room) here stay_in_room(); P(&mutex); // could have put V(&room) here people_count--; if(people_count == 0) toggle_switch(); V(&mutex); V(&room); // order does not matter } // room

A last word Your answers may be correct even though they look quite different from the ones that were presented here There are several ways to express the same ideas