Threads 2
Threads 2
SYSTEMS
1
MULTI-THREADING 2
POSIX THREADS (THE
PTHREAD LIBRARY)
▪ Pthreads refers to the POSIX standard defining an API for thread creation,
scheduling, and synchronization.
▪ This is a specification for thread behavior not an implementation.
▪ OS designers may implement the specification in any way they wish.
▪ Generally, libraries implementing the Pthreads specification are restricted
to UNIX-based systems such as Solaris 2.
3
CREATING A THREAD
▪ You can create a threads by using the pthread_create() call.
▪ Here is the syntax of this call.
int pthread_create(pthread_t *threadp, const pthread_attr_t *attr, void*
(*routine)(void *), arg *arg);
▪ Here, ‘threadp’ contains thread ID (TID) of the thread created by the call,
▪ ‘attr’ is used to modify the thread attributes (stack size, stack address,
detached, joinable, priority, etc.),
▪ ‘routine’ is the thread function, and
▪ ‘arg’ is any argument we want to pass to the thread function.
▪ The argument does not have to be a simple native type; it can be a ‘struct’ of
whatever we want to pass in
4
PTHREAD CREATE CALL
FAILS
▪ The pthread_create() call fails and returns the corresponding value if any of
the following conditions is detected:
▪ EAGAIN—The system-imposed limit on the total number of threads in a
process has been exceeded or some system resource has been exceeded
(for example, too many LWPs were created).
▪ EINVAL—The value specified by ‘attr’ is invalid.
▪ ENOMEM—Not enough memory was available to create the new thread.
▪ You can do error handling by including the file <errno.h> and incorporating
proper error handling code in your programs.
5
JOINING A THREAD
▪ You can have a thread wait for another thread within the same process by using
the pthread_join() call.
▪ Here is the syntax of this call.
int pthread_join(pthread_t aThread, void **statusp);
▪ Here, ‘aThread’ is the thread ID of the thread to wait for and
▪ ‘statusp’ gets the return value of pthread_exit() call made in the process for
whom wait is being done.
▪ A thread can only wait for a joinable thread in the same process address space
▪ A thread cannot wait for a detached thread.
▪ Multiple threads can join with a thread but only one returns successfully; others
return with an error that no thread could be found with the given TID.
6
TERMINATING A THREAD
▪ You can terminate a thread explicitly by either returning from the thread
function or by using the pthread_exit() call.
▪ Here is the syntax of the pthread_exit() call.
void pthread_exit(void *valuep);
▪ Here, ‘valuep’ is a pointer to the value to be returned to the thread which is
waiting for this thread to terminate (i.e., the thread which has executed
pthread_join() for this thread).
▪ A thread also terminates when the main thread in the process terminates.
▪ When a thread terminates with the exit() system call, it terminates the
whole process because the purpose of the exit() system call is to terminate
a process and not a thread.
7
USER AND KERNEL
THREADS
▪ User threads—are supported above the kernel and are implemented by a thread
library at the user level.
▪ The library provides support for thread creation, scheduling, and management with no
support from the kernel.
▪ CPU not interrupted during thread switching.
▪ Fair scheduling in case P1 has one thread and P2 has 100.
▪ Since the kernel is unaware of user-level threads, all thread creation and scheduling are
done in the user space without the need for kernel intervention, and therefore are fast to
create and manage.
▪ If the kernel is single-threaded, then any user-level thread performing a blocking system
call will cause the entire process to block, even if other threads are available to run within
the application.
▪ User thread libraries include POSIX Pthreads, Solaris 2 UI-threads, and Mach C-threads.
8
USER AND KERNEL
THREADS
▪Kernel threads—are supported directly by the operating system.
▪ The kernel performs the scheduling, creation, and management in kernel space.
▪ CPU switched between context switching (among threads)
▪ Scheduling is not going to be fair in case P1 has one thread and P2 has 100.
▪ The kernel-level threads are hence slower to create and manage, compared to
user-level threads.
▪ However, since the kernel is managing threads, if a thread performs a blocking
system call, the kernel can schedule another thread in the application for
execution.
▪ Windows NT, Windows 2000, Solaris, BeOS, and Tru64 UNIX support kernel
threads.
9
MULTI-THREADING MODELS
▪ There are various models for mapping user-level threads to kernel-level
threads.
Many-to-One—In this model, many user-level threads are supported per
kernel thread.
▪ Since only one kernel-level thread supports many user threads, there is no
concurrency.
▪ This means that a process blocks when a thread makes a system call.
▪ Examples of these threads are Solaris Green threads and POSIX Pthreads.
10
MANY-TO-ONE
11
MULTI-THREADING MODELS
12
ONE-TO-ONE
13
MULTI-THREADING MODELS
14
MANY-TO-ONE
15