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

Slides:



Advertisements
Similar presentations
Object-Oriented Programming Session 9 Course : T Programming Language Concept Year : February 2011.
Advertisements

Operating Systems: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
Lecture 10: Part 1: OO Issues CS 540 George Mason University.
ICE1341 Programming Languages Spring 2005 Lecture #19 Lecture #19 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
ISBN Chapter 12 Support for Object-Oriented Programming.
Software Productivity
Chapter 12 Support for Object-Oriented Programming.
Chapter 12: Support for Object-Oriented Programming
Object-Oriented Programming CS 3360 Spring 2012 Sec , Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison.
ISBN Chapter 12 Support for Object-Oriented Programming.
Object-Oriented Programming
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Concurrency - 1 Tasking Concurrent Programming Declaration, creation, activation, termination Synchronization and communication Time and delays conditional.
1 Chapter 12 © 2002 by Addison Wesley Longman, Inc Introduction - Categories of languages that support OOP: 1. OOP support is added to an existing.
ISBN Chapter 12 Support for Object- Oriented Programming.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 12 Topics Introduction Object-Oriented Programming.
Support for Object-Oriented Programming
Chapter 12 Support for Object-Oriented Programming.
CPS 506 Comparative Programming Languages
(11.1) COEN Data Abstraction and OOP  Data Abstraction – Problems with subprogram abstraction – Encapsulation – Data abstraction – Language issues.
1 Chapter 11 Abstract Data Types and Encapsulation Constructs.
ISBN Chapter 12 Support for Object-Oriented Programming.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
CS 403 – Programming Languages Class 25 November 28, 2000.
Object-Oriented Programming Session 9 Course : T Programming Language Concept Year : February 2011.
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
The Procedure Abstraction, Part V: Support for OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in.
April 23, ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Programming Languages (ICE 1341) Lecture #16 Programming Languages (ICE 1341)
CPS 506 Comparative Programming Languages
Introduction to Object Oriented Programming CMSC 331.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 12 Concurrency can occur at four levels: 1. Machine instruction level 2. High-level language.
Lecture 13 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
1 Concurrency Architecture Types Tasks Synchronization –Semaphores –Monitors –Message Passing Concurrency in Ada Java Threads.
Chapter 12 Support for Object oriented Programming.
Chapter 12 Support for Object-Oriented Programming.
Information System Design (IT60105) Lecture 26 Object-Oriented System Testing.
ISBN Chapter 12 Support for Object-Oriented Programming.
Chapter 12 Support for Object-Oriented Programming.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
ISBN Chapter 12 Support for Object-Oriented Programming.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Chapter 12 Support for Object-Oriented Programming.
Object-Oriented Programming Chapter Chapter
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 11 Categories of languages that support OOP: 1. OOP support is added to an existing language.
ISBN Object-Oriented Programming Chapter Chapter
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
C++ General Characteristics: - Mixed typing system - Constructors and destructors - Elaborate access controls to class entities.
ISBN Chapter 12 Support for Object-Oriented Programming.
Support for Object-Oriented Programming
12.1 Introduction - Categories of languages that support OOP:
Support for Object-Oriented Programming
Support for Object-Oriented Programming
CMP 339/692 Programming Languages Day 24 Tuesday May 1, 2012
Lecture 12 Concepts of Programming Languages
Support for Object-Oriented Programming
Support for Object-Oriented Programming
Object-Oriented Programming
Support for Object-Oriented Programming
Final Review In Text: Chapters 1-3, 5-16.
Support for Object-Oriented Programming
Subject : T0152 – Programming Language Concept
Support for Object-Oriented Programming
Support for Object-Oriented Programming
Final Review In Text: Chapters 1-3, 5-16.
Lecture 10 Concepts of Programming Languages
Chapter 12 Categories of languages that support OOP:
Presentation transcript:

May 6, 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

May 6, 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

May 6, 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

May 6, 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

May 6, 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

May 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

May 6, 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

May 6, 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

May 6, 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;};

May 6, 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

May 6, 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

May 6, 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

May 6, 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”

May 6, 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

May 6, 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();}

May 6, 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();}

May 6, 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

May 6, 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

May 6, 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

May 6, 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 …}

May 6, 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