Monday, January 26, 2015

Chapter 13 Programming Language Concepts R Sebesta

Nama: Stefanus Eduard Adrian
NIM: 1801382963

Kali ini saya akan menjawab Assignment #13 dari Chapter 13 Programming Language Concepts R Sebesta


Review Questions

6. Describe the logical architecture of a vector processor.
*Vector processor have groups of registers that store the operands of a vector operation in which the same instruction is executed on the whole group of operands simultaneously. Originally, the kinds of programs that could most benefit from this architecture were in scientific computation, an area of computing that is often the target of multiprocessor machines.

7. What is the difference between physical and logical concurrency?
*Physical concurrency – Multiple independent processors (multiple threads of control)
Logical concurrency – The appearance of physical concurrency is presented by time-sharing one processor (software can be designed as if there were multiple threads of control)

8. What is a thread of control in a program?
*A thread of control in a program is the sequence of program points reached as control flows through the program.

9. Why are coroutines called quasi-concurrent?
*Because they have a single thread of control.

10. What is a multithreaded program?
*A program designed to have more than one thread of control.


Problem Set

6. Suppose two tasks, A and B, must use the shared variable Buf_Size. Task A adds 2 to Buf_Size, and task B subtracts 1 from it. Assume that such arithmetic operations are done by the three-step process of fetching the current value, performing the arithmetic, and putting the new value back. In the absence of competition synchronization, what sequences of events are possible and what values result from these operations? Assume that the initial value of Buf_Size is 6.
*The idea here is that the add and subtract operations are not atomic, and could be interrupted in mid-operation, when the other task could then run. If A runs to completion, then B runs to completion, Buf_Size has the value 7 (6 + 2 – 1). Similarly if B runs to completion then A runs to completion. If A or B get interrupted in the middle of adding or subtracting, then whichever task finishes last will determine the value in Buf_Size. If A runs but is interrupted after it fetches Buf_Size but before it stores the modified value (allowing B to fetch Buf_Size), or if B runs first and is interrupted after the fetch but before the store, allowing A to fetch Buf_Size, then if A finishes last Buf_Size will have value 8, and if B finishes last Buf_Size will have value 5.

7. Compare the Java competition synchronization mechanism with that of Ada.
*Java methods (but not constructors) can be specified to be synchronized. A synchronized method called through a specific object must complete its execu- tion before any other synchronized method can run on that object. Competition synchronization on an object is implemented by specifying that the methods that access shared data are synchronized.
The competition synchronization mechanism of the Ada Language is intended to provide a facility for tasks to synchronize their actions. Accept and select statements are the two main features of the language that deal with the issue of synchronization This paper points out one major problem that arises in connection with these features and proposes a possible solution to it.

8. Compare the Java cooperation synchronization mechanism with that of Ada.
*Cooperation synchronization in Java is implemented with the wait, notify, and notifyAll methods, all of which are defined in Object, the root class of all Java classes. All classes except Object inherit these methods. Every object has a wait list of all of the threads that have called wait on the object.
In Ada, cooperation synchronization is required between two tasks that when the second task must wait for the first task to finish executing before it may proceed.

9. What happens if a monitor procedure calls another procedure in the same monitor?
*Because the access mechanisms are part of the monitor, implementation of a monitor can be made to guarantee synchro- nized access by allowing only one access at a time. Calls to monitor procedures are implicitly blocked and stored in a queue if the monitor is busy at the time of the call.

10. Explain the relative safety of cooperation synchronization using semaphores and using Ada’s when clauses in tasks.
*As soon as we start using concurrent threads, we need to think about various issues that fall under the broad description of thread safety. Generally, we need to take steps to make sure that different threads don't interact in negative ways:
-if one thread is operating on some data or structure, we don't want another thread to simultaneously operate on that same data/structure and corrupt the results;
-when Thread A writes to a variable that Thread B accesses, we need to make sure that Thread B will actually see the value written by Thread A;
-we don't want one thread to hog, take or lock for too long a resource that other threads need in order to make progress.

Thursday, January 15, 2015

Chapter 12 Programming Language Concepts R Sebesta

Nama: Stefanus Eduard Adrian
NIM: 1801382963

Kali ini saya akan menjawab Assignment #12 dari Chapter 12 Programming Language Concepts R Sebesta


Review Questions

6. Describe a situation where dynamic binding is a great advantage over its absence.
*There is a base class, A, that defines a method draw that draws some figure associated with the base class. A second class, B, is defined as a subclass of A. Objects of this new class also need a draw method that is like that provided by A but a bit different. With overriding, we can directly modify B’s draw function. But without it, we either make a specific function in A for B and inherit it.

7. What is a virtual method?
*A virtual method is a declared class method that allows overriding by a method with the same derived class signature. Virtual methods are tools used to implement the polymorphism feature of an object-oriented language, such as C#. When a virtual object instance method is invoked, the method to be called is determined based on the object's runtime type, which is usually that of the most derived class.

8. What is an abstract method? What is an abstract class?
*An abstract method is a method which all descendant classes should have. An abstract class is a class which has abstract method.

