Document 10805479

advertisement
Short Quiz 12 1) Consider Amdahl’s law, what is the speedup of following system, say its parallel computing instructions account for 70 percent of all instructions and they can be shared with 16 processors core. Ans: 1
(1! P) +
P
S
=
1
(1! 0.7) +
0.7
16
=
1
1
=
= 2.9
0.3+ 0.04375 0.34375
2) If we have a class A written in Java public class A { double c = 7.3 public void print_c() { System.out.println(c); } } What is the result if we create 2 process and each process has 2 thread of print_c() ? Ans: print four 7.3 3) What is the outline/challenge of concurrency? Ans: Communication – send or receive information Synchronization – wait for another process to act Atomicity – do not step in the middle and leave a mess 4) Why processes needs to communicate with each other and how they communicate with each other? Ans: the reason Processes may need to communicate 1) Process requires exclusive access to some resources 2) Process need to exchange data with another process There are majorly three ways: a) Shared variables b) Message passing c) Parameters 5) What is Critical Section ? Why we say it implements concept of Atomicity? Ans: In concurrent programming, a critical section is a piece of code that accesses a shared resource (data structure or device) that must not be concurrently accessed by more than one thread of execution.
The “atomic” means that the result of the operation is always valid. Suppose we have two threads: thread A writes to a variable one of the two possible values (0 or 1); thread B reads the variable. If both writes and reads are atomic, then the result of the read operation in thread B can be only 0 or 1 – it is impossible to read a different value from the variable, even if write and read operations are performed at the same time. So to answer the common question about threads : “I need a read/write access to a variable from two or more threads. Should I protect an access to a variable by critical section?” A simple answer is: yes, if you want to be on the safe side always protect the variable by a critical section. 6) Say we have a semaphore S of one resource( e.g. file handle ), if we initialize S with value 2, and the time limit of 5 seconds, consider the following scenario: 5 threads want to read the file and among them 2 get the semaphore, what happens if the 2 thread does not release the handle of semaphore after 5 seconds? Ans: the Scheduler will release the semaphores that 2 thread have and let all 5 threads compete again 7)In Java, when an object is locked, are all methods not visited by other object’s method? How about data fields in the object? What is wait() method for the locked object? What is notify() method for the locked object? What is notifyAll() method for the locked object? What is join() method for the locked object? Ans: No, only the ones with synchronized keyword can not be visited by other objects’ method. All data fields can be visited. Wait() pauses current thread until another thread calls Notify ; Notify() wakes the highest-­‐priority thread closest to the front of the object’s internal queue; notifyAll() wakes up all waiting threads so that: 0 a) Threads non-­‐deterministically compete for access to object b) May not be fair (low-­‐priority threads may never get access) The join method allows one thread to wait for the completion of another. If t is a Thread object whose thread is currently executing, t.join(); causes the current thread to pause execution until t's thread terminates. Overloads of join allow the programmer to specify a waiting period. However, as with sleep, join is dependent on the OS for timing, so you should not assume that join will wait exactly as long as you specify. 8) Briefly explain what is immutable object? What is Thread-­‐Safe class ? Ans: An object is considered immutable if its state cannot change after it is constructed. Take java for example, following rules can define an immutable object: 1. Don't provide "setter" methods — methods that modify fields or objects referred to by fields. 2. Make all fields final and private. 3. Don't allow subclasses to override methods. The simplest way to do this is to declare the class as final. A more sophisticated approach is to make the constructor private and construct instances in factory methods. 4. If the instance fields include references to mutable objects, don't allow those objects to be changed: o Don't provide methods that modify the mutable objects. o Don't share references to the mutable objects. Never store references to external, mutable objects passed to the constructor; if necessary, create copies, and store references to the copies. Similarly, create copies of your internal mutable objects when necessary to avoid returning the originals in your methods. Thread-­‐safety or thread-­‐safe code in Java refers to code which can safely be used or shared in concurrent or multi-­‐threading environment and they will behave as expected. any code, class or object which can behave differently from its contract on concurrent environment is not thread-­‐safe. 9) Briefly explain how to create a Ruby Thread ? !"#$%#&%'"#$(&)*+,-&.'"#$(/,#0&&
Ans: Create thread using Thread.new. method takes code block argument. –  New 1#0&2#%'3(&%$4#*&53(#&67354&$"-)2#,%&
&!"#"$%&'()*+',"-"***./)0"/1"!%&'()***"2"
"!"#"$%&'()*+',"3(&45"-"6"(&4"6"***./)0"/1"!%&'()***"2"" –  83+,&2#%'3(&0$+%*&93"&%'"#$(&%3&532:7#%#&
!*7/8+"
"
;<$2:7#=&
"
"90$%&'()"#"$%&'()*+',"-"
"
""""":;''<"=""""""">":;''<"1/&"=":'?/+)"
!"#$%&'()*+,%
• 
• 
10) Briefly explain what is the difference between synchronous and asynchronous methods in C# ? Ans: In C#, a synchronous method call waits for the method to complete before continuing with program flow, whereas an asynchronous method call will return immediately so that the program can perform other operations while the called method completes its work. 
Download