Threads These slides are adapted from Operating Systems

  • Slides: 25
Download presentation
Threads These slides are adapted from Operating Systems Concepts textbook, 8 th edition, Silberscatz,

Threads These slides are adapted from Operating Systems Concepts textbook, 8 th edition, Silberscatz, Galvin, Gagne.

Threads • A thread comprises a thread ID, a program counter, a register set,

Threads • A thread comprises a thread ID, a program counter, a register set, and a stack. • It shares with other threads belonging to same process its code section, data section and other OS resources (open files, . . . ) • A web browser “process” might have one thread display images or text while another thread retrieves data from the network • A single application may be required to perform several similar tasks (e. g. a web server) • Server may create several processes. Process creation is time consuming. So multiple threads are used. • Many OS kernels are now multithreaded, each thread performs a specific task, such as interrupt handling.

Single and Multithreaded Processes

Single and Multithreaded Processes

User Threads • Thread management done by user-level threads library • Three primary thread

User Threads • Thread management done by user-level threads library • Three primary thread libraries: • POSIX Pthreads • Win 32 threads • Java threads

Kernel Threads • Supported by the Kernel • Examples • • • Windows XP/2000

Kernel Threads • Supported by the Kernel • Examples • • • Windows XP/2000 Solaris Linux Tru 64 UNIX Mac OS X

Thread Libraries • A thread library provides the programmer an API for creating and

Thread Libraries • A thread library provides the programmer an API for creating and managing threads • Ways of implementing a thread library: • Provide a library entirely in user space with no kernel support • All code and data structures for the library exist in user space • Invoking a function in the library results in a local function call in user space and not a system call • Implement a kernel-level library supported directly by the OS • Code and data structures exist in the kernel space • Invoking a function in the API results in a system call

Thread Libraries • POSIX Pthreads library may be provided as either a user-or kernellevel

Thread Libraries • POSIX Pthreads library may be provided as either a user-or kernellevel library • The Win 32 thread library is a kernel level library available on Windows • Java thread API allows thread creation and management directly in Java programs, but since JVM is running on top of a host OS, the Java thread API is implemented using a thread library on the host (e. g. Win 32) • We will examine these thread libraries. We design a multithreaded program that performs the summation of a nonnegative integer in a separate thread using the summation function: sum =

Multithreaded C program using Pthreads API #include <pthread. h> #include <stdio. h> int sum;

Multithreaded C program using Pthreads API #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 thread attributes */ if (argc != 2) { fprintf(stderr, “usage: a. out < integer value>n”); return -1; } if (atoi(argv[1]) < 0) { fprintf(stderr, “%d must be >= 0n”, atoi(argv[1])); return -1; }

Multithreaded C program using Pthreads API /* get the default attributes */ pthread_attr_init(&attr); /*

Multithreaded C program using Pthreads API /* get the default attributes */ pthread_attr_init(&attr); /* create thread */ pthread_create(&tid, &attr, runner, argv[1]); /* wait for the thread to exit */ pthread_join(tid, NULL); printf(“sum = %dn”, sum); } /* The thread will begin control in this function */ void *runner(void *param) { int i, upper = atoi(param); sum = 0; for (i = 1; i <= upper; i++) sum += i; pthread_exit(0); }

Multithreaded C program using the Win 32 API #include <windows. h> #include <stdio. h>

Multithreaded C program using the Win 32 API #include <windows. h> #include <stdio. h> DWORD Sum; /* data is shared by the thread(s) */ /* the thread runs in this separate function */ DWORD WINAPI Summation(LPVOID Param) { DWORD Upper = *(DWORD*)Param; for (DWORD i = 0; i <= Upper; i++) Sum += i; return 0; } int main(int argc, char * argv[]) { DWORD Thread. Id; HANDLE Thread. Handle; int Param; /* perform some basic error checking */ if (argc != 2) { fprintf(stderr, “An integer parameter is requiredn”); return -1; } Param = atoi(argv[1];

Multithreaded C program using the Win 32 API // create thread Thread. Handle =

Multithreaded C program using the Win 32 API // create thread Thread. Handle = Create. Thread( NULL, // default security attributes 0, // default stack size Summation, // thread function &Param, // parameter to thread function 0, // default creation flags &Thread. Id); // returns the thread identifier if (Thread. Handle != NULL) { // now wait for the thread to finish Wait. For. Single. Object(Thread. Handle, INFINITE); // close thread handle Close. Handle(Thread. Handle); printf(“Sum = %dn”, Sum); } }

Java program for the summation example class Sum { private int sum; public int

Java program for the summation example class Sum { private int sum; public int get. Sum() { return sum; } public void set. Sum(int sum) { this. sum = sum; } } class Summation implements Runnable { private int upper; private Sum sum. Value; public Summation(int upper, Sum sum. Value) { this. upper = upper; this. sum. Value = sum. Value; } public void run() { int sum = 0; for (int i = 0; i <= upper; i++) sum += i; sum. Value. set. Sum(sum); } }

Java program for the summation example Public class Driver { public static void main(String[]

Java program for the summation example Public class Driver { public static void main(String[] args) { if (arg. length > 0) { if (Integer. parse. Int(args[0]) < 0) System. err. println(args[0] + “ must be >= 0. ”); else { // create the object to be shared Sum sum. Object = new Sum(); int upper = Integer. parse. Int(args[0]); Thread thrd = new Thread(new Summation(upper, sum. Object)); thrd. start(); try { thrd. join(); System. out. println (“The sum of “+upper+” is “+sum. Object. get. Sum()); } catch (Interrupted. Exception ie) { } } } else System. err. println(“Usage: Summation < integer value>”); } }

Java • There are two techniques for creating threads in a Java program •

Java • There are two techniques for creating threads in a Java program • Create a new class that is derived from the Thread class and to override its run() method. • Define a class that implements the Runnable interface. public interface Runnable { public abstract void run(); } The code implementing the run() method is what runs as a thread • The specification for the JVM does not indicate how Java threads are to be mapped to the underlying OS. • Windows XP uses the one-to-one model. • There may be a relationship between the Java thread library and the thread library on the host OS. • For windows Win 32 API can be used when creating Java threads. • Linux and Solaris systems might use Pthreads API.

Sharing of Data • Occurs easily in Win 32 and Pthreads, as shared data

Sharing of Data • Occurs easily in Win 32 and Pthreads, as shared data are simply declared globally. • As a pure object-oriented language, Java has no such notion of global data; if two or more threads are to share data in a Java program, the sharing occurs by passing reference to the shared object to the appropriate threads. • In the example program, the main thread and the summation thread share the object instance of the Sum class which is referenced through the appropriate get. Sum() and set. Sum() methods.

Threading Issues • • • Semantics of fork() and exec() system calls Thread cancellation

Threading Issues • • • Semantics of fork() and exec() system calls Thread cancellation Signal handling Thread pools Thread specific data Scheduler activations

Semantics of fork() and exec() • Does fork() duplicate only the calling thread or

Semantics of fork() and exec() • Does fork() duplicate only the calling thread or all threads? • Some UNIX systems have chosen to have two versions • If a thread invokes the exec() system call, the program specified in the parameter to exec() will replace the entire process—including all threads • Which of the two versions of fork() to use depends on the application • If exec() is called immediately after forking, then duplicating all threads is unnecessary, as the program specified in the parameters to exec() will replace the process. Duplicating only the calling thread is appropriate. • If the separate process does not call exec() after forking, the separate process should duplicate all threads.

Thread Cancellation • Terminating a thread before it has finished • E. g. When

Thread Cancellation • Terminating a thread before it has finished • E. g. When a user presses the stop button on a web browser, all threads loading the page are cancelled. • Two general approaches: • Asynchronous cancellation terminates the target thread immediately. • Troublesome. The OS will reclaim system resources from a canceled thread but will not reclaim all resources. So, canceling may not free a system-wide resource • Deferred cancellation allows the target thread to periodically check if it should be cancelled • Allows a thread to check whether it should be canceled at a point when it can be canceled safely (“cancellation points” in Pthreads).

Win 32 Thread Pools DWORD WINAPI Pool. Function(AVOID Param) { /* This function runs

Win 32 Thread Pools DWORD WINAPI Pool. Function(AVOID Param) { /* This function runs as a separate thread */ } • A pointer to Pool. Function() is passed to one of the functions in the thread pool API, and a thread from the pool executes this function. One such member in the thread pool API is the Queue. User. Work. Item which has three parameters LPTHREAD_START_ROUTINE Function – a pointer to the function that is to run as a separate thread PVOID Param – the parameter passed to Function ULONG Flags – flags indicating how the thread pool is to create and manage execution of the thread. An example call Queue. User. Work. Item(&Pool. Function, NULL, 0); Which causes a thread from the thread pool to invoke Pool. Function() on behalf of the programmer.

Thread Specific Data • Threads belonging to a process share the data of the

Thread Specific Data • Threads belonging to a process share the data of the process • But 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)

Pthreads • A POSIX standard (IEEE 1003. 1 c) API for thread creation and

Pthreads • A POSIX standard (IEEE 1003. 1 c) API for thread creation and synchronization • 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)

Windows XP Threads • Win 32 API • Implements the one-to-one mapping • Each

Windows XP Threads • Win 32 API • Implements the one-to-one mapping • Each thread contains • • A thread id Register set Separate user and kernel stacks Private data storage area for run-time libraries and DLLs. • The register set, stacks, and private storage area are known as the context of the threads • The primary data structures of a thread include: • ETHREAD (executive thread block) • Contains thread start adress, pointer to parent process, pointer to KTHREAD • KTHREAD (kernel thread block) • Contains scheduling and synchronization info, kernel stack, pointer to TEB • TEB (thread environment block) • Contains thread identifier, user stack, thread-local storage (thread specific data)

Linux Threads • Linux refers to them as tasks rather than threads • Thread

Linux Threads • Linux refers to them as tasks rather than threads • Thread creation is done through clone() system call • clone() allows a child task to share the address space of the parent task (process) • File-system info may be shared • The same memory space may be shared • Signal handlers may be shared • The set of open files may be shared • Rather then copying all data structures, the new task points to the data structures of the parent task • A unique kernel data structure (struct task_struct) exist for each task in the system. It contains pointers to other data structures where these data are stored • When fork() is invoked, a new task is created, along with a copy of all the associated data structures of the parent process

Java Threads • Java threads are managed by the JVM • Java threads may

Java Threads • Java threads are managed by the JVM • Java threads may be created by: • Extending Thread class • Implementing the Runnable interface

Java Thread States

Java Thread States