os threads

16
THREADS BY SALMAN MEMON 2K12/IT/109 UNIVERSITY OF SINDH JAMSHORO

Upload: salman-memon

Post on 15-Apr-2017

632 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Os Threads

THREADS

BY SALMAN MEMON2K12/IT/109

UNIVERSITY OF SINDH JAMSHORO

Page 2: Os Threads

INTRODUCTION A thread is contained inside a process and different threads in the same process share some resources (most commonly memory), while different processes do not.

Process and threads

Page 3: Os Threads

INTRODUCTION A thread is a basic unit of CPU utilization, consisting of a program counter, a stack, and a set of registers, ( and a thread ID. )

On a single processor, multithreading generally occurs by as in multitasking, the processor switches between different threads.

On a multiprocessor or multi-core system, the threads or tasks actually do run at the same time, with each processor or core running a particular thread or task

Page 4: Os Threads

SINGLE AND MULTI THREAD

Page 5: Os Threads

Single Thread◦ Has single thread of control ◦ It allows the process to perform only 1 task at a time.

Multi thread◦ Has many threads◦ Simultaneous execution of different task

Page 6: Os Threads

BENEFITS Take less time to create a new tread than a process. Less time to terminate a tread than a presses. less time to switch between two treads within the same process.

Hence treads within the same process share memory and files, they can communicate with each other without invoking the kernel.

Responsiveness. Resource sharing . Economy. Utilization of MP Architectures.

Page 7: Os Threads

User Threads Thread management done by user-level threads library◦ Thread creation, scheduling, are done in user level

Fast to create and manage Drawback:

◦ If kernel is single thread, then user level thread performing a blocking system call will cause entire process to block

Page 8: Os Threads

KERNEL THREAD Supported by OS

◦ Thread creation, scheduling, are done in user level by kernel

Thread management is performed by os, thus kernel thread are slow.

If thread perform blocking system call, kernel can schedule another thread in application for execution

Page 9: Os Threads

Multithreading Models(In a specific implementation, the user threads must be mapped to kernel threads, using one of the following strategies. )

Many-to-One

One-to-One

Many-to-Many

Page 10: Os Threads

MANY-TO-ONE many user-level threads are all mapped onto a single kernel thread.

Thread management is handled by the thread library in user space, which is very efficient.

if a blocking system call is made, then the entire process blocks, even if the other user threads would otherwise be able to continue.

Ex: Green threads for Solaris and GNU Portable

Page 11: Os Threads

ONE-TO-ONE MODEL Each user-level thread maps to kernel thread Allow anther thread to run if block Run parallel Drawback: along with user thread kernel thread shld be created.

Ex: Linux and Windows from 95 to XP

Page 12: Os Threads

MANY-TO-MANY The many-to-many model multiplexes any number of user threads onto an equal or smaller number of kernel threads, combining the best features of the one-to-one and many-to-one models.

Blocking kernel system calls do not block the entire process.

Individual processes may be allocated variable numbers of kernel threads, depending on the number of CPUs present and other factors.

Ex: RIX, HP-UX, and Tru64 UNIX

Page 13: Os Threads

Thread Libraries Thread libraries provide programmers with an API for creating and managing threads.

Thread libraries may be implemented either in user space or in kernel space.

There are three main thread libraries in use today: POSIX Pthreads .Win32 threads.Java threads .

Page 14: Os Threads

Pthreads May be provided either as user-level or kernel-level.

A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization.

API specifies behavior of the thread library, implementation is up to development of the library.

Common in UNIX operating systems (Solaris, Linux, Mac OS X).

Page 15: Os Threads

Linux Threads Linux refers to them as tasks rather than threads. Thread creation is done through clone() system call.

clone() allows a child task to share the address space of the parent task (process).

Page 16: Os Threads