Application of Threads
This section discusses various implementation of threads. Multithreading concepts are implemented by Pthreads (POSIX standard), Solaris, Linux, Windows 2000 and Java.
POSIX Thread (Pthreads)
- Pthreads refers to the POSIX standard (IEEE 1003.1C) that defines an API for thread creation and synchronization.
- Prior to the existence of POSIX threads, each hardware vendor implemented their own versions of threads.
- As each implementation was different from the others, writing portable multithreaded applications was difficult. Thus, there was a need to standardize the API for thread management.
- In 1995, IEEE came up with POSIX 1003.1C standards. The POSIX stands for ‘Portable Operating System Interface’.
- The implementation of threads as per IEEE 10034.1C specification are called POSIX threads or Pthreads.
- The current Pthread API is defined only for C programming language and it is implemented as functions with a header file pthread.h and a thread library.
- The Pthread API consists of over 60 functions covering thread management, thread synchronization and communication between threads.
- The naming convention for Pthreads is well defined and the prefix of all the objects or functions is pthread.
- In addition to this, based on the prefix, it is possible to have an idea about the functionality of the identifier. For example, all the functions with prefix pthread_ cover thread management, those with prefix pthreads_attr_cover thread attribute object and so on.
- Most implementations of UNIX operating system such as Solaris, AIX, HP-UX etc. and Linux operating system provide implementation of Pthreads.
Solaris Threads
Solaris 2 is a version of UNIX, that implements Pthreads API in addition to supporting user-level threads with a library containing APIS for thread creation and management.
Solaris implements multilevel thread support designed to provide considerable flexibility. It make use of four separate thread related concepts:
1. Process. This is normal UNIX process and includes the user’s address space, stack and PCB.
2. User level threads. A user level thread (ULT) is a user created unit of execution within a process.
These are created by application programmer. They provide support for parallel execution within a process. These are implemented through a thread library in the address space of a process. These are invisible to the OS.
3. Lightweight processes. A LWP is a mapping between user level and kernel threads. Each LWP supports ULT and maps to one kernel thread. LWP are scheduled by the kernel independently and may execute in parallel multiprocessors.
4. Kernel threads. These threads are visible to OS. They are managed by OS.
Linux threads
- Threads in Linux are handled in a different way as compared to other operating system due to the open source nature of Linux.
- An important difference between Linux threads and other threads is that Linux does not differentiate between a process and a thread. A task represents basic unit of work in Linux.
- To create a child process, Linux provides two system calls: fork and clone.
- Clone call creates a child process like the fork call, but main difference is that, fork creates a child process that has its own process context similar to the parent process whereas the child process created by clone shares parts of it execution context with the calling process, such as memory space, the file descriptor table and the signal handler table.
- At user level, various libraries that implements Pthreads are available. For example, Linuxthreads, NPTL (Native POSIX Thread Library).
Windows 2000
- Threads Windows 2000 implements the Win32 API.
- A windows application runs as a separate process where each process contains one or more threads.
- Every thread of a process has access to the virtual address space of a process.
- Each thread includes : A thread ID for identification, a register set to represent the status of the processor, a user stack (when thread is running in user mode), a kernel stack (when thread is running in kernel mode), a private storage area used by various run time libraries and dynamic link libraries (DLLS).
The various data structure used by threads are :
- ETHREAD (executive thread block). It includes pointer to the process to which thread belongs and the address of the routine in which the thread starts control. The ETHREAD also contains a pointer to corresponding KTHREAD. ETHREAD is n kernel space and only kernel can access it.
- KTHREAD (Kernel thread block). It includes scheduling and synchronization information for the threads. It also includes the kernel stack and a pointer to TEP KTHREAD also lies in kernel space and only kernel has access to it.
- TEB (Thread environment block). It is a user space data structure that is accessed when thread is running in user mode. TEB contains a user model stack and an array for thread specific data.