Threads Of Computation - I

✬ Operating System IIIT Kalyani ✩ 1 Threads Of Computation - I ✫ Lect 4 ✪ Goutam Biswas ✬ Operating System ✩ IIIT Kalyani 2 Threads wit...
Author: Leona Wheeler
3 downloads 0 Views 123KB Size


Operating System

IIIT Kalyani

✩ 1

Threads Of Computation - I



Lect 4



Goutam Biswas



Operating System



IIIT Kalyani

2

Threads within a Process

• So far we have looked at a process as a program in execution. • There is a single execution sequence or thread of computation of code within a process. • For exploiting any parallelism to do a certain job, it is necessary to split the job into parts. ✫

Lect 4



Goutam Biswas



Operating System



IIIT Kalyani

3

Threads within a Process

• Then child processes are created to do the parts in parallel. • And finally combine the outcome of the child processes if necessary. • There are computational jobs that have distinct logical parts and can have almost independent threads of computation. ✫

Lect 4



Goutam Biswas



Operating System



IIIT Kalyani

4

Multiple Threads in a Process

• But instead of creating process, it is possible to have multiple threads of execution within a process. • Each thread can be scheduled independently. • Each thread has its own identification, thread ID (TID), CPU state (program counter (PC) and other registers), and stack. ✫

Lect 4



Goutam Biswas



Operating System



IIIT Kalyani

5

Multiple Threads in a Process

• But all threads of a process share the same code, global data, open files etc. • A software may have different kinds of activity e.g. user interfaces, computation, database access etc. • It may provide service to different users simultaneously. ✫

Lect 4



Goutam Biswas



Operating System



IIIT Kalyani

6

Multiple Threads in a Process

• So it is running as one or more processes each of which may have different threads running on different core of a modern processor. • In a client-server world this is a natural model where corresponding to every client request, the server may start a new thread of computation.



Lect 4



Goutam Biswas



Operating System



IIIT Kalyani

7

Multiple Threads in a Process

• It is also claimed that switching thread is order of magnitude faster than switching process. • A thread based interactive system may have a better response time. If one thread is blocked, other threads may continue. ✫

Lect 4



Goutam Biswas



Operating System



IIIT Kalyani

8

Multiple Threads in a Process

• A process can communicate either through shared memory or by message passing. Both requires some dialog with the OS. But a threads of a process can communicate through the common area of global data. ✫

Lect 4



Goutam Biswas



Operating System



IIIT Kalyani

9

Types of Threads

• The implementation of thread may be at the user level known as a user thread or at the OS level, known as a kernel thread. • User threads are managed at the user level, but kernel threads are managed by the OS. • But at the lower level any thread runs on a kernel thread. Following are three different mapping models.



Lect 4



Goutam Biswas



Operating System

IIIT Kalyani

Types of Threads

✩ 10

• Many-to-one model: maps many user threads to one kernel thread. • One-to-one model: each user thread is mapped to a kernel thread. • Many-to-many model: the set of user threads are mapped to a set (smaller or same size) of kernel threads. ✫

Lect 4



Goutam Biswas



Operating System



IIIT Kalyani

11

Many-to-One Model

• Threads are managed at the user space by the thread library, so there is no overhead of transition from user mode to kernel mode during thread switching. • But user threads cannot take the advantage of the multiprocessor or multi-core architecture. ✫

Lect 4



Goutam Biswas



Operating System



IIIT Kalyani

12

Many-to-One Model

• Any blocking system call will block the underlying kernel thread, resulting the blocking of all user level threads mapped to it. • User level threads are used for fine grain parallelism where system calls are often not required. ✫

Lect 4



Goutam Biswas



Operating System



IIIT Kalyani

13

Many-to-One Model

• Normally user level threads are small computation intensive code. Each thread has its CPU state and a thread control block to manage the user level scheduling of threads. • It should have its own mechanism to manage the atomicity of critical sections of code and synchronization. ✫

Lect 4



Goutam Biswas



Operating System



IIIT Kalyani

14

Many-to-One Model

• One important advantage is that it does not require any OS support and can be implemented on any OS. • But most OS today support kernel level thread and most processors are multi-core. That makes user level thread less popular. ✫

Lect 4



Goutam Biswas



Operating System



IIIT Kalyani

15

One-to-One Model

• Each user level thread is mapped to a kernel level thread. • Threads can run in parallel on a multiprocessor or multi-core architecture. • Blocking of one thread does not affect the execution of another thread. ✫

Lect 4



Goutam Biswas



Operating System



IIIT Kalyani

16

One-to-One Model

• For every user thread there is a kernel thread. So the thread creation overhead and the presence of large number of kernel threads may be a problem. • Thread creation time may be comparable to process creation time. • This model is good for coarse-grained parallelism.



Lect 4



Goutam Biswas



Operating System



IIIT Kalyani

17

One-to-One Model

• It require full support from the OS e.g. creation, scheduling, blocking and termination of threads. • OS must support data structures like thread control block (TCB) etc. ✫

Lect 4



Goutam Biswas



Operating System



IIIT Kalyani

18

Many-to-Many Model

• This is a middle-path between the first two models where an user can create as many threads as he wishes. • But the OS can create number of kernel threads depending on the architecture (number of processor or core). ✫

Lect 4



Goutam Biswas



Operating System



IIIT Kalyani

19

Many-to-Many Model

• If an user thread issues a blocking system call, the kernel can schedule a ready user thread on the kernel thread. • It is also possible to nail some particular user thread to a kernel thread. ✫

Lect 4



Goutam Biswas



Operating System



IIIT Kalyani

20

Thread Library

• An API to create and manage threads is provided by a thread library. • The library may work at the user space or at the kernel level. • We shall talk about POSIX Threads known as pthread. The API is defined by POSIX standard (IEEE Std 1003.1c-1995). ✫

Lect 4



Goutam Biswas



Operating System



IIIT Kalyani

21

An Example

/* Programming with pthread: pthread1.c++ $ g++ -Wall pthread1.c++ -lpthread $ ./a.out 5 */ #include using namespace std; #include #include



Lect 4



Goutam Biswas



Operating System

IIIT Kalyani

#include #include

✩ 22

void * thread1(void *) ; void * thread2(void *) ; int fact(int n){ if(n == 0) return 1 ; return n*fact(n-1) ; } int fib(int n) {



Lect 4



Goutam Biswas



Operating System

IIIT Kalyani

int f0 = 0, f1 = 1, i ;

✩ 23

if(n == 0) return f0 ; if(n == 1) return f1 ; for(i=2; i