Presentation is loading. Please wait.

Presentation is loading. Please wait.

Threads Relation to processes Threads exist as subsets of processes Threads share memory and state information within a process Switching between threads.

Similar presentations


Presentation on theme: "Threads Relation to processes Threads exist as subsets of processes Threads share memory and state information within a process Switching between threads."— Presentation transcript:

1

2 Threads Relation to processes Threads exist as subsets of processes Threads share memory and state information within a process Switching between threads is faster than switching between processes

3 Threads Benefits Responsiveness: one thread can respond to the user if another is busy Resource sharing: threads share resources by default Economy: threads are easier to create and maintain than processes Scalability: use multiple processors easily Challenges Dividing activities: Which tasks can run concurrently? Balance: How to use each processor effectively? Data splitting: dividing the data between different cores Data dependency: Does one task depend on data from another? Testing and debugging: more difficult when using threads

4 User vs. Kernel Threads User Threads Managed by the application The kernel is not aware of user threads Programmed using a thread library (e.g. Pthreads, Win32, Java) Many-to-one mapping Kernel Threads Managed by the kernel Applications don’t have to manage threads Programmed using an API Most modern operating systems support kernel threads One-to-one mapping user space kernel space threads library process user threads user space kernel space user threads process kernel threads

5 User vs. Kernel Threads Advantages of user threads over kernel threads Thread switching does not require kernel mode Thread scheduling can be application-specific User threads can run on any operating system, iwthout support from the kernel Disadvantages of user threads compared to kernel threads If one user thread executes a system call that blocks its process, all other threads within that process are blocked Threads cannot execute concurrently if multiple processors are available

6 Combined Approach Some operating systems combine the previous two approaches. Thread creation is done in user space. Thread scheduling and synchronization done in user or kernel space. User threads from an application are mapped to some (smaller or equal) number of kernel threads. Many-to-many mapping or a two-level model user space kernel space threads library user threads kernel threads process

7 Thread Libraries A thread library provides programmer with API for creating and managing threads Two primary ways of implementing: Library entirely in user space Kernel-level library supported by the OS (invoke functions by system calls) POSIX Pthreads A specification, not an implementation, for thread creation and synchronization May be provided either as user-level or kernel-level API specifies behavior of the thread library, implementation is up to development of the library Common in UNIX operating systems (Solaris, Linux, Mac OS X)

8 Example: C program using Pthreads /** * Pthread example program * * @author Gagne, Galvin, Silberschatz * Operating System Concepts with Java, 8th ed, fig. 4.9 */ #include int sum; /* this data is shared by the thread(s) */ void *runner(void *param); /* the thread */ int main(int argc, char *argv[]) { pthread_t tid; /* the thread identifier */ pthread_attr_t attr; /* set of attributes for the thread */ if (argc != 2) { fprintf(stderr,"usage: a.out \n"); return -1; } if (atoi(argv[1]) < 0) { fprintf(stderr,"Argument %d must be non-negative\n",atoi(argv[1])); return -1; } /* get the default attributes */ pthread_attr_init(&attr); /* create the thread */ pthread_create(&tid,&attr,runner,argv[1]); /* now wait for the thread to exit */ pthread_join(tid,NULL); printf("sum = %d\n",sum); } /* thread will begin control in this function */ void *runner(void *param) { int i, upper = atoi(param); sum = 0; if (upper > 0) { for (i = 1; i <= upper; i++) sum += i; } pthread_exit(0); } /** * Pthread example program * * @author Gagne, Galvin, Silberschatz * Operating System Concepts with Java, 8th ed, fig. 4.9 */ #include int sum; /* this data is shared by the thread(s) */ void *runner(void *param); /* the thread */ int main(int argc, char *argv[]) { pthread_t tid; /* the thread identifier */ pthread_attr_t attr; /* set of attributes for the thread */ if (argc != 2) { fprintf(stderr,"usage: a.out \n"); return -1; } if (atoi(argv[1]) < 0) { fprintf(stderr,"Argument %d must be non-negative\n",atoi(argv[1])); return -1; } /* get the default attributes */ pthread_attr_init(&attr); /* create the thread */ pthread_create(&tid,&attr,runner,argv[1]); /* now wait for the thread to exit */ pthread_join(tid,NULL); printf("sum = %d\n",sum); } /* thread will begin control in this function */ void *runner(void *param) { int i, upper = atoi(param); sum = 0; if (upper > 0) { for (i = 1; i <= upper; i++) sum += i; } pthread_exit(0); }

9 Java Threads Java threads are managed by the JVM Java threads may be created in two ways: Extend the Thread class and override its run() method Implement the Runnable interface (preferred) public interface Runnable { public abstract void run() } public interface Runnable { public abstract void run() } Creating a Java thread using a Runnable object: 1.Create an instance of the Thread class and pass the constructor a Runnable object 2.Call the start() method of the thread object

10 Java Example /** * Create a separate thread by implementing the Runnable interface. * * @author Gagne, Galvin, Silberschatz * Operating System Concepts with Java, 8th Ed., Fig. 4.11 */ class Sum { private int sum; public int get() { return sum; } public void set(int sum) { this.sum = sum; } class Summation implements Runnable { private int upper; private Sum sumValue; public Summation(int upper, Sum sumValue) { this.upper = upper; this.sumValue = sumValue; } public void run() { int sum = 0; for (int i = 0; i <= upper; i++) sum += i; sumValue.set(sum); } public class Driver { public static void main(String[] args) { if (args.length != 1) { System.err.println("Usage Driver "); System.exit(0); } if (Integer.parseInt(args[0]) < 0) { System.err.println(args[0] + " must be >= 0"); System.exit(0); } Sum sumObject = new Sum(); // Create the shared object int upper = Integer.parseInt(args[0]); Thread worker = new Thread(new Summation(upper, sumObject)); worker.start(); try { worker.join(); } catch (InterruptedException ie) { } System.out.println("The sum of " + upper + " is " + sumObject.get()); } /** * Create a separate thread by implementing the Runnable interface. * * @author Gagne, Galvin, Silberschatz * Operating System Concepts with Java, 8th Ed., Fig. 4.11 */ class Sum { private int sum; public int get() { return sum; } public void set(int sum) { this.sum = sum; } class Summation implements Runnable { private int upper; private Sum sumValue; public Summation(int upper, Sum sumValue) { this.upper = upper; this.sumValue = sumValue; } public void run() { int sum = 0; for (int i = 0; i <= upper; i++) sum += i; sumValue.set(sum); } public class Driver { public static void main(String[] args) { if (args.length != 1) { System.err.println("Usage Driver "); System.exit(0); } if (Integer.parseInt(args[0]) < 0) { System.err.println(args[0] + " must be >= 0"); System.exit(0); } Sum sumObject = new Sum(); // Create the shared object int upper = Integer.parseInt(args[0]); Thread worker = new Thread(new Summation(upper, sumObject)); worker.start(); try { worker.join(); } catch (InterruptedException ie) { } System.out.println("The sum of " + upper + " is " + sumObject.get()); }

11 Java Threads Note: If two Java threads are to share data, references to the shared objects must be passed to the threads. The join() method causes the parent thread to wait for its child to finish processing, and this can throw an InterruptedException. Java doesn’t specify how the JVM maps java threads to the underlying operating system. Java threads may be in one of six states: new, runnable, blocked, waiting, timed waiting, terminated Java provides methods to determine the state of a thread: – isAlive() returns true if a thread is started, but not terminated – getState() returns the state as an enumerated data type

12 Java Threads

13 Java Example /** * Factory class that creates the MessageQueue class and * the producer and consumer threads. * * @author Gagne, Galvin, Silberschatz * Operating System Concepts with Java, 8th ed, Fig. 4.13 */ import java.util.Date; public class Factory { public static void main(String[] args) { // create the message queue Channel queue = new MessageQueue (); // create the producer and consumer threads Thread producer = new Thread(new Producer(queue)); Thread consumer = new Thread(new Consumer(queue)); // start the threads producer.start(); consumer.start(); } /** * Factory class that creates the MessageQueue class and * the producer and consumer threads. * * @author Gagne, Galvin, Silberschatz * Operating System Concepts with Java, 8th ed, Fig. 4.13 */ import java.util.Date; public class Factory { public static void main(String[] args) { // create the message queue Channel queue = new MessageQueue (); // create the producer and consumer threads Thread producer = new Thread(new Producer(queue)); Thread consumer = new Thread(new Consumer(queue)); // start the threads producer.start(); consumer.start(); } /** * The message queue * * @author Gagne, Galvin, Silberschatz * Operating System Concepts with Java, 8th ed, Fig. 3.21 */ import java.util.Vector; public class MessageQueue implements Channel { private Vector queue; public MessageQueue() { queue = new Vector (); } public void send(E item) { queue.addElement(item); } public E receive() { if (queue.size() == 0) return null; else return queue.remove(0); } /** * The message queue * * @author Gagne, Galvin, Silberschatz * Operating System Concepts with Java, 8th ed, Fig. 3.21 */ import java.util.Vector; public class MessageQueue implements Channel { private Vector queue; public MessageQueue() { queue = new Vector (); } public void send(E item) { queue.addElement(item); } public E receive() { if (queue.size() == 0) return null; else return queue.remove(0); } /** * The producer class * * @author Gagne, Galvin, Silberschatz * Operating System Concepts with Java, 8th ed, Fig. 4.14 */ import java.util.Date; class Producer implements Runnable { private Channel queue; public Producer(Channel queue) { this.queue = queue; } public void run() { Date message; while (true) { SleepUtilities.nap(); //nap for awhile message = new Date(); System.out.println("Producer produced " + message); queue.send(message); // produce item and enter it into buffer } /** * The producer class * * @author Gagne, Galvin, Silberschatz * Operating System Concepts with Java, 8th ed, Fig. 4.14 */ import java.util.Date; class Producer implements Runnable { private Channel queue; public Producer(Channel queue) { this.queue = queue; } public void run() { Date message; while (true) { SleepUtilities.nap(); //nap for awhile message = new Date(); System.out.println("Producer produced " + message); queue.send(message); // produce item and enter it into buffer } /** * The consumer class * * @author Gagne, Galvin, Silberschatz * Operating System Concepts with Java, 8th ed, Fig. 4.15 */ import java.util.Date; class Consumer implements Runnable { private Channel queue; public Consumer(Channel queue) { this.queue = queue; } public void run() { Date message; while (true) { SleepUtilities.nap(); // consume an item from the buffer System.out.println("Consumer wants to consume."); message = queue.receive(); if (message != null) System.out.println("Consumer consumed " + message); } /** * The consumer class * * @author Gagne, Galvin, Silberschatz * Operating System Concepts with Java, 8th ed, Fig. 4.15 */ import java.util.Date; class Consumer implements Runnable { private Channel queue; public Consumer(Channel queue) { this.queue = queue; } public void run() { Date message; while (true) { SleepUtilities.nap(); // consume an item from the buffer System.out.println("Consumer wants to consume."); message = queue.receive(); if (message != null) System.out.println("Consumer consumed " + message); }

14 Creating and Cancelling Threads Semantics of fork() and exec() system calls Does fork() duplicate only the calling thread or all threads? It could be implemented either way. The exec() system call typically replaces the entire process (all threads) with a new program. Thread cancellation Terminating a thread before it has finished Two general approaches: – Asynchronous cancellation terminates the target thread immediately – Deferred cancellation allows the target thread to periodically check if it should be cancelled If a thread is cancelled, how do we ensure that the system is stable? Java provides an interrupt status for each thread.

15 Java Example /** * Example program illustrating thread interruption. * * @author Gagne, Galvin, Silberschatz * Operating System Concepts with Java, 8th Ed., Fig. 4.16 */ public class InterruptibleThread implements Runnable { /** * This thread will continue to run as long * as it is not interrupted. */ public void run() { while (true) { /* do some work for awhile */ if (Thread.currentThread().isInterrupted()) { System.out.println("I'm interrupted!"); break; } /* clean up and terminate */ } public static void main(String[] args) { Thread worker = new Thread (new InterruptibleThread()); worker.start(); /* now wait 3 seconds before interrupting it */ try { Thread.sleep(3000); } catch (InterruptedException ie) { } worker.interrupt(); } /** * Example program illustrating thread interruption. * * @author Gagne, Galvin, Silberschatz * Operating System Concepts with Java, 8th Ed., Fig. 4.16 */ public class InterruptibleThread implements Runnable { /** * This thread will continue to run as long * as it is not interrupted. */ public void run() { while (true) { /* do some work for awhile */ if (Thread.currentThread().isInterrupted()) { System.out.println("I'm interrupted!"); break; } /* clean up and terminate */ } public static void main(String[] args) { Thread worker = new Thread (new InterruptibleThread()); worker.start(); /* now wait 3 seconds before interrupting it */ try { Thread.sleep(3000); } catch (InterruptedException ie) { } worker.interrupt(); }

16 Signal Handling Signals are used in UNIX systems to notify a process that a particular event has occurred. Synchronous signals: delivered to the thread that caused the signal – examples: division by zero, illegal memory access Asynchronous signals: generated by an external event, delivered to a process or thread – examples: user input, user termination of process A signal handler is used to process signals. Options: – Deliver the signal to the thread to which the signal applies – Deliver the signal to every thread in the process – Deliver the signal to certain threads in the process – Assign a specific thread to receive all signals for the process

17 Thread Pools Idea: create a number of threads, place them in a “pool” where they await work. Advantages: – Usually slightly faster to service a request with an existing thread than to create a new thread. – Allows the number of threads in an application to be bound to the size of the pool, avoiding the creation of an extremely large number of threads. In Java, the java.util.concurrent package facilitates thread pools via the Executor interface.

18 Thread-Specific Data Allows each thread to have its own copy of data Useful when you do not have control over the thread creation process (i.e., when using a thread pool) Java provides the ThreadLocal class for thread-specific data. Example…

19 Java Example /** * Example program illustrating thread-specific data. * * @author Gagne, Galvin, Silberschatz * Operating System Concepts with Java, 8th Ed., Fig. 4.18 and 4.19 */ public class TSD { public static void main(String[] args) { java.util.concurrent.ExecutorService pool = java.util.concurrent.Executors.newCachedThreadPool(); for (int i = 0; i < 5; i++) { // just for kicks, use a thread pool pool.execute(new Worker()); } pool.shutdown(); } /** * Example program illustrating thread-specific data. * * @author Gagne, Galvin, Silberschatz * Operating System Concepts with Java, 8th Ed., Fig. 4.18 and 4.19 */ public class TSD { public static void main(String[] args) { java.util.concurrent.ExecutorService pool = java.util.concurrent.Executors.newCachedThreadPool(); for (int i = 0; i < 5; i++) { // just for kicks, use a thread pool pool.execute(new Worker()); } pool.shutdown(); } /** * Each thread performs a transaction and every transaction is * identified by a separate serial number. For logging purposes, * we may wish to log the transaction being performed by each thread. */ class Worker implements Runnable { private static Service provider; public void run() { provider.transaction(); System.out.println(Thread.currentThread().getName() + " > " + provider.getErrorCode() + " < "); } /** * Each thread performs a transaction and every transaction is * identified by a separate serial number. For logging purposes, * we may wish to log the transaction being performed by each thread. */ class Worker implements Runnable { private static Service provider; public void run() { provider.transaction(); System.out.println(Thread.currentThread().getName() + " > " + provider.getErrorCode() + " < "); } /** * This service class fulfills transactions which are performed by * separate threads. Because a transaction may result in an error, we * need to record the error. However, since there is only a static * instance of this class, there is only one copy of errorCode. If an * error occurs in one thread, it will set the value of errorCode, * however another thread may set it to a different value. The solution * to this is to use ThreadLocal copies of errorCode. Every thread that * causes an error will set its own copy of errorCode. */ class Service { public static void transaction() { // fulfill some kind of transaction service try { Thread.sleep( (int) (Math.random() * 1000) ); } catch (InterruptedException ie) { } // some operation where an error may occur try { int num = (int) (Math.random() * 2); double recip = 1 / num; } catch (Exception e) { errorCode.set(e); } /* get the error code for this transaction */ public static Object getErrorCode() { System.out.println("calling get() " + Thread.currentThread().getName()); return errorCode.get(); } private static ThreadLocal errorCode = new ThreadLocal(); } /** * This service class fulfills transactions which are performed by * separate threads. Because a transaction may result in an error, we * need to record the error. However, since there is only a static * instance of this class, there is only one copy of errorCode. If an * error occurs in one thread, it will set the value of errorCode, * however another thread may set it to a different value. The solution * to this is to use ThreadLocal copies of errorCode. Every thread that * causes an error will set its own copy of errorCode. */ class Service { public static void transaction() { // fulfill some kind of transaction service try { Thread.sleep( (int) (Math.random() * 1000) ); } catch (InterruptedException ie) { } // some operation where an error may occur try { int num = (int) (Math.random() * 2); double recip = 1 / num; } catch (Exception e) { errorCode.set(e); } /* get the error code for this transaction */ public static Object getErrorCode() { System.out.println("calling get() " + Thread.currentThread().getName()); return errorCode.get(); } private static ThreadLocal errorCode = new ThreadLocal(); }

20 Scheduler Activations Both many-to-many and two-level models require communication between the kernel and the thread library to maintain the appropriate number of kernel threads allocated to the application. Many systems use an intermediate data structure known as a lightweight process (LWP) between user threads and kernel threads. The LWP appears as a virtual processor to user threads. Scheduler activations provide upcalls: a communication mechanism from the kernel to the thread library Example: If a thread blocks to wait for I/O, the kernel makes an upcall to the thread library. This communication allows an application to maintain the correct number kernel threads. user thread kernel threads LWP

21 Windows Windows uses the Win32 and Win64 APIs Implements a one-to-one mapping of user threads to kernel threads Offers a fiber library to provide support for the many-to-many model Each thread contains – A thread id – Register set representing the status of the processor – Separate user and kernel stacks, for when the thread is running in user mode and kernel mode – Private data storage area The register set, stacks, and private storage area are known as the context of the threads

22 Windows The primary data structures of a thread include: ETHREAD (executive thread block): includes pointers to the process that owns the thread and to the KTHREAD KTHREAD (kernel thread block): scheduling and synchronization information TEB (thread environment block): user-mode data

23 Linux Threads Linux refers to them as tasks rather than threads Thread creation is done through clone() system call – clone() requires a set of parameters that determine how much sharing takes place between parent and child tasks The Linux kernel includes a data structure for each task that contains pointers to structures where data is stored (e.g. list of open files, signal-handling information, virtual memory) – fork() creates a new task and copies the data structure of parent process – clone() creates a new task and allows the new data structure to point to that of the parent


Download ppt "Threads Relation to processes Threads exist as subsets of processes Threads share memory and state information within a process Switching between threads."

Similar presentations


Ads by Google