9. Describe briefly the eight design issues used in this chapter for object-oriented languages.
*-What non-objects are in the language?
-Are Subclasses Subtypes? If so, derived objects can be legally used wherever a parent object could be used.
-Type Checking and Polymorphism
-Single and Multiple Inheritance. Inherit from 1 (or more than 1) parent.
-Object Allocation and Deallocation.  Are objects allocated from heap or stack.
-Dynamic and Static Binding. When are messages bound to methods, before or during run-time?
-Nested Classes. Can a class be nested inside another class?
-Initialization of Objects. Are objs init'd when created? Implicit or explicit?

10. What is a nesting class?
*A nesting class is a class defined inside another class.



Problem Set

6. Compare the multiple inheritance of C++ with that provided by interfaces in Java.
*C++ inheritance is implementation inheritance. That is, a class inheriting from two of more superclasses actually inherits the code from those classes. Java’s interface mechanism is an interface inheritance. That is, a class implementing two or more interfaces simply inherits (and must provide its own implementations for) the methods of the interface.

7. What is one programming situation where multiple inheritance has a significant advantage over interfaces?
*When two or more parent classes are derived from one grandparent class and has one child (diamond problem).

8. Explain the two problems with abstract data types that are ameliorated by inheritance.
*The problems solved are reusability of code and "extensibility". Reusability because one won't have to copy/paste his code from one data type to another, allowing for a greater readability. Extensibility because a method can accept a certain class as an argument, and get a child class of this one. This will allow the user to have a wider set of functionality, but the method will still be able to know that the entities it relies on are present.

9. Describe the categories of changes that a subclass can make to its parent class.
*Subclasses can add things (variables, methods). Subclass in C++ can effectively remove a method using "private" inheritance. Inherited methods can be overridden.

10. Explain one disadvantage of inheritance.
*Language & implementation complexity. The shared inheritance problem of multiple inheritance. Subclass is dependent upon its base class (which might change over time).

Chapter 11 Programming Language Concepts R Sebesta

Nama: Stefanus Eduard Adrian
NIM: 1801382963

Kali ini saya akan menjawab Assignment #11 dari Chapter 11 Programming Language Concepts R Sebesta


Review Questions

6. Explain how information hiding is provided in an Ada package.
*There are two approaches to hiding the representation from clients in the package specification. The first one is to include two sections in the package specification, the second one is in which entities are visible to clients and one that hides its contents.

7. To what is the private part of an Ada package specification visible?
*The private part of an Ada package specification is visible to the compiler but not to the client programs.

8. What is the difference between private and limited private types in Ada?
*Private types have built-in operations for assignment and comparison. Limited private types don’t have built-in operations.

9. What is in an Ada package specification? What about a body package?
*Package specification, is an Ada package which provides the interface of the encapsulation (and perhaps more). Body package, is an Ada package which provides the implementation of most, if not all, of the entities named in the associated package specification.

10. What is the use of the Ada with clause?
*The use of Ada with clause is to make the names defined in external packages visible.


Problem Set

6. Discuss the advantages of C# properties, relative to writing accessor methods in C++ or Java.
*One of the advantage of C# properties relative writing accessor methods in C++ or Java is it can control access to the fields.

7. Explain the dangers of C’s approach to encapsulation.
*The main problem is that the biggest part of encapsulation is done via hidding, rather than protection. This hidding is achieved through definition hidding: a header file is preprocessed (which is a synonym for copy-pasted) into the implementation file. Anyone with this header file will be able to access any method or public variable of a the client related to the header, left appart any "static" method / variable. Static is actually the only rue level of protection here, as it's the only one that another unit (file) would not be able to access even if it's aware of it's existence. This whole "protection-is-name-hiding" approach leads to a load of problems: you can access a symbol using the wrong datatype and your compiler will happily do so. To protect critical parts, you should rely on text being hidden from the compiler while it's processing certain units (via #ifdef / #define).

8. Why didn’t C++ eliminate the problems discussed in Problem 7?
*It didn't eliminate the problem because it evolved from C. Hence, it kept a lot of backward compatibility, and the same way of doing basic things. While some problems where solved (like the protected access, which is in-between normal and static in C), some stay, as the symbol access using wrong datatype (inherent to the linker, which doesn't do type-checking).

9. What are the advantages and disadvantages of the Objective-C approach to syntactically distinguishing class methods from instance methods?
*Instance methods use an instance of a class, whereas a class method can be used with just the class name. A class is like the blueprint of a house: You only have one blueprint and (usually) you can't do that much with the blueprint alone. An instance (or an object) is the actual house that you build based on the blueprint: You can build lots of houses from the same blueprint. You can then paint the walls a different color in each of the houses, just as you can independently change the properties of each instance of a class without affecting the other instances.

10. In what ways are the method calls in C++ more or less readable than those of Objective-C?
*In Objective C, all method calls are essentially virtual. This makes it a bit easier on the programmer, but it comes at a possible performance decrease. So sometimes methods call in C++ can be more or less readable than those of Objective-C.