outline
play

Outline Please check the web page about your recitation group s - PowerPoint PPT Presentation

Outline Please check the web page about your recitation group s meeting place. First group meeting: this Thursday (13:15) Terminology Exceptions Control flow -- the order of execution throw and catch


  1. Outline �� Please check the web page about your recitation group � s meeting place. � First group meeting: this Thursday (13:15) � Terminology � Exceptions � Control flow -- the order of execution � � “throw” and “catch” � – 3 :: 2 – � 0024 Spring 2010 �

  2. Terminology � � � Program -> sequence of operations � � � Translation is done by the compiler � � � We may later see exactly what operations are executed � � � We are for now interested in atomic operations � – 3 :: 3 – � 0024 Spring 2010 �

  3. Java model � – 3 :: 4 – � 0024 Spring 2010 �

  4. Java rules � Simple � “print" – 3 :: 5 – � 0024 Spring 2010 �

  5. Java rules � – 3 :: 6 – � 0024 Spring 2010 �

  6. Java model � – 3 :: 7 – � 0024 Spring 2010 �

  7. Simple Thread class � class TwoThreads { public static void main(String args[]) { SimpleThread st1 = new SimpleThread(); SimpleThread st2 = new SimpleThread(); st1.start(); st2.start(); } /* main */ } – 3 :: 8 – � 0024 Spring 2010 �

  8. Simple Thread class, part 2 � class SimpleThread extends Thread { public void run() { System.out.println(“Started.”); } /* run */ } – 3 :: 9 – � 0024 Spring 2010 �

  9. Variant � class TwoThreads_2 { public static void main(String args[]) { Thread t1 = new Thread(new MyRunnable()); Thread t2 = new Thread(new MyRunnable()); t1.start(); t2.start(); } /* main */ } – 3 :: 10 – � 0024 Spring 2010 �

  10. Variant, part 2 � class MyRunnable implements Runnable { public void run() { System.out.println(“Started.”); } /* run */ } – 3 :: 11 – � 0024 Spring 2010 �

  11. Atomic operations � An activity is called atomic if it completes without the possibility of interruption � int j, k; j = k + 1; � Is this (Java) statement executed atomically? � int j, k; j = j + 1; � Is this (Java) statement executed atomically? � – 3 :: 12 – � 0024 Spring 2010 �

  12. Atomic operations � int j, k; j = j++; � Is this (Java) statement executed atomically? � Who decides what operations are done atomically? � � Ideally, the language designers have specified exactly what operations must be executed atomically. � – 3 :: 13 – � 0024 Spring 2010 �

  13. Atomic operations � For now, we accept that reading or writing an int value is done atomically. � And nothing else. � � � Not completely true. All 32-bit quantities must be written atomically. � � � Some details are missing. � – 3 :: 14 – � 0024 Spring 2010 �

  14. Primitive data types in Java � Java provides a rich set of primitive types � We � ve seen int. Variables, stored in 32 bits. � int j, k; There are “long int” and “double” -- both require 64 bits of storage. � – 3 :: 15 – � 0024 Spring 2010 �

  15. Machine instruction not atomic � (Normal) machine instruction are not atomic � � � Several operations � � � E.g., addl 4(%ebp), %eax // add register and memory � � � Compute effective address (4+ebp) � � � Get value from memory � � � Get value from %eax � � � Add � � � Update %eax � � � Interruption possible after each step � � � Possibility of interruption implies that the machine instruction does not execute atomically. � – 3 :: 16 – � 0024 Spring 2010 �

  16. Why looking at details? � We � ll stay at as high a level as possible but sometimes we must look “under the hood” � � � Most of the time source statements (or even methods) are a good abstraction level � � � When we want to understand parallel execution then we must understand which operations are executed atomically. � � � We � ll later see how we can ensure the atomic execution of sequences of operations (even methods) � – 3 :: 17 – � 0024 Spring 2010 �

  17. Terminology, continued � � � Thread � � � Model of execution of a sequence of operations � � � “Program counter” (PC) + state � � � Program counter - tells us what operation to do next � � � State = local variables (sometimes in registers) � � � Address (data) space � � � Sequence of memory locations � � � Abstract memory cells � � � No information about access time � � � All memory locations have the same protection properties � � � Access rights, kind of address space (read-only, execute, ….) � � � Threads work with one (or more) address spaces � � � Each thread has a private address space for its private data � � � Some of the address spaces may be shared among threads � – 3 :: 18 – � 0024 Spring 2010 �

  18. Terminology, continued � � � Process consists of one (or more) threads and one (or more address spaces) � � � Job: collection of processes � � � (Essential) Operating System (OS) services � � � Thread management � � � Creation, execution, interruption, termination, … � � � Address space management � � � Creation, mapping to real storage, setting/revoking access rights, space reclamation, … � � � Process management � � � Inter-process communication (IPC) � � � Threads communicate via shared address space(s) � – 3 :: 19 – � 0024 Spring 2010 �

  19. Threads and processes � A simple model (but good for many scenarios) � � � 1 thread (“init thread” -- started by OS) � � � 1 address space for private data (i.e. stack) (for init thread) � � � 1 address space for thread (private) instructions � � � 1 address space for shared libraries � � � 1 address space for object instances � A slightly more sophisticated setup � � � 1 thread (“init thread”) -- started by OS � � � 1 .. N-1 threads started by init thread � � � N address spaces for thread-private data � � � N address spaces for thread (private) instructions � � � 1 address space for shared libraries � � � 1 address space for object instances � OS may not use (full) address space but just use different segments � – 3 :: 20 – � 0024 Spring 2010 �

  20. Thread � � � Thread � � � Model of execution of a sequence of operations � � � “Program counter” (PC) + state � � � Program counter - tells us what operation to do next � � � State = local variables � class T1 { � public static void main(String args[]) { int k; k = 0; k = k + 1; } } (at least) 4 Operations: update k, read k, add one, update k � – 3 :: 21 – � 0024 Spring 2010 �

  21. Detour: static methods � class S1 { public int static foo () { return 1; } } class S2 { public void static main(String args[ ]) { System.out.println(S1.foo()); } } � Static methods can be invoked with class name. � � � You don � t need an object instance � – 3 :: 22 – � 0024 Spring 2010 �

  22. Why bother? � Java knows constructors -- special method to produce an instance of a given class. � � � Great if you need one instance. � � � What if you need two instances. Or two instances from different classes. � � � Constructor(s) won � t do the job. � Static methods help in such a situation. � � � Can impose rules on the usage/construction of object instances. � � � Also useful at the start of a program -- when there are no objects yet. � – 3 :: 23 – � 0024 Spring 2010 �

  23. Example revisited � class S1 { public int foo () { /* not static */ return 1; } } class S2 { public void static main(String args[ ]) { int j; S1 sref = new S1(); j = sref.foo() System.out.println(j); } } � – 3 :: 24 – � 0024 Spring 2010 �

  24. Comments � Default constructor -- S1(); � Reference variable to instance of S1 -- sref � “new” operator with constructor X() yields a reference to an instance of class X. � “j” and “sref” are local variables (local to method main) � – 3 :: 25 – � 0024 Spring 2010 �

  25. Operations in example � 1 (or more) to get reference to instance of S1 � � � Plus whatever it takes to produce an instance of S1 � 1 (or more) to access sref � 1 (or more) to invoke foo() � 1 (or more) to get the result from foo() � 1 to update j � 1 (or more) to access System � 1 (or more) to access field out � 1 (or more) to invoke println() � 1 (or more) to return � – 3 :: 26 – � 0024 Spring 2010 �

  26. More comments � Our goal is not to count operations. � Want to understand that many operations are not atomic and that a thread can be interrupted often. � – 3 :: 27 – � 0024 Spring 2010 �

  27. Terminology, continued � � � Address (data) space � � � Sequence of memory locations � � � Abstract memory cells � � � Can store values � » � Don � t worry for now about details � � � No information about access time � � � All memory locations have the same protection properties � � � Access rights, kind of address space (read-only, execute, ….) � � � Not important for us right now -- just for the record � public void static main(String args[ ]) { int j; S1 sref … } Space for j and sref � – 3 :: 28 – � 0024 Spring 2010 �

  28. Data space � S1 sref = new S1(); System will find space to store instance of S1. � sref can be stored, passed as an argument (to a method), or copied. � No other manipulation is allowed. � – 3 :: 29 – � 0024 Spring 2010 �

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend