Thread Libraries



Rating - 4/5
453 views

Thread Libraries

A thread library provides the programmer an API for creating and managing threads. There are two primary ways of implementing a thread library. The first approach is to provide a library entirely in user space with no kernel support. All code and data structures for the library exist in user space. This means that invoking a function in the library results in a local function call in user space and not a system call.

The second approach is to implement a kernel-level library supported directly by the operating system. In this case, code and data structures for the library exist in kernel space. Invoking a function in the API for the library typically results in a system call to the kernel. Three main thread libraries are in use today: (1) POSIX Pthreads, (2) Win32, and (3) Java. Pthreads, the threads extension of the POSIX standard, may be provided as either a user- or kernel-level library. The Win32 thread library is a kernel-level library available on Windows systems. The Java thread API allows thread creation and management directly in Java programs. However, because in most instances the JVM is running on top of a host operating system, the Java thread API is typically implemented using a thread library available on the host system. This means that on Windows systems, Java threads are typically implemented using the Win32 API; UNIX and Linux systems often use Pthreads.

Thread Libraries

In the remainder of this section, we describe basic thread creation using these three thread libraries. As an illustrative example, we design a multithreaded program that performs the summation of a non-negative integer in a separate thread using the well-known summation function: sum — /=o For example, if N were 5, this function would represent the summation from 0 to 5, which is 15. Each of the three programs will be run with the upper bounds of the summation entered on the command line; thus, if the user enters 8, the summation of the integer values from 0 to 8 will be output.

Pthreads

Pthreads refers to the POSIX standard (IEEE 1003.1c) defining an API for thread creation and synchronization. This is a specification for thread behavior, not an implementation. Operating system designers may implement the specification in any way they wish. Numerous systems implement the Pthreads specification, including Solaris, Linux, Mac OS X, and Tru64 UNIX. Shareware implementations are available in the public domain for the various Windows operating systems as well. The C program shown in Figure 4.6 demonstrates the basic Pthreads API for constructing a multithreaded program that calculates the summation of a nonnegative integer in a separate thread. In a Pthreads program, separate threads begin execution in a specified function. In Figure 4.6, this is the runner () function. When this program begins, a single thread of control begins in mainO.

 

 After some initialization, mainO creates a second thread that begins control in the runner () function. Both threads share the global data sum. Let's look more closely at this program. All Pthreads programs must include the pthread.h header file. The statement pthreadjt tid declares the identifier for the thread we will create. Each thread has a set of attributes, including stack size and scheduling information. The pthread_attr_t attr declaration represents the attributes for the thread. We set the attributes in the function call pthread_attr_init C&attr). Because we did not explicitly set any attributes, we use the default attributes provided. (In Chapter 5, we will discuss some of the scheduling attributes provided by the Pthreads API.) A separate thread is created with the pthread_creat e () function call. In addition to passing the thread identifier and the attributes for the thread, we also pass the name of the function where the new thread will begin execution-—in this case, the runner () function. Last, we pass the integer parameter that was provided on the command line, argv [1]. At this point, the program has two threads: the initial (or parent) thread in mainO and the summation (or child) thread performing the summation operation in the runner () function. After creating the summation thread, the parent thread will wait for it to complete by calling the pthread_join() function. The summation thread will complete when it calls the function pthread.exit 0 . Once the summation thread has returned, the parent thread will output the value of the shared data sum.

Win32 Threads

The technique for creating threads using the Win32 thread library is similar to the Pthreads technique in several ways. We illustrate the Win32 thread API in the C program shown in Figure 4.7. Notice that we must include the windows. h header file when using the Win32 API. Just as in the Pthreads version shown in Figure 4.6, data shared by the separate threads—in this case, Sum—are declared globally (the DWORD data type is an unsigned 32-bit integer. We also define the SummationO function that is to be performed in a separate thread. This function is passed a pointer to a void, which Win32 defines as LPVOID. The thread performing this function sets the global data Sum to the value of the summation from 0 to the parameter passed to SummationO.

Threads are created in the Win32 API using the CreateThreadO function and—just as in Pthreads—a set of attributes for the thread is passed to this function. These attributes include security information, the size of the stack, and a flag that can be set to indicate if the thread is to start in a suspended state. In this program, we use the default values for these attributes (which do not initially set the thread to a suspended state and instead make it eligible to be run by the CPU scheduler). Once the summation thread is created, the parent must wait for it to complete before outputting the value of Sum, as the value is set by the summation thread. Recall that the Pthread program (Figure 4.6) had the parent thread wait for the summation thread using the pthread_j oin () statement. We perform the equivalent of this in the Win32 API using the WaitForSingleObj ect () function, which causes the creating thread to block until the summation thread has exited. (We will cover synchronization objects in more detail in Chapter 6.)

Java Threads

Threads are the fundamental model of program execution in a Java program, and the Java language and its API provide a rich set of features for the creation and management of threads. All Java programs comprise at least a single thread Chapter 4 Threads of control—even a simple Java program consisting of only a main.0 method runs as a single thread in the JVM. There are two techniques for creating threads in a Java program. One approach is to create a new class that is derived from the Thread class and to override its run() method. An alternative—and more commonly used— technique is to define a class that implements the Runnable interface.

These Topics Are Also In Your Syllabus Thread Libraries
1 Types of Operating Systems - Batch operating system, Time-sharing systems, Distributed OS, Network OS, Real Time OS link
2 Distributed System-Summary link
You May Find Something Very Interesting Here. Thread Libraries link
3 Summary of CPU Scheduling link
4 Summary-Virtual Memory link
5 Mass-Storage Structure-Summary link

The JVM and Host Operating System

The JVM is typically implemented on top of a host operating system (see Pigure 2.17). This setup allows the JVM to bide the implementation details of the underlying operating system and to provide a consistent, abstract environment that allows Java programs to operate on any platform that supports- a JVM. The specification for the JVM does not indicate how Java 'threads are to be mapped to the underlying operating system, instead leaving that decision to the particular implementation.of the JVM. For example, the Windows XP operating system uses the one-to-one model; therefore, each Java thread for a ' JVVI running on such a system maps to a kernel thread. On operating systems that use the m.any-to-many model.(such as Tru64 UNIX), a Java thread is mapped according to the many-to-many model. Solaris ini tially implemented the JVM using the many-to-one model (the green thre'adslibrary,' mentioned-earlier). Later releases of the JVM were implemented using the many-to-many model.

Beginning with Solaris 9, Java threads were mapped using the one-to-one model. In addition, there may be a relationship between the Java thread library and-the-thread library on the host operating system. For example, implementations of a. JVM for the Windows family of operating systems might use the Win32 API when creating Java threads; Linux and Solaris systems might use the Pthreads -APL

The Runnable interface is defined as follows: public interface Runnable { public abstract void run(); When a class implements Runnable, it must define a run() method. The code implementing the run() method is what runs as a separate thread. Figure 4.8 shows the Java version of a multithreaded program that determines the summation of a non-negative integer. The Summation class implements the Runnable interface. Thread creation is performed by creating an object instance of the Thread class and passing the constructor a Runnable object. Creating a Thread object does not specifically create the new thread; rather, it is the start () method that actually creates the new thread. Calling the start () method for the new object does two things:

1. It allocates memory and initializes a new thread in the JVM.

2. It calls the run () method, making the thread eligible to be run by the JVM. (Note that we never call the run() method directly. Rather, we call the start () method, and it calls the run() method on our behalf.)

When the summation program runs, two threads are created by the JVM. The first is the parent thread, which starts execution in the main() method. The second thread is created when the start () method on the Thread object is invoked. This child thread begins execution in the run () method of the Summation class. After outputting the value of the summation, this thread terminates when it exits from its run () method. Sharing of data between threads occurs easily in Win32 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 Java program shown in Figure 4.8, the main thread and the summation thread share the the object instance of the Sum class. This shared object is referenced through the appropriate getSumO and setSumO methods. (You might wonder why we don't use an Integer object rather than designing a new sum class.

The reason is that the Integer class is immutable—that is, once its value is set, it cannot change.) Recall that the parent threads in the Pthreads and Win32 libraries use pthreacLjoinO and WaitForSingleObject() (respectively) to wait for the summation threads to finish before proceeding. The joinO method in Java provides similar functionality. (Notice that joinO can throw an InterruptedException, which we choose to ignore.)


Rating - 4/5
482 views

Advertisements