chapter 3 process description design and control principles · process description and control....

31
Seventh Edition By William Stallings Operating Systems: Internals and Design Principles Chapter 3 Process Description and Control

Upload: others

Post on 03-Aug-2020

41 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Chapter 3 Process Description Design and Control Principles · Process Description and Control. Process Control Block. Structure of Process ... A process switch may occur any time

Seventh EditionBy William Stallings

Operating Systems:Internals

and Design

Principles

Chapter 3Process Description and Control

Page 3: Chapter 3 Process Description Design and Control Principles · Process Description and Control. Process Control Block. Structure of Process ... A process switch may occur any time

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

Page 4: Chapter 3 Process Description Design and Control Principles · Process Description and Control. Process Control Block. Structure of Process ... A process switch may occur any time

How to do SWITCH???

1.Mode Switch2.Context Switch3.Process Switch

Page 5: Chapter 3 Process Description Design and Control Principles · Process Description and Control. Process Control Block. Structure of Process ... A process switch may occur any time

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

Page 6: Chapter 3 Process Description Design and Control Principles · Process Description and Control. Process Control Block. Structure of Process ... A process switch may occur any time

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

Page 7: Chapter 3 Process Description Design and Control Principles · Process Description and Control. Process Control Block. Structure of Process ... A process switch may occur any time

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

Page 8: Chapter 3 Process Description Design and Control Principles · Process Description and Control. Process Control Block. Structure of Process ... A process switch may occur any time

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?

Page 9: Chapter 3 Process Description Design and Control Principles · Process Description and Control. Process Control Block. Structure of Process ... A process switch may occur any time

Types of Kernel

Non-Process Kernel

Exe within User-process

Process –based Kernel

Page 10: Chapter 3 Process Description Design and Control Principles · Process Description and Control. Process Control Block. Structure of Process ... A process switch may occur any time

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

Page 11: Chapter 3 Process Description Design and Control Principles · Process Description and Control. Process Control Block. Structure of Process ... A process switch may occur any time

Seventh EditionBy William Stallings

Operating Systems:Internals

and Design

Principles

Chapter 4Process and

Threads

Page 12: Chapter 3 Process Description Design and Control Principles · Process Description and Control. Process Control Block. Structure of Process ... A process switch may occur any time

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

Page 14: Chapter 3 Process Description Design and Control Principles · Process Description and Control. Process Control Block. Structure of Process ... A process switch may occur any time

Process has PCB and Address spaceThread has TCB and Stacks

Process has PCB and Address spaceThread has TCB and Stacks

Page 15: Chapter 3 Process Description Design and Control Principles · Process Description and Control. Process Control Block. Structure of Process ... A process switch may occur any time

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

Page 16: Chapter 3 Process Description Design and Control Principles · Process Description and Control. Process Control Block. Structure of Process ... A process switch may occur any time

Types of Threads

Kernel level Thread (KLT)

User Level Thread (ULT)

Page 17: Chapter 3 Process Description Design and Control Principles · Process Description and Control. Process Control Block. Structure of Process ... A process switch may occur any time

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

Page 18: Chapter 3 Process Description Design and Control Principles · Process Description and Control. Process Control Block. Structure of Process ... A process switch may occur any time

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

Page 19: Chapter 3 Process Description Design and Control Principles · Process Description and Control. Process Control Block. Structure of Process ... A process switch may occur any time

Relationships Between ULTStates and Process States

Relationships Between ULTStates and Process States

Page 21: Chapter 3 Process Description Design and Control Principles · Process Description and Control. Process Control Block. Structure of Process ... A process switch may occur any time

Combined Approaches

Thread creation is donein the user space

Bulk of scheduling andsynchronization of threads is by the application

Solaris is an example

Page 22: Chapter 3 Process Description Design and Control Principles · Process Description and Control. Process Control Block. Structure of Process ... A process switch may occur any time

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

Page 23: Chapter 3 Process Description Design and Control Principles · Process Description and Control. Process Control Block. Structure of Process ... A process switch may occur any time

Processes and Threadsin Solaris

Processes and Threadsin Solaris

Page 24: Chapter 3 Process Description Design and Control Principles · Process Description and Control. Process Control Block. Structure of Process ... A process switch may occur any time

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

Page 25: Chapter 3 Process Description Design and Control Principles · Process Description and Control. Process Control Block. Structure of Process ... A process switch may occur any time

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

Page 26: Chapter 3 Process Description Design and Control Principles · Process Description and Control. Process Control Block. Structure of Process ... A process switch may occur any time

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

Page 27: Chapter 3 Process Description Design and Control Principles · Process Description and Control. Process Control Block. Structure of Process ... A process switch may occur any time

Relationship Between Threads and ProcessesRelationship Between Threads and Processes

Page 28: Chapter 3 Process Description Design and Control Principles · Process Description and Control. Process Control Block. Structure of Process ... A process switch may occur any time

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);}

}

Page 29: Chapter 3 Process Description Design and Control Principles · Process Description and Control. Process Control Block. Structure of Process ... A process switch may occur any time

Java Threads

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

Java threads are managed by the JVM

Page 30: Chapter 3 Process Description Design and Control Principles · Process Description and Control. Process Control Block. Structure of Process ... A process switch may occur any time

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

Page 31: Chapter 3 Process Description Design and Control Principles · Process Description and Control. Process Control Block. Structure of Process ... A process switch may occur any time

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