Presentation is loading. Please wait.

Presentation is loading. Please wait.

May 6, 2004 1 ICE 1341 – Programming Languages (Lecture #19) In-Young Ko Programming Languages (ICE 1341) Lecture #19 Programming Languages (ICE 1341)

Similar presentations


Presentation on theme: "May 6, 2004 1 ICE 1341 – Programming Languages (Lecture #19) In-Young Ko Programming Languages (ICE 1341) Lecture #19 Programming Languages (ICE 1341)"— Presentation transcript:

1 May 6, 2004 1 ICE 1341 – Programming Languages (Lecture #19) In-Young Ko Programming Languages (ICE 1341) Lecture #19 Programming Languages (ICE 1341) Lecture #19 May 7, 2004 In-Young Ko iko.AT. icu.ac.kr Information and Communications University (ICU) iko.AT. icu.ac.kr

2 May 6, 2004 2 ICE 1341 – Programming Languages (Lecture #19) In-Young Ko Announcements The scores of the homework #4 are accessible from the class website The scores of the homework #4 are accessible from the class website The grading policy of the homework #4 can be accessed via the class BBS The grading policy of the homework #4 can be accessed via the class BBS For the final project, you may need to revise your programming-language design if: For the final project, you may need to revise your programming-language design if: Your language doesn’t support the full language features (variable declarations, arithmetic/logical operations, assignment, branching, iteration and block definitions) Your language doesn’t support the full language features (variable declarations, arithmetic/logical operations, assignment, branching, iteration and block definitions) Your XML syntax is not complete or too complex to use Your XML syntax is not complete or too complex to use

3 May 6, 2004 3 ICE 1341 – Programming Languages (Lecture #19) In-Young Ko Review of the Previous Lectures Object-oriented Programming Concepts and Definitions Object-oriented Programming Concepts and Definitions Overriden Methods Overriden Methods Abstract Classes and Methods Abstract Classes and Methods Design Issues for OOPLs Design Issues for OOPLs Exclusive use of objects in typing systems Exclusive use of objects in typing systems Subclsses as subtypes Subclsses as subtypes Polymorphism and dynamic type checking Polymorphism and dynamic type checking Multiple inheritance Multiple inheritance Object allocation and deallocation mechanisms Object allocation and deallocation mechanisms Object-oriented Processing of XML Data Object-oriented Processing of XML Data

4 May 6, 2004 4 ICE 1341 – Programming Languages (Lecture #19) In-Young Ko Smalltalk – Characteristics The 1 st language to include complete support for the object-oriented programming paradigm The 1 st language to include complete support for the object-oriented programming paradigm Everything is an object Everything is an object All binding of messages to methods is dynamic All binding of messages to methods is dynamic Because all variables are typeless, methods are all polymorphic Because all variables are typeless, methods are all polymorphic All subclasses are subtypes All subclasses are subtypes No multiple inheritance No multiple inheritance

5 May 6, 2004 5 ICE 1341 – Programming Languages (Lecture #19) In-Young Ko Smalltalk – Evaluation The syntax of the language is simple and regular The syntax of the language is simple and regular Slow compared with conventional compiled imperative languages Slow compared with conventional compiled imperative languages Dynamic binding allows type errors to go undetected until run time Dynamic binding allows type errors to go undetected until run time Greatest impact: advancement of OOP Greatest impact: advancement of OOP

6 May 6, 2004 6 ICE 1341 – Programming Languages (Lecture #19) In-Young Ko C++ – Characteristics A hybrid language: procedural + OOP A hybrid language: procedural + OOP Access controls for members: Access controls for members: Private: visible only in the class and friends Private: visible only in the class and friends Protected: visible in the class and in subclasses, but not clients Protected: visible in the class and in subclasses, but not clients Public: visible in subclasses and clients Public: visible in subclasses and clients Disallows subclasses from being subtypes Disallows subclasses from being subtypes Inheritance Inheritance Private derivation – inherited public and protected members are private in the subclasses Private derivation – inherited public and protected members are private in the subclasses Public derivation – public and protected members are also public and protected in subclasses Public derivation – public and protected members are also public and protected in subclasses

7 May 6, 2004 7 ICE 1341 – Programming Languages (Lecture #19) In-Young Ko C++ – Private & Public Derivations class base_class { private: int a;float x; private: int a;float x; protected:int b;float y; protected:int b;float y; public:int c;float z; public:int c;float z;}; class subclass_1 : public base_class { // In this, b and y are protected and c and z are public // In this, b and y are protected and c and z are public}; class subclass_2 : private base_class { // In this, b, y, c, and z are private, and no derived class // In this, b, y, c, and z are private, and no derived class // has access to any member of base_class // has access to any member of base_class}; * AW Lecture Notes

8 May 6, 2004 8 ICE 1341 – Programming Languages (Lecture #19) In-Young Ko C++ – Reexportation A member that is not accessible in a subclass (because of private derivation) can be declared to be visible there using the scope resolution operator ( :: ) A member that is not accessible in a subclass (because of private derivation) can be declared to be visible there using the scope resolution operator ( :: ) e.g., class subclass_3 : private base_class { base_class :: c; base_class :: c; … … } } Motivation for using private derivation: a derived class adds some new members, but does not want its clients to see the members of the parent class, even though they had to be public in the parent class definition Motivation for using private derivation: a derived class adds some new members, but does not want its clients to see the members of the parent class, even though they had to be public in the parent class definition * AW Lecture Notes

9 May 6, 2004 9 ICE 1341 – Programming Languages (Lecture #19) In-Young Ko C++ – Multiple Inheritance If there are two inherited members with the same name, they can both be reference using the scope resolution operator If there are two inherited members with the same name, they can both be reference using the scope resolution operator class A { public: int n; float f; public: int n; float f;}; class B { public: int n; char c; public: int n; char c;}; class C : public A, public B { … f = 10.23; c = ‘$’; f = 10.23; c = ‘$’; A::n = 10; B::n = 20; A::n = 10; B::n = 20;};

10 May 6, 2004 10 ICE 1341 – Programming Languages (Lecture #19) In-Young Ko C++ – Dynamic Binding Virtual method: a method that can be called through polymorphic variables and dynamically bound to messages public class shape { public: virtual void draw() = 0; … public: virtual void draw() = 0; …} public class circle : public shape { public: virtual void draw() { … } … public: virtual void draw() { … } …} public class rectangle : public shape { public: virtual void draw() { … } … public: virtual void draw() { … } …} public class square : public rectangle { public: virtual void draw() { … } … public: virtual void draw() { … } …}… square s; rectangle r; shape &ref_shape = s; ref_shape.draw(); r.draw(); Dynamically bound to draw in square Dynamically bound to draw in rectangle Pure virtual function

11 May 6, 2004 11 ICE 1341 – Programming Languages (Lecture #19) In-Young Ko Java – Characteristics All data are objects except the primitive types All data are objects except the primitive types All primitive types have wrapper classes that store one data value (e.g., new Integer(10)) All primitive types have wrapper classes that store one data value (e.g., new Integer(10)) All objects are heap-dynamic, are referenced through reference variables, and most are allocated with new All objects are heap-dynamic, are referenced through reference variables, and most are allocated with new Single inheritance only, but interfaces provide a version of multiple inheritance Single inheritance only, but interfaces provide a version of multiple inheritance Methods can be final (cannot be overriden) Methods can be final (cannot be overriden) All messages are dynamically bound to methods, unless the method is final All messages are dynamically bound to methods, unless the method is final

12 May 6, 2004 12 ICE 1341 – Programming Languages (Lecture #19) In-Young Ko Implementing OO Constructs Class Instance Record (CIR): A storage structure for the instance variables of class instances Class Instance Record (CIR): A storage structure for the instance variables of class instances Virtual Method Table (VMT): A storage structure for the list of dynamically bound methods that can be called from an instance of a class Virtual Method Table (VMT): A storage structure for the list of dynamically bound methods that can be called from an instance of a class public class A { public int a, b; public int a, b; public void draw() { … } public void draw() { … } public int area() { … } public int area() { … }} Public class B extends A { public int c, d; public int c, d; public void draw() { … } public void draw() { … } public void sift() { … } public void sift() { … }} vtable pointer a b c d CIR for B VMT for B A’s area B’s draw B’s sift

13 May 6, 2004 13 ICE 1341 – Programming Languages (Lecture #19) In-Young Ko Concurrency “A program is said to be concurrent if it contains more than one active execution context” [M. Scott] “A program is said to be concurrent if it contains more than one active execution context” [M. Scott] void concurrentPrint() { (new Thread () { (new Thread () { public void run() { public void run() { while (true) { while (true) { try { System.out.print("A"); sleep(0,1); try { System.out.print("A"); sleep(0,1); } catch (Exception e) { } } catch (Exception e) { } } }).start(); }).start(); (new Thread () { (new Thread () { public void run() { public void run() { while (true) { while (true) { try { System.out.print("B"); sleep(0,1); try { System.out.print("B"); sleep(0,1); } catch (Exception e) { } } catch (Exception e) { } } }).start(); }).start();} Java Forking two concurrent execution threads Main Program Control Printing “A” Printing “B”

14 May 6, 2004 14 ICE 1341 – Programming Languages (Lecture #19) In-Young Ko Categories of Concurrency Thread of control: A sequence of program points reached as control flows through the program Thread of control: A sequence of program points reached as control flows through the program Categories of Concurrency: Categories of Concurrency: Physical concurrency: Concurrency supported multiple independent processors Physical concurrency: Concurrency supported multiple independent processors Logical concurrency: Concurrency presented by time-sharing one processor (software can be designed as if there were multiple threads of control) Logical concurrency: Concurrency presented by time-sharing one processor (software can be designed as if there were multiple threads of control) Coroutines provide only quasi-concurrency Coroutines provide only quasi-concurrency * AW Lecture Notes

15 May 6, 2004 15 ICE 1341 – Programming Languages (Lecture #19) In-Young Ko Subprogram-Level Concurrency (1) Task (Process): A program unit that can be in concurrent execution with other program units Task (Process): A program unit that can be in concurrent execution with other program units  Tasks differ from ordinary subprograms in that:  A task may be implicitly started  When a program unit starts the execution of a task, the program is not necessarily suspended  When a task’s execution is completed, control may not return to the caller void concurrentPrint() { (new Thread () { (new Thread () { public void run() { public void run() { while (true) { while (true) { try { System.out.print("A"); try { System.out.print("A"); } catch (Exception e) { } } catch (Exception e) { } } }).start(); }).start(); (new Thread () { (new Thread () { public void run() { public void run() { while (true) { while (true) { try { System.out.print("B"); try { System.out.print("B"); } catch (Exception e) { } } catch (Exception e) { } } }).start(); }).start();}

16 May 6, 2004 16 ICE 1341 – Programming Languages (Lecture #19) In-Young Ko Subprogram-Level Concurrency (2) A task is disjoint if it does not communicate with or affect the execution of any other task in the program in any way A task is disjoint if it does not communicate with or affect the execution of any other task in the program in any way Task communication is necessary for synchronization Task communication is necessary for synchronization Shared nonlocal variables Shared nonlocal variables Parameters Parameters Message passing Message passing Race Condition: a situation in which more than one tasks are racing to use shared resources Race Condition: a situation in which more than one tasks are racing to use shared resources int count = 0; void concurrentPrint() { (new Thread () { (new Thread () { public void run() { public void run() { try { while (count++ < 20) { try { while (count++ < 20) { System.out.print("A"); System.out.print("A"); sleep(0, 1); sleep(0, 1); } } catch (Exception e) { } } catch (Exception e) { } } }).start(); }).start(); (new Thread () { (new Thread () { public void run() { public void run() { try { while (count++ < 20) { try { while (count++ < 20) { System.out.print("B"); System.out.print("B"); sleep(0, 1); sleep(0, 1); } } catch (Exception e) { } } catch (Exception e) { } } }).start(); }).start();}

17 May 6, 2004 17 ICE 1341 – Programming Languages (Lecture #19) In-Young Ko Semaphores – Dijkstra 1965 class Semaphore { int s = 0; int s = 0; public synchronized void p() { public synchronized void p() { while (s < 0) continue; while (s < 0) continue; s--; s--; } public synchronized void v() { public synchronized void v() { s++; s++; }} int count = 0; Semaphore sem = new Semaphore(); void concurrentPrint() { (new Thread () { public void run() { (new Thread () { public void run() { try { while (true) { try { while (true) { sem.p(); // wait sem.p(); // wait if (count++ >= 20) { v(semaphore); break; } if (count++ >= 20) { v(semaphore); break; } sem.v(); // release sem.v(); // release System.out.print("A"); sleep(0, 1); System.out.print("A"); sleep(0, 1); } } catch (Exception e) { } } } catch (Exception e) { } } }).start(); }).start(); (new Thread () { public void run() { (new Thread () { public void run() { try {while (true) { try {while (true) { sem.p(); // wait sem.p(); // wait if (count++ >= 20) { v(semaphore); break; } if (count++ >= 20) { v(semaphore); break; } sem.v(); // release sem.v(); // release System.out.print(“B"); sleep(0, 1); System.out.print(“B"); sleep(0, 1); } } catch (Exception e) { } } } catch (Exception e) { } } }).start(); }).start();} Semaphore: a data structure consisting of a counter and a queue for storing task descriptors Guarded code Guarded code

18 May 6, 2004 18 ICE 1341 – Programming Languages (Lecture #19) In-Young Ko Synchronization Problems Cooperation Synchronization: synchronization in counting something Cooperation Synchronization: synchronization in counting something e.g., The previous example of sharing a counter, Sharing a buffer by a producer and a consumer Competition Synchronization: synchronization in sharing a resource Competition Synchronization: synchronization in sharing a resource e.g., Dining Philosophers problem Peterson & Silberschatz

19 May 6, 2004 19 ICE 1341 – Programming Languages (Lecture #19) In-Young Ko Dining Philosophers Problem Peterson & Silberschatz Five philosophers spend their lives thinking and eating Five philosophers spend their lives thinking and eating The philosophers share a circular table The philosophers share a circular table In the center of the table, there is a bowl of rice In the center of the table, there is a bowl of rice The table is laid with five chopsticks The table is laid with five chopsticks When a philosopher gets hungry, he tries to pickup two chopsticks that are closest to him When a philosopher gets hungry, he tries to pickup two chopsticks that are closest to him Dijstra 1965

20 May 6, 2004 20 ICE 1341 – Programming Languages (Lecture #19) In-Young Ko Solving The Dining Philosophers Problem Using Semaphores Peterson & Silberschatz Semaphore[] chopsticks = new Semaphore[5]; chopsticks = new Semaphore[5]; chopsticks[0] = new Semaphore(); … while (true) { chopsticks[i].p(); chopsticks[i].p(); chopsticks[(i+1) % 5].p(); chopsticks[(i+1) % 5].p();… // eat … chopsticks[i].v(); chopsticks[i].v(); chopsticks[(i+1) % 5].v(); chopsticks[(i+1) % 5].v();… // think …} 0 1 2 3 4 0 1 2 3 4

21 May 6, 2004 21 ICE 1341 – Programming Languages (Lecture #19) In-Young Ko Evaluation of Semaphores Misuse of semaphores (when a P operation is left out) can cause failures in cooperation synchronization Misuse of semaphores (when a P operation is left out) can cause failures in cooperation synchronization e.g., out of index, buffer overflow Misuse of semaphores (when a V operation is left out) can cause failures in competition synchronization Misuse of semaphores (when a V operation is left out) can cause failures in competition synchronization e.g., infinite loop, deadlock


Download ppt "May 6, 2004 1 ICE 1341 – Programming Languages (Lecture #19) In-Young Ko Programming Languages (ICE 1341) Lecture #19 Programming Languages (ICE 1341)"

Similar presentations


Ads by Google