UnitII Process Threads Contents Process State Transition Deadlock

  • Slides: 44
Download presentation
Unit-II Process & Threads

Unit-II Process & Threads

Contents : • Process State Transition • Deadlock

Contents : • Process State Transition • Deadlock

Process State Transition user running 1 system call or interrupt kernel running return 2

Process State Transition user running 1 system call or interrupt kernel running return 2 schedule process sleep asleep 4 context switch permissible Interrupt return wakeup 3 ready to run

Example program

Example program

Example program (1) At initial, the process is running

Example program (1) At initial, the process is running

Example program (2) Transfer to waiting state while waiting the user input from the

Example program (2) Transfer to waiting state while waiting the user input from the keyboard

Example program (3) Transfer to Ready state after the user inputs something from the

Example program (3) Transfer to Ready state after the user inputs something from the keyboard

Example program (4) Regain the CPU after the OS picks it up

Example program (4) Regain the CPU after the OS picks it up

Process State Transition : User Running interrupt, interrupt return 9 4 Asleep in Memory

Process State Transition : User Running interrupt, interrupt return 9 4 Asleep in Memory sleep swap out Sleep, Swapped 7 reschedule process 3 Ready to Run in Memory enough mem Created swap out wakeup Preempted Kernel Running preempt wakeup 6 return to user return 2 Zombie 1 system call, interrupt 8 swap in 5 fork not enough mem (swapping system only) Ready to Run, Swapped

Deadlocks : • • Request Use Release Resource Allocation Graph • Request Edge •

Deadlocks : • • Request Use Release Resource Allocation Graph • Request Edge • Assignment Edge Pi -> Rj Rj -> Pi • Necessary Conditions • • Mutual Exclusion Hold and Wait No Preemption Circular Wait

Deadlock Handling : • Deadlock Prevention • Ensuring system will never enter deadlock state

Deadlock Handling : • Deadlock Prevention • Ensuring system will never enter deadlock state • Deadlock Avoidance • Banker’s Algorithm • Need information regarding resources in advance • Ostrich Algorithm • Deadlock Detection

Deadlock Prevention : • Mutual Exclusion • For non-sharable resources only • Hold and

Deadlock Prevention : • Mutual Exclusion • For non-sharable resources only • Hold and Wait • No Preemption • Preemption of resources • Circular Wait • Total Ordering

Deadlock Detection : • Single Instance of Each Resource • Wait for graph •

Deadlock Detection : • Single Instance of Each Resource • Wait for graph • Several Instance of Each Resource • Available , Request , Allocation • Detection of Unsafe State

Deadlock Recovery : • Process Termination • Abort all- simple but costly • Abort

Deadlock Recovery : • Process Termination • Abort all- simple but costly • Abort one at a time- effective but overhead • • Priority Process time Simple to preempt How many more • Resource Preemption • Selecting a Victim • Rollback • Starvation

Layout of System Memory : • Regions • • • Pointer to inode Region

Layout of System Memory : • Regions • • • Pointer to inode Region Type(text, shared mem, private data, stack) Size Location in physical mem Reference Count Status • • Locked In demand In the process of being loaded into mem Valid, loaded into mem

Pregion : per process region • Pointers to region table • Permissions RO, RW,

Pregion : per process region • Pointers to region table • Permissions RO, RW, RE • Starting virtual address

Layout of System Memory : Region Per Proc Region Tables (Virtual Addresses) Process A

Layout of System Memory : Region Per Proc Region Tables (Virtual Addresses) Process A Text 8 K b Data 16 K c Stack 32 K a Text 4 K Process Data 8 K B Stack 32 K e d <Processes and Regions>

Memory Management Register Triples : • Address of page table in physical memory •

Memory Management Register Triples : • Address of page table in physical memory • First virtual address mapped via triple • Control info i. e. no of pages and page permissions • Exceptions • Out of Memory Access • Permissions Error • Separate U Area for each process accessed via 3 rd kernel triple.

Memory Map of U area in kernel :

Memory Map of U area in kernel :

Process Context : P 1 P 2 R 0 = 3 R 0 =

Process Context : P 1 P 2 R 0 = 3 R 0 = 2 R 2 = R 0 + 1 R 2 = R 0

Context Of Process : • Contents of user address space • Contents of Hardware

Context Of Process : • Contents of user address space • Contents of Hardware register • Contents of kernel data structures

User Level Context: • • • Text Data Stack Heap Shared Memory • In

User Level Context: • • • Text Data Stack Heap Shared Memory • In Virtual Memory due to swapping and paging of processes

Example : MAX … int aa; char buf[1000]; void foo() { int a; …

Example : MAX … int aa; char buf[1000]; void foo() { int a; … } main() { int b; char *p; p = new char[1000]; foo(); } (b, *p) - main (a) - foo heap (p) (char[1000]) data (aa, buf) text (code) 0 Process memory stack

Register Context : • Program Counter • Execution Address • Processor status Register •

Register Context : • Program Counter • Execution Address • Processor status Register • Reg result +ve, -ve, overflow, mode of execution • Stack Pointer • General Purpose Register • Reg 0 and reg 1 for general information transfer

System Level Context : • Process Table Entry • State Static • U Area

System Level Context : • Process Table Entry • State Static • U Area • Process control info • Pregion • Page Table, VM to PM address mapping • Defines text, stack and data • Set of layers-LIFO Stack • Kernel Stack

Components of Context of Process :

Components of Context of Process :

Layers : • Interrupt Level Layers • • • Software Disk Clock Terminal Other

Layers : • Interrupt Level Layers • • • Software Disk Clock Terminal Other peripherals • Layer for System calls • Layer for user level

Saving the Context of a Process : • When do we need to save

Saving the Context of a Process : • When do we need to save the context • Interrupt • System calls • Context Switch

Interrupt and Exceptions : • Save current reg context create new one • Push

Interrupt and Exceptions : • Save current reg context create new one • Push to stack • Determine cause using interrupt vector table • Interrupt Vector Table • Kernel invokes interrupt handler • Kernel stack – executing process/global interrupt • Interrupt handler completes work and return • Restores previous context (pop)

System calls : Read(fd, &buff, nbytes)

System calls : Read(fd, &buff, nbytes)

System calls : • • • Find entry in system call table Determine system

System calls : • • • Find entry in system call table Determine system call parameters Save current context Invoke system call code in kernel Update register context • If error • Set reg 0 to error no • Set carry bit in PS reg • Else • Set PS reg 0 and 1 • Return the values from system call

Context Switch : • • Sleep Exit Returns to user mode after system call

Context Switch : • • Sleep Exit Returns to user mode after system call not eligible to run Returns to user mode after interrupt not eligible to run • • Whether to do context switch or not and is it permitted Save old context Find next process to schedule Restore its context

Region Table Entry : • Regions • • • Pointer to inode Region Type(text,

Region Table Entry : • Regions • • • Pointer to inode Region Type(text, shared mem, private data, stack) Size Location in physical mem Reference Count Status • • Locked In demand In the process of being loaded into mem Valid, loaded into mem

Manipulation of the Process Address space : • • • Lock Region Unlock Region

Manipulation of the Process Address space : • • • Lock Region Unlock Region Allocate Region (allocreg) Attaching a Region to a process (attachreg) Changing size of Region (growreg) Loading a Region (loadreg) Freeing a Region (freereg) Detaching a Region from a process (detachreg) Duplicating a region (dupreg)

Allocreg( ) : • • remove region from linked list of free regions; assign

Allocreg( ) : • • remove region from linked list of free regions; assign region type; assign region inode pointer; if (inode pointer not null) • increment inode reference count; • place region on linked list of active regions; • return (locked region) ;

Attachreg( ) : • allocate per process region table entry for process; • initialize

Attachreg( ) : • allocate per process region table entry for process; • initialize per process region table entry: • set pointer to region being attached; • set type field; • set virtual address field; • • • check legality of virtual address, region size; increment region reference count; increment process size according to attached region; initialize new hardware register triple for process; return (per process region table entry) ;

Growreg( ) : • if (region size increasing) • check legality of new region

Growreg( ) : • if (region size increasing) • check legality of new region size; • allocate required tables (page tables) ; • if (not system supporting demand paging) • allocate physical memory; • initialize required tables, as necessary; • else • free physical memory, as appropriate; • free required tables, as appropriate; • do initialization of required tables, as necessary; • set size field in process table;

Loadreg ( ) : • • increases region size according to eventual size of

Loadreg ( ) : • • increases region size according to eventual size of region mark region state: being loaded into memory; unlock region ; set up u area parameters for reading file: • target virtual address where data is read to • start offset value for reading file • count of bytes to read from file • • read file into region lock region; mark region state: completely loaded into memory; awaken all processes waiting for region to be loaded;

Freereg ( ) : • if (region reference count non zero) • release region

Freereg ( ) : • if (region reference count non zero) • release region lock; • If (region has an associated inode) • release inode lock; • return; • if (region has associated inode) • release inode • • • free physical memory still associated with region; free required tables associated with region; clear region fields; place region on region free list; unlock region;

Detachreg ( ) : • • • get required memory management tables for process,

Detachreg ( ) : • • • get required memory management tables for process, release as appropriate; decrement process size; decrement region reference count; if (region reference count is 0 and region not sticky bit) • free region (algorithm freereg) ; • Else// if region not req anymore • free inode lock; • free region lock;

Dupreg( ) : • if (region type shared) • return(input region pointer); allocate new

Dupreg( ) : • if (region type shared) • return(input region pointer); allocate new region (algorithm allocreg) ; set up required memory management structures; allocate physical memory for region contents; copy region contents from input region to newly allocated region; • return(pointer to allocated region) ; • •

Sleep :

Sleep :

Process creation :

Process creation :