19
  TOPIC: THREADS  AUTHOR: SILBERCHATZ , GALVIN AND GAGNE  PRESENTED B Y : HEENAZ 

Heenaz Os Ppt

Embed Size (px)

DESCRIPTION

Pattern recognition

Citation preview

TOPIC: THREADS, INTERPROCESS COMMUNICATION CONCURRENT PROCESSING, SEMAPHORES AND PROCESS SYNCHRONISATION

TOPIC: THREADS

AUTHOR: SILBERCHATZ , GALVIN AND GAGNE

PRESENTED BY: HEENAZ1Topics covered:ThreadsDifference between threads and processesTypes of processes with threadsBenefits of multithreaded programmingUser and kernel threads Threading implementationsThreading IssuesJava threads

Threads:Relatively recent development in OS.Light weight process which is a basic unit of CPU utilization.Process with single thread is called as classical process.Each thread is associated with a single process ( sequential flow of control ).Takes over the role of process as a smallest unit of scheduling.

How threads differ from processes:

Processes are typically independent, while threads exist as subsets of a process.Processes carry considerably moreinformation than threads, whereas multiple threads within a process share memory as well as otherresources.Processes have separateaddress space, whereas threads share their address space.

Types of processes with threads:

Single threaded process2. Multithreaded processSingle threaded process:codedatafilesstackregistersThreadMultithreaded process:codedatafilesstackregistersThreadOne thread may display image while other thread may retrieve the data from a network.registerstackregisterstackBenefits of multithreaded programming:Responsiveness: Multithreading allows to continue running even if part of it is blocked or is performing a lengthy operation, thereby increasing responsiveness to the user. Eg.a web browser allows user interaction with one thread while the image is loading in the other thread.Resource sharing: Threads share resources and memory of the process to which it belong (several threads within the same address space).Economy: It is economical to create switch threads.Utilization of the multiprocessor architecture: Each thread runs parallely on different processors. A single threaded process can run only in a single CPU but multithreaded process run on multi-CPU which increases the concurrency.User and Kernel threads:User threads : ( visible to programmer and are unkonwn to Kernel) - implemented by the thread library at the user level. - provides support for thread creation, scheduling and management with no support from the kernel. - fast to create and manage.ProcessThreadThread-TableUser SpaceKernel SpaceTypes of threading implementationMany-to-one ModelOne-to-one ModelMany-to-many Model

Kernel threads: - supported directly by the operating system. - performs thread creation, scheduling, and management in Kernel space. - slower to create and manage.Many-to-one ModelIt maps many user level threads to one Kernel thread.This is done in the user space.If a thread makes a blocking system call, entire process will block.Only one thread can access the kernel at a time (multiple threads are unable to run in parallel).One-to-one ModelIt maps each user thread to kernel thread.If a thread makes a blocking system call, then it allows the other thread to run parallely.

Drawback: Creating a user thread requires creating the corresponding Kernel thread.

Many-to-many ModelIt maps many user level threads to smaller or equal Kernel threads.We can create as many user threads as necessary and the corresponding Kernel threads run in parallel on multiprocessor.Threading Issues:Fork system callCancellationSignal HandlingThread PoolsFork system call:

New process is created.Copy of the address space.Easy communication of parent with its child process.Process identifier of the child is returned to the parent.Exec system call:

Used after the fork system call by one of the two processes to replace the process memory space.Cancellation:It is the task of terminating a thread before it has completed.Eg. If multiple threads are searching through the data base and one thread returns the result, the remaining threads might be cancelled.

The thread which is to be cancelled is known as target thread.

Two scenarios:

Asynchronous cancellation: One thread immediately terminates the target thread.

Defered cancellation: Allowing the target thread to terminate itself in an orderly fashion.Signal Handling:It is used to notify a process that a particular event has occured. The signal may occur synchronously or asynchronously.All signals follow the folowing pattern:A signal is generated by the occurence of the particular event.A generated signal is delivered to a process.Once delivered, the signal must be handled.Thread Pools:The idea is to create the threads at process startup and place them into a pool.When a request is received, the thread becomes active and completes its service. Java Threads:Threads are managed by Java Virtual Machine.Difficult to classify whether the thread belongs to user level or Kernel level.Each process has its own thread.Even a simple java program consisting of only a main method runs as a single thread in the JVM.

Thread Creation:One way to create a thread explicitly is to create a new class that is derived from the Thread class, and to override the run method of the Thread class .Calling the start method for the new object does two things:It allocates memory and initializes a new thread in the JVM.It calls the run method, making the thread eligible to be run by the JVM.When the summation program runs, two threads are created by the JVM. The first is the thread associated with the application-the thread that starts execution at the main method. The second thread is the summation thread that is created explicitly with the start method. The summation thread begins execution in its run method. The thread terminates when it exists from its run method.Class summation extends Thread{ public summation ( int n ) { upper = n; } public void run() { int sum = 0; if ( upper > 0) { for ( int i = 1; i 0) {

if ( Integer.parseInt (args[0]) < 0) System.err.println(args[0] + must be >=0.); else { Summation thrd = new Summation( Integer.parseInt ( args[0])); thrd.start(); } } else System.err.println ( Usage: Summation ); }} Java program for the summation of a non-negative integer.Thank you