Docsity

Operating Systems: Threads and Multiprocessing - Prof. Prasad Kulkarni, Study notes of Operating Systems

Partial preview of the text

Download Operating Systems: Threads and Multiprocessing - Prof. Prasad Kulkarni and more Study notes Operating Systems in PDF only on Docsity!

Chapter 4: Threads – Outline

n What are threads? How do they compare with processes?

n Why are threads important?

n What are the common multithreading models?

n What are the common multithreading libraries?

n Discussion on threading issues.

n Examples of threads in contemporary OSes.

Process Overview

n The basic unit of CPU utilization is a process.

n To run a program (a sequence of instructions), create a

process.

n Process properties

l (^) fork() → exec() can be used to start new program execution l (^) processes are well protected from each other l (^) context-switching between processes is fairly expensive l (^) inter-process communication used for information sharing and co- ordination between processes  (^) shared memory  (^) message passing

Is the Process Abstraction Always

Suitable? (2)

n Ability to run multiple sequences of code (threads of control) for

different object

l (^) individual process only offers one thread of control

n Way for threads of control to share data effectively

l (^) processes NOT designed to do this

n Protection between threads of control not very important

l (^) all in one application, anyway! l (^) process is an over-kill

n Switching between threads of control must be efficient

l (^) context switching involves a lot of overhead

n Different threads of control may share most information

l (^) processes duplicate entire address space

Threads to the Rescue

n Threads are designed to achieve all the above requirements!

l (^) do as little as possible to allow execution of a thread of control

n Thread are known as a lightweight process

l (^) only the necessary context information is re-generated  (^) thread-context: PC, registers, stack, other misc. info  (^) process-context: also includes data and code regions l (^) threads are executed within a process  (^) code and data shared among different threads  (^) reduced communication overhead l (^) smaller context  (^) faster context switching l (^) a single address space for all threads in a process  (^) reduced inter-thread protection

Single and Multithreaded Process

source: https://computing.llnl.gov/tutorials/pthreads/

Thread Benefits

n Responsiveness

l (^) for an interactive user, if part of the application is blocked

n Resource Sharing

l (^) easier, via memory sharing l (^) be aware of synchronization issues

n Economy

l (^) sharing reduces creation, context-switching, and space overhead

n Scalability

l (^) can exploit computational resources of a multicore CPU

Pthreads Example

#include <pthread.h> #include <stdio.h> int sum; /* data shared by all threads */ void runner(void param); / * thread function prototype */ int main (int argc, char argv[]) { pthread_t tid; / thread identifier / pthread_attr_t attr / set of thread attributes / if(atoi(argv[1]) < 0) { fprintf(stderr, “%d must be >=0\n”, atoi(argv[1])); return -1; } / get the default thread attributes / pthread_attr_init(&attr); / create the thread / pthread_create(&tid, &attr, runner, argv[1]); / wait for the thread to exit */ pthread_join(tid, NULL); fprintf(stdout, “sum = %d\n”, sum); }

Pthreads Example (2)

.... (cont. from previous page...) /* 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); }

User Vs. Kernel Level Threads

n User-level threads – manage threads in user code

l (^) advantages  (^) efficient and flexible in space, speed, switching, and scheduling l (^) disadvantages  (^) one thread blocked on I/O can block all threads  (^) difficult to automatically take advantage of SMP l (^) examples  (^) GNU Portable Threads, Free BSD's userland threads, QuickThreads

n Kernel-level threads – kernel manages the threads

l (^) Advantages  (^) removes disadvantages of user-level threads l (^) Disadvantages  (^) greater overhead due to kernel involvement l (^) Examples  (^) Pthreads, Windows XP threads, etc.

Multithreading Models

n Relationships between user and kernel threads

l (^) Many-to-One l (^) One-to-One l (^) Many-to-Many

One–to–One Multithreading Model

n Each user-level thread maps to kernel thread

l (^) examples – Windows NT/XP/2000, Linux, Solaris 9 and later

Many–to–Many Multithreading Model

n m user level threads mapped to n kernel threads

l (^) operating system can create a sufficient number of kernel threads l (^) examples – Solaris prior to v9, Windows NT/2000 ThreadFiber package

Threading Issues

n Semantics of fork() and exec() system calls

n Thread cancellation of target thread

n Signal handling

n Thread pools

n Thread-specific data

n Scheduler activations

Semantics of fork() and exec()

n Does fork() duplicate only the calling thread or all threads?

l (^) some systems provide two versions of fork()

n How about exec()?

l (^) most systems maintain the semantics of exec()

n Observations

l (^) exec() called immediately after fork  (^) duplicating all threads is unnecessary l (^) exec() not called after fork  (^) new process should duplicate all threads