+ All Categories
Home > Documents > Threads Relation to processes Threads exist as subsets of processes Threads share memory and state...

Threads Relation to processes Threads exist as subsets of processes Threads share memory and state...

Date post: 14-Dec-2015
Category:
Upload: joey-free
View: 292 times
Download: 3 times
Share this document with a friend
Popular Tags:
23
Threads CS 355 Operating Systems Dr. Matthew Wright Operating System Concepts chapter 4
Transcript
Page 1: Threads Relation to processes Threads exist as subsets of processes Threads share memory and state information within a process Switching between threads.

Threads

CS 355Operating Systems

Dr. Matthew Wright

Operating System Conceptschapter 4

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

ThreadsRelation 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

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

ThreadsBenefits• 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

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

User vs. Kernel ThreadsUser 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

threadslibrary

process

user threads

user space

kernel space

user threads

process

kernel threads

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

User vs. Kernel ThreadsAdvantages 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

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

Combined ApproachSome 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

threadslibrary

user threads

kernel threads

processprocess

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

Thread LibrariesA thread library provides programmer with API for creating and managing threadsTwo 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)

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

Example: C program using Pthreads/** * Pthread example program * * @author Gagne, Galvin, Silberschatz * Operating System Concepts with Java, 8th ed, fig. 4.9 */

#include <pthread.h>#include <stdio.h>

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 <integer value>\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);}

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

Java ThreadsJava threads are managed by the JVMJava 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()}

Creating a Java thread using a Runnable object:1. Create an instance of the Thread class and pass the constructor

a Runnable object2. Call the start() method of the thread object

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

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 <integer>");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 objectint 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());}

}

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

Java ThreadsNote:• 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

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

Java Threads

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

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 queueChannel<Date> queue = new MessageQueue<Date>();

// create the producer and consumer threadsThread producer = new Thread(new Producer(queue));Thread consumer = new Thread(new Consumer(queue));

// start the threadsproducer.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<E> implements Channel<E>{

private Vector<E> queue;

public MessageQueue() {queue = new Vector<E>();

}

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<Date> queue;

public Producer(Channel<Date> 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<Date> queue;

public Consumer(Channel<Date> queue) { this.queue = queue;

}

public void run() {Date message;

while (true){

SleepUtilities.nap();// consume an item from the bufferSystem.out.println("Consumer wants to

consume.");message = queue.receive();if (message != null)

System.out.println("Consumer consumed " + message);

} }}

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

Creating and Cancelling ThreadsSemantics 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.

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

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

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

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

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

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.

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

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…

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

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 poolpool.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()

+ " < ");}

}

/** * 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 servicetry { Thread.sleep( (int) (Math.random() * 1000) ); } catch (InterruptedException ie) { }

// some operation where an error may occurtry {

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

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

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

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

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

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

WindowsThe 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

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

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


Recommended