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

Slides:



Advertisements
Similar presentations
3.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Process An operating system executes a variety of programs: Batch system.
Advertisements

Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Multithreaded Programming.
Threads. Objectives To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the basis of multithreaded computer systems.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 6: Threads Chapter 4.
Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads CS 170 TY, Sept 2011.
Based on Silberschatz, Galvin and Gagne  2009 Threads Definition and motivation Multithreading Models Threading Issues Examples.
02/01/2007CSCI 315 Operating Systems Design1 Java Threads Notice: The slides for this lecture have been largely based on those accompanying the textbook.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Process Concept An operating system executes a variety of programs
02/02/2004CSCI 315 Operating Systems Design1 Threads Notice: The slides for this lecture have been largely based on those accompanying the textbook Operating.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Chapter 4: Threads Adapted to COP4610 by Robert van Engelen.
14.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Chapter 4 Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Thread. A basic unit of CPU utilization. It comprises a thread ID, a program counter, a register set, and a stack. It is a single sequential flow of control.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads CS 170 T Yang, Sept 2012.
14.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2011Operating System Concepts Essentials – 8 th Edition Chapter 4: Threads.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Threads A thread (or lightweight process) is a basic unit of CPU.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
4.1 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Thread Libraries  Pthreads  Windows.
Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads Modified from the slides of the text book. TY, Sept 2010.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 14 Threads 2 Read Ch.
Chapter 4: Multithreaded Programming. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts What is Thread “Thread is a part of a program.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 4: Threads.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Outline n Overview n Multithreading.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
CS307 Operating Systems Threads Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2011.
Threads. Thread A basic unit of CPU utilization. An Abstract data type representing an independent flow of control within a process A traditional (or.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 4: Threads.
CSS430 Threads1 Textbook Ch5 These slides were compiled from the OSC textbook slides (Silberschatz, Galvin, and Gagne) and the instructor’s class materials.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
CISC2200 Threads Fall 09. Process  We learn the concept of process  A program in execution  A process owns some resources  A process executes a program.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
CMSC 421 Spring 2004 Section 0202 Part II: Process Management Chapter 5 Threads.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Lecture 5. Example for periority The average waiting time : = 41/5= 8.2.
14.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 4: Multithreaded Programming.
Chapter 4: Threads Modified by Dr. Neerja Mhaskar for CS 3SH3.
Introduction to threads
Chapter 4: Multithreaded Programming
OPERATING SYSTEM CONCEPT AND PRACTISE
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Multithreaded Programming
Chapter 4 Threads.
Chapter 4: Threads.
Chapter 4: Threads Overview Multithreading Models Thread Libraries
Chapter 4: Threads.
Operating Systems Lecture 13.
Chapter 4: Threads.
Chapter 4: Threads.
Modified by H. Schulzrinne 02/15/10 Chapter 4: Threads.
CHAPTER 4:THreads Bashair Al-harthi OPERATING SYSTEM
Chapter 4: Threads & Concurrency
Q:(a)Describe the action for a kernel to context switching for threads (b)why it is faster than processes?
Outline Chapter 3: Processes Chapter 4: Threads So far - Next -
Chapter 4 Threads “Most modern applications are multithreaded”
Presentation transcript:

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

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

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

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

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

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)

Example: C program using Pthreads /** * Pthread example program * 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 * 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); }

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

Java Example /** * Create a separate thread by implementing the Runnable interface. * Gagne, Galvin, Silberschatz * Operating System Concepts with Java, 8th Ed., Fig */ 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. * Gagne, Galvin, Silberschatz * Operating System Concepts with Java, 8th Ed., Fig */ 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()); }

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

Java Threads

Java Example /** * Factory class that creates the MessageQueue class and * the producer and consumer threads. * Gagne, Galvin, Silberschatz * Operating System Concepts with Java, 8th ed, Fig */ 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. * Gagne, Galvin, Silberschatz * Operating System Concepts with Java, 8th ed, Fig */ 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 * Gagne, Galvin, Silberschatz * Operating System Concepts with Java, 8th ed, Fig */ 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 * Gagne, Galvin, Silberschatz * Operating System Concepts with Java, 8th ed, Fig */ 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 * Gagne, Galvin, Silberschatz * Operating System Concepts with Java, 8th ed, Fig */ 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 * Gagne, Galvin, Silberschatz * Operating System Concepts with Java, 8th ed, Fig */ 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 * Gagne, Galvin, Silberschatz * Operating System Concepts with Java, 8th ed, Fig */ 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 * Gagne, Galvin, Silberschatz * Operating System Concepts with Java, 8th ed, Fig */ 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); }

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.

Java Example /** * Example program illustrating thread interruption. * Gagne, Galvin, Silberschatz * Operating System Concepts with Java, 8th Ed., Fig */ 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. * Gagne, Galvin, Silberschatz * Operating System Concepts with Java, 8th Ed., Fig */ 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(); }

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

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.

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…

Java Example /** * Example program illustrating thread-specific data. * Gagne, Galvin, Silberschatz * Operating System Concepts with Java, 8th Ed., Fig 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. * Gagne, Galvin, Silberschatz * Operating System Concepts with Java, 8th Ed., Fig 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(); }

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

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

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

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