Concurrent Programming Concurrent Programming

Concurrent Programming Concurrent Programming

05/01/2017 Today Comp 104: Operating Systems Concepts • Introduction to Concurrent Programming – Threads – Java Threads and Realisation Concurrent Programming & Threads 1 2 Concurrent Programming Concurrent Programming • Consider a program that resolves an arithmetic • Consider formula to find one root of a expression quadratic: x = (-b + √(b 2 - 4ac) ) / 2a • The steps in the calculation are performed serially: instructions are executed one step at a time • This can be split into several operations: – every operation within the expression is evaluated in sequence following the order dictated by the programmer (and compiler) Concurrent operations Serial operations 1. t1 = -b 5. t5 = t3*c 2. t2 = b*b 6. t5 = t2 - t5 • However, it may be possible to evaluate numerous 3. t3 = 4*a 7. t5 = √t5 sub-expressions at the same time in a multiprocessing 4. t4 = 2*a 8. t5 = t1 + t5 system 9. x = t5/t4 • In Java, parallel execution is achieved with 3 threads 4 1 05/01/2017 Exercise Question • In calculating the formula ut + ½at 2 using maximal concurrency, • Identify the parallelism in the following: which of the operations might be computed in parallel? a) u*t; a/2; t*t 1) for i = 1 to 10 b) u*t; t+½; a*t a[i] = a[i] + 1 c) u+a; t*t d) u+a; t*t; ½ e) no parallelism is possible for this formula 2) for i = 1 to 10 a[i] = a[i] + a[i - 1] Answer: a u*t; a/2; and t*t – i.e. only those parts of the formula that have no dependencies on other parts of the formula can be run concurrently. Think how the formula could be written in 3-code" 5 6 Threads Thread Benefits • Four major categories: • A thread can be thought of as a lightweight process • Responsiveness: In a multithreaded interactive application a program • Threads are created within a normal (heavyweight) may be able to continue executing, even if part of it is blocked – e.g. in a web browser: user waiting for image to download, but can still process interact with another part of the page • Example 1: a Web browser – one thread for retrieving data from Internet • Resource sharing: Threads share memory and resources of the process they belong to, thus we have several threads all within the same address – another thread displays text and images space • Example 2: a word processor – one thread for display • Economy: Threads are more economical to create and context switch as they share the resources of the processes to which they belong – one for reading keystrokes – one for spell checking • Utilisation of multiprocessor architectures: In a multiprocessor architecture, where each thread may be running in parallel on a different processor, the benefits of multithreading can be increased 7 8 2 05/01/2017 Thread Types Thread Types • Support for threads may be provided either at the user • Kernel level: Threads are supported directly by the OS level, for user threads, or at the kernel level, for kernel threads – Thread creation, scheduling and management done by the kernel in the kernel space • User level: Threads are supported above the kernel and implemented at the user level by a thread library – Slow to create and manage – Library provides support for thread creation, scheduling and management, with no support from the kernel – But, since the threads are managed by the kernel, if one thread performs a blocking system call, the kernel can schedule another thread within the application to run – User level threads are fast to create and manage – But, if kernel is single threaded, one thread performing a – In a multiprocessor system, kernel can schedule threads on blocking system call will cause the entire process to block, different processors even if other threads within the process can run 9 10 Java Thread Creation The Java Thread Class • When a Java program starts, a single thread • public class Thread extends Object is created implements Runnable – JVM also has own threads for garbage collection, screen updates, event handling etc. • A Thread describes a “Run” method • New threads may be created by extending that defines what processing will be the Thread class carried out during the Thread’s lifetime. • Again, threads may be managed directly by • Threads may be started within main(), kernel, or implemented at user level by a and run simultaneously, sharing library variables, etc. 3 05/01/2017 A Basic Java Thread Class and how it might be used class TwoChar extends Thread { public class ThreadEx { private char[2] Out; public static void main(String args[]) { public TwoChar(char First,Second) { Out[0]=First; Out[1]=Second; // Thread declaration }; TwoChar LET = new TwoChar(‘A’,’B’); public void run() { TwoChar DIG = new TwoChar(‘1’,’2’); System.out.println(Out[0]); System.out.println(Out[1]); LET.start(); DIG.start(); }; }; } } Thread Methods I Thread Methods II ThreadName.start() ThreadName.sleep(int millis) Causes the Thread, Threadname, to begin Causes the Thread, Threadname, to sleep executing (ie calls the run() method in its (ie temporalily stop) executing for millis specification) milliseconds. 1. There is no limit (other than machine Execution resumes from exactly the point resource) on the total number of Threads where the thread suspended: ie if X.run() that may simultaneously run. contains 2. Concurrently running threads may access y++; sleep(5000); z++; and alter common variables. after y++ and suspension z++ is the next 3. Because of the potential for undesirable operation performed (the run() method does side-effects from (2), support is offered to not restart) allow this to happen in a “controlled” style. 4 05/01/2017 Problem Question!!! • Suppose we have an object (called ‘thing’) • What value will ‘count’ have afterwards? which has the following method: public void inc() { count = count + 1; } • Count is private to ‘thing’, and is initially zero • Two threads, T1 and T2, both execute the following: thing.inc(); Answer Question • Which of the following statements about threads is FALSE? • We don’t know! a) A Java program need not necessarily lead to the creation of any threads b) A thread is sometimes referred to as a lightweight process • This is called indeterminacy c) Threads share code and data access d) Threads share access to open files • If T1 executes assignment before T2, or e) Threads are usually more efficient than conventional processes vice-versa, then count will have value 2 • Similarly: what will be the output produced Answer: a Every Java program starts as a thread! The rest of the statements by running ThreadEx the example multi- are trueV thread earlier? 20 5 05/01/2017 Java Thread States Java Thread States • A Java thread can be in one of four possible states: runnable termination start() • New: when an object for the thread is created (i.e. through use of I/O the ‘new’ statement) new dead new sleep() etc • Runnable: when the thread’s run() method is invoked it moves from the new state to the runnable state, where it is eligible to be run by blocked the JVM • Blocked: when performing I/O the thread becomes blocked, and also when it invokes specific Thread methods, such as sleep() (or, as a • All threads capable of execution are in the consequence of invoking suspend(): a method now deprecated) runnable state • Dead: when the thread’s run() method terminates (or when its stop() – Includes currently executing thread method is called – stop() is also deprecated), the thread moves to the dead state. Question Comp 104: Operating Systems • A Java object called ‘helper’ contains public void addone() { the two methods opposite, where num num = num + 1; Concepts is an integer variable that is private to } helper. Its value is initially 100. public void subone() { • One thread makes the call num = num - 1; – helper.addone(); } • At the same time, another thread makes the call a) 100 – helper.subone(); b) 99 Synchronisation c) 101 • What value will num have afterwards? d) either 99 or 101, but not 100 e) the value of num is undefined Answer: d either 99 or 101, but not 100 – if the two threads are run simultaneously, then it depends on the order in which the threads are executed by the ready queue. However, as “num” is not protected by a semaphore, its final value could be either value 23 6 05/01/2017 Today Problem • Mutual Exclusion • Suppose we have an object (called ‘thing’) • Synchronisation methods: which has the following method: public void inc() { – Semaphores count = count + 1; } • Classic synchronisation problems – The readers-writers (Producer-Consumer) • Count is private to ‘thing’, and is initially zero Problem • Two threads, T1 and T2, both execute the – The Dining Philosophers Problem following: thing.inc(); Mutual Exclusion Semaphores • Indeterminacy arises because of possible simultaneous access • A semaphore is an integer-valued variable that to a shared resource – The variable ‘count’ in the example is used as a flag to signal when a resource is free and can be accessed • Solution is to allow only one thread to access ‘count’ at any one time; all others must be excluded • Only two operations possible: wait(S) also called • To control access to such a shared resource we declare the section of code in which the thread/process accesses the P and signal(S) also called V (from Dutch, resource to be the critical region/section proberen and verhogen – proposed by the late • We can then regulate access to the critical region Dutch computer scientist Edsgar Dijkstra) – When one thread is executing in its critical region, no other thread/process is allowed to execute in its critical region wait(S) { signal(S) { – This is known as mutual exclusion while

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    19 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us