Chapter 3 Process Description Design and Control Principles · Process Description and Control....

Post on 03-Aug-2020

25 views 1 download

transcript

Seventh EditionBy William Stallings

Operating Systems:Internals

and Design

Principles

Chapter 3Process Description and Control

Structure of Process Structure of Process Images in Virtual MemoryImages in Virtual Memory

Structure of Process Structure of Process Images in Virtual MemoryImages in Virtual Memory

How to do SWITCH???

1.Mode Switch2.Context Switch3.Process Switch

Context Switch

save the context of the processor

save the context of the processor

update the process control block of the process currently in the Running state

update the process control block of the process currently in the Running state

move the process control block of this

process to the appropriate queue

move the process control block of this

process to the appropriate queue

select another process for execution

select another process for execution

update the process control block of the

process selected

update the process control block of the

process selected

update memory management data

structures

update memory management data

structures

restore the context of the processor to

that which existed at the time the selected

process was last switched out

restore the context of the processor to

that which existed at the time the selected

process was last switched out

If the currently running process is to be moved to another state (Ready, Blocked, etc.), then the OS must make substantial changes in its environment

Mode Switch

If no interrupts are pending the processor:

If no interrupts are pending the processor:

proceeds to the fetch stage and fetches the next instruction of the current

program in the current process

If an interrupt is pending the processor:

If an interrupt is pending the processor:

Saves the context of the current program being executed.

switches from user mode to kernel mode so that the interrupt processing code may

include privileged instructions

sets the program counter to the starting address of an interrupt handler program

Process Switch

A process switch may occur any time that the OS has gained control from the currently running process.Possible events giving OS control are:

• Due to some sort of event that is external to and independent of the currently running process

– clock interrupt– I/O interrupt– memory fault

• Time slice– the maximum amount of

time that a process can execute before being interrupted

• An error or exception condition generated within the currently running process

• OS determines if the condition is fatal– moved to the Exit state and

a process switch occurs– action will depend on the

nature of the error

Process Switch

A process switch may occur any time that the OS has gained control from the currently running process.Steps:

1. Save the Current state 2. Give control the new process3. Restore the previous state.

What is the difference between Mode switch and Process switch?

Types of Kernel

Non-Process Kernel

Exe within User-process

Process –based Kernel

Cooperating Processes• Independent process cannot affect or be affected

by the execution of another process.• Cooperating process can affect or be affected by

the execution of another process• Advantages of process cooperation

– Information sharing – Computation speed-up via parallel sub-tasks– Modularity by dividing system functions into separate

processes – Convenience - even an individual may want to edit,

print and compile in parallel

Seventh EditionBy William Stallings

Operating Systems:Internals

and Design

Principles

Chapter 4Process and

Threads

Process and ThreadsProcessØThe unit or resource allocation and a unit of protectionØThe key states for a Process are:qRunningqReadyqBlockedqSuspendedqExit

ØProcess operations are:■ Create ■ Terminate ■ Spawn.

ThreadØThe unit of dispatching is referred to as a thread or lightweight processØThe key states for a thread are:qRunningqReadyqBlocked

ØThread operations associated with a change in thread state are:

n Spawnn Blockn Unblockn Finish

Process has PCB and Address spaceThread has TCB and Stacks

Process has PCB and Address spaceThread has TCB and Stacks

Benefits of THREADS

Takes less time to create a new thread than a process

Less time to terminate a thread than a process

Switching between two threads takes less timethan switching between processes

Threads enhance efficiency in communicationbetween programs

Types of Threads

Kernel level Thread (KLT)

User Level Thread (ULT)

ComparisonKLTs

1. Kernel is aware of the presence of KLT

2. Thread management is done by the kernel

3. If one thread in a process is blocked, the kernel can schedule another thread of the same process

ULTs

1. The kernel is not aware of the existence of threads

2. All thread management is done by the application (Thread Library; A package of routines for ULT)

3. In a typical OS many system calls are blocking as a result, when a ULT executes a system call, not only is that thread blocked, but all of the threads within the process are blocked

Difference and Similarities

KLTs

4. Can run on specific OS

5. The kernel can simultaneously schedule multiple threads from the same process on multiple processors

6. The transfer of control from one thread to another within the same process requires a mode switch to the kernel

7. Examples are:Windows NT

ULTs

4. ULTs can run on any OS

5. In a pure ULT strategy, a multithreaded application cannottake advantage of multiprocessing (multi-processor or multi-core)

6. Thread switching does not require kernel mode privileges

7. Examples are:DCE Thread library

Relationships Between ULTStates and Process States

Relationships Between ULTStates and Process States

Combined Approaches

Thread creation is donein the user space

Bulk of scheduling andsynchronization of threads is by the application

Solaris is an example

Solaris Process

• includes the user’s address space, stack, and process control blockProcessProcess

• a user-created unit of execution within a processUser-level Threads

User-level Threads

• a mapping between ULTs and kernel threadsLightweight

Processes (LWP)Lightweight

Processes (LWP)

• fundamental entities that can be scheduled and dispatched to run on one of the system processorsKernel ThreadsKernel Threads

Processes and Threadsin Solaris

Processes and Threadsin Solaris

A Lightweight Process (LWP) Data Structure Includes:

A Lightweight Process (LWP) Data Structure Includes:

1. An LWP identifier2. The priority of this LWP 3. A signal mask 4. Saved values of user-level registers 5. The kernel stack for this LWP6. Resource usage and profiling data7. Pointer to the corresponding kernel

thread8. Pointer to the process structure

User-Level Threads

Thread management is done by user-level threads library without the intervention of the kernel§ Fast to create and manage§ If the kernel is single threaded, any user-level thread performing a blocking system call willcause the entire process to be blocked

User thread libraries§ POSIX Pthreads§ Mach C-threads§ Solaris 2 UI-threads

Kernel-Level ThreadsSupported by the Kernel§Slower to create and manage than user threads§If thread performs a blocking system call, the kernel can schedule another thread in the application for execution§In multi-processor environments, the kernel can schedule threads on multiple processors

Examples- Windows 95/98/NT/2000- Solaris- Tru64 UNIX- BeOS- Linux

Relationship Between Threads and ProcessesRelationship Between Threads and Processes

Implementation of Threads in Java

public class Mythread{public static void main(String args[]){

SomeThread p1=new SomeThread(1);p1.start();

SomeThread p2=new SomeThread(2);p2.start();

SomeThread p3=new SomeThread(3);p3.start();

}}

class SomeThread extends Thread{

int myId;SomeThread(int id)

{this.myId=id;

}public void run(){

int i;for(i=1;i<10;i++)System.out.println

("Thread" + myId + ":" + i);}

}

Java Threads

Java threads may be created by extending the Thread class orimplementing the Runnable interface

Java threads are managed by the JVM

Thread Run Trace

Thread2:1Thread2:2Thread3:1Thread3:2Thread3:3Thread3:4Thread3:5Thread3:6Thread3:7

Thread3:8Thread3:9Thread1:1Thread2:3Thread2:4Thread2:5Thread1:2Thread1:3Thread2:6

Thread1:4Thread1:5Thread2:7Thread1:6Thread1:7Thread2:8Thread1:8Thread1:9Thread2:9

Summary

User-level threadscreated and managed by a threads library that runs in the user space of a processa mode switch is not required to switch from one thread to anotheronly a single user-level thread within a process can execute at a timeif one thread blocks, the entire process is blocked

Kernel-level threadsthreads within a process that are maintained by the kernela mode switch is required to switch from one thread to anothermultiple threads within the same process can execute in parallel on a multiprocessorblocking of a thread does not block the entire process

Process/related to resource ownership

Thread/related to program execution