SlideShare a Scribd company logo
1
Structure of Processes
2
Contents
Process States and Transitions
Layout of System Memory
The Context of a Process
Saving the Context of a Process
Manipulation of the Process Address Space
3
Process States and Transitions
1
2
9
7
34
6 5
8
User Running
Preempted
Zombie
Asleep
in
Memory
Sleep, Swapped Ready to Run, Swapped
fork
Created
Ready to
Run in Memory
Kernel
Running
not enough mem
(swapping system only)
swap
out
swap
in
wakeup
swap
out
wakeup enough mem
sleep
reschedule
process
preempt
return
return
to user
system call,
interruptinterrupt,
interrupt return
exit
4
1. User Running: The process is executing in user mode.
2. Kernel Running: The process is executing in kernel mode.
3. Ready to Run in Memory: The process is not executing but is ready to
run as soon as the kernel schedules it.
4. Asleep in Memory: The process is sleeping and resides in main memory.
5. Ready to Run, Swapped: The process is ready to run, but the swapper
(process 0) must swap the process into main memory before the kernel
can schedule it to execute.
6. Sleep, Swapped: The process is sleeping, and the swapper has swapped
the process to secondary storage to make room for other processes in
main memory.
Process States and Transitions
5
7. Preempted: The process is returning from the kernel to user mode, but the
kernel preempts it and does a context switch to schedule another process.
8. Created: The process is newly created and is in a transition state; the
process exists, but it is not ready to run, nor is it sleeping. This state is the
start state for all processes except process 0.
9. Zombie: The process executed the exit system call and is in the zombie
state. The process no longer exists, but it leaves a record containing an exit
code and some timing statistics for its parent process to collect. The
zombie state is the final state of a process.
Process States and Transitions
6
Process Structure
Text
Data
Stack
Process consists of 3 regions. Region is a contiguous area
of the virtual address space.
7
State of a Process
– Process table entry
• Contains general fields of processes that must be
always be accessible to the kernel.
– U area
• Contains fields that only need to be accessible to
the running process.
8
Data Structures for a Process
U Area
Process table
Per process region table allows independent processes
to share regions.
text
data
stack
Per Process Region table
Region Table
Main Memory
9
User Area - U
Text
Data
Stack
•Each process has a user area.
• User area (U) has a fixed virtual address; it is mapped to different physical
address.
•Each user area is mapped to a physical memory when process is loaded to
memory.
U Area
Text
Data
Stack
Process A
Process B
U Area
Same virtual
address
Physical memory
Physical memory
10
Process table fields
• State field: Identifies Process state. e.g. user running,
kernel running, etc.
• Fields that allow the kernel to locate the process and u
area in Main Memory and Secondary Storage.
(Requires while context switch.)
– Process size: kernel know how much space to allocate
for the process.
• User ID
• Process ID
11
Process table fields (cont…)
• Event descriptor:
– Used when the process is in the "sleep" state.
(Sleep/Wakeup).
• Scheduling parameters:
– Allow the kernel to determine the order in which
processes move to the states "kernel running" and "user
running”.
• A Signal field:
– keeps the signals sent to a process but not yet handled.
• Various timers: process execution time, resource
utilization, etc.
12
U Area fields
• A pointer to the process table entry.
• Real and Effective User IDs
• Timer fields:
– Execution time in user mode
– Execution Time in kernel mode
• An error field: keeps error during system call
• Return value field: result of system call.
• I/O parameters:
– Amount of data transfer
– Address of source and target etc.
13
U Area (cont...)
• The current directory and current root
• User file descriptor table
• Limit fields
– Restrict process size
– Restrict size of the file it can write
• The control terminal field:
– login terminal associated with the process, if one exists
• An array
– indicates how the process wishes to react to signal.
• A permission modes field.
14
Layout of System Memory
Text
Data
Stack
•Process consists of 3 regions.
•Region is a contiguous area of the virtual address space
of a process.
Set of instructions, includes text
addresses.
Data addresses (to access global
data variables.)
Stack addresses (to access data
structures local to subroutine)
15
Regions
• Divides virtual address space of a process
into logical regions.
• Region : Contiguous area of the virtual
address space of a process
16
Per Process Region Table (Pregion)
• Pregion : region = File table : inode
• Each pregion entry points to the kernel region
table
• Starting virtual (absolute) address of the region
• Permission filed:
– read-only, read-write, read-execute
17
Kernel Region table
• Kernel region table contains the pointer to
the page table which keeps the physical
memory address
18
Regions
8K
16K
32K
b
a
d
c
e
4K
8K
32K
Text
Data
Stack
Text
Data
Stack
Process
A
Process
B
Per Proc Region Tables
(Virtual Addresses)
Region
<Processes and Regions>
• A -> 8K
B -> 4K
reading,
region 'a’
• Data and stack
region for two
processes are
private.
19
Pages and Page Tables
• Memory Management polices are
Paging
Segmentation
• Pages and page table
 Divides Physical Memory in to set of equal
sized blocks called pages.
Page size range: 512 bytes to 4KB
Every memory location is addressed by a pair of
(page number, byte offset in page)
20
Pages and Page Tables
Logical Page Number Physical Page Number
0 177
1 54
2 209
3 17
Logical page numbers -> Physical page numbers
Virtual addresses of a region -> Physical machine addresses
21
Pages and Page Tables
• Region : A contiguous range of virtual
addresses in a program.
– logical page number : index into an array
of physical page number
• Region table contains the pointer to a table
of physical page no. called page table.
• Page table contain m/c dependent
information such as permission bits(to allow
reading & writing page).
22
Pages and Page Tables (contd.)
8K
32K
64K
Text
Data
Stack
541K
783K
986K
897K
87K
552K
727K
941K
empty
137K
852K
764K
.
.
.
.
.
.
.
.
1096K
2001K
433K
333K
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Per Proc Region Table
Page Tables(Physical Addresses)
Virtual Addresses
Fig: Mapping Virtual Addresses to Physical Address
23
Layout of Kernel
• Kernel executes in the context of a process
– virtual memory mapping associated with the
kernel is independent of all processes.
• Interrupt or system call
– user mode -> kernel mode ; OS
– user mode OS
24
Layout of Kernel
856K
917K
564K
444K
.
.
.
.
.
.
.
.
0
1M
2M
4M
Kernel Reg Triple 1
Kernel Reg Triple 2
Kernel Reg Triple 3
User Reg Triple 1
User Reg Triple 2
User Reg Triple 3
Address of
Page Table
No. of Pages
in Page TableVirtual Addr
747K
950K
333K
.
.
.
.
.
.
.
.
.
.
.
.
556K
997K
458K
632K
.
.
.
.
.
.
.
.
0K
4K
3K
17K
.
.
.
.
.
.
.
.
128K
97K
135K
139K
.
.
.
.
.
.
.
.
256K
292K
304K
279K
.
.
.
.
.
.
.
.
Process(Region) Page Tables Kernel Page Tables
Fig: Changing Mode from User to Kernel
25
U-area (User)
• Every process has a private u area.
• The two physical address represent the u areas of
two processes,
– but the kernel accesses them via the same virtual address
• A process can access its u area when it executes in
kernel mode.(not user mode)
26
U-area (User)
856K
917K
564K
444K
Reg Triple 1
2M 4
Address of
Page Table
Page Tables for U Areas
No. of Pages
in Page Table
Virtual Addr
in Process
Reg Triple 2
Reg Triple 3(U Area)
Proc B
856K
917K
564K
444K
Proc A
856K
917K
564K
444K
Proc C
856K
917K
564K
444K
Proc D
Fig: Memory Map of U Area in the Kernel
data
text
27
Context of a Process
• Context of a process
– Contents of user address space (user-level context)
– Contents of hardware registers (register context)
– kernel data structure that relate to the process (system-level
context)
• User-level context
– text, data, user stack, shared memory
• register context
• Program Counter (PC) – specifies address of the next instruction the
CPU will execute.
• Processor Status register (PS) – Specifies h/w status of machine.
• User Stack Pointer (SP) – contains current address of next entry in
kernel or user stack.
• General purpose registers – contain data generated by the process
during execution.
28
Context of a Process
• System-level context
• Static Part-
– Process table entry,
– U area of a Process,
– Pregion entries , region tables , page tables
• Dynamic Part-
– Kernel Stack - Stack Frames,
– Set of layers.
29
Context of a Process
Dynamic Portion of Context
Static Part of
System Level context
Process Text
Data
Stack
Shared Data
Process Table Entry
U Area
Per Process Region Table
User Level context
Static Portion of Context
Layer 3
Layer 2
Layer 1
Layer 0
Kernel
Context (User Level)
Kernel Stack for Layer 3
Saved Register Context
for Layer2
.
.
.
.
.
Kernel Stack for Layer 2
Saved Register Context
for Layer1
Kernel Stack for Layer 1
Saved Register Context
for Layer0
logical pointer
to current context
layer
Fig: Components of the Context of a Process>
30
Saving the Context of a Process
• Kernel saves the context of a process
– Whenever it pushes a new system context layer.
• Interrupts and Exception
• System call Interface
• Context switch.
31
Interrupts and Exception
• System is responsible for handling interrupts
– result from hardware (clock, peripheral)
– programmed interrupt (software interrupt)
– exceptions (page faults)
• Integrity of kernel data structure
32
Interrupts and Exception
• Sequence of interrupt operation
1. Saves the current register context of the
executing process and creates(push) a new
context layer.
2. Determine interrupt source and cause.
• find interrupt vector.
1. Kernel invokes interrupt handler.
2. Interrupt handler completes it work and return.
• Restores previous context layer.
33
Interrupts and Exception
34
System Call Interface
• Usual calling sequence
– cannot change the mode of a process form user
to kernel
• Library : have the system call name
– User program system call
• OS trap
• Same as Interrupt Handler
35
Context Switch
• Kernel permits a context switch
– When a process puts itself to sleep
– When it exits
– When it returns from a system call to user mode but is
not the most eligible process to run
– When it returns to user mode after the kernel completes
handling an interrupt but is not the most eligible process
to run.
36
Context Switch
• Ensure integrity and consistency.
• Procedure for a context switch.
– similar to handling interrupts and system call
• Steps for a Context Switch
1. Decide whether to do a context switch, and whether a context
switch is permissible now.
2. Save the context of the "old" process.
3. Find the "best" process to schedule for execution, using the
process scheduling algorithm.
4. Restore its context.
37
Manipulation of Process Address Space
• System calls Manipulate the virtual address
space of a process.
• The region table entry contains the
information necessary to describe a region.
38
Manipulation of Process Address Space
• Region Table contains the following entries:
– A pointer to the Inode of the file whose contents were
originally loaded into the region.
– Region type (text, shared memory, private data or stack).
– Size of the region.
– The location of the region in physical memory.
– The status of a region, which may be a combination of
• Locked
• In demand
• In the process of being loaded into memory
• Valid, loaded into memory
– The reference count, giving the number of processes
that reference the region.
39
Manipulation of Process Address Space
• Operations on Region:
– Locking and unlocking a Region.
– Allocating a Region.
– Attaching a Region to a Process.
– Changing the Size of a Region.
– Loading a Region.
– Freeing a Region.
– Detaching a region from a Process.
– Duplicating a Region.
40
Locking and unlocking a Region
• Kernel has operations to lock and unlock a
region
– independent of the operations to allocate and free
a region.
• Thus the kernel can lock and allocate a
region.
• Later can unlock it without having to free the
region.
41
Allocating a Region
• Allocate a new region during fork, exec, shmget
(shared memory) system calls.
42
Attaching a Region to a Process
• Attach a region to a process during fork, Exec, and shmat system
calls.
• It connects the region to the process address space.
43
Changing the size of a Region
• Kernel invoke growreg to change the size of the
region.
1. Process expands size by executing sbrk system call.
2. Stack expand explicitly according to the depth of nested
procedure call.
• The virtual space of the expanded region shouldn't
overlap with others.
• The process size shouldn’t exceed the maximum
size.
• The shared region never increase in size if it is
attached to other processes.
44
Changing the size of a Region
45
Loading a Region• Allocate memory to load a file (growreg).
• Load a file on demand if on demand paging is supported.
46
Freeing a Region
• The region will be freed when it is not attached to any process (ref
count =0).
• Free inode associated with region using iput.
47
Detaching a Region from a Process
• The kernel detaches regions in the exec, exit, and shmdt (detach
shared memory).
• Decrement process size.
• Decrement region reference count..
48
Duplicating a Region
• Fork requires the kernel to duplicate data and
stack regions.
• The Region reference count is incremented in
case of shared text & memory, allowing the
parent & child processes to share regions.
• In case of stack & data regions are copied:
1. Allocate a new region entry.
2. Allocate page map table.
3. Allocate physical memory for the region.
49
Duplicating a Region
50
Duplicating a Region

More Related Content

PPTX
Register organization, stack
PPTX
Join ordering in fragment queries
PPTX
database recovery techniques
PPTX
Planning in AI(Partial order planning)
PPT
Query optimization and processing for advanced database systems
PPTX
Segmentation in Operating Systems.
PPTX
contiguous memory allocation.pptx
PPT
Context Switching
Register organization, stack
Join ordering in fragment queries
database recovery techniques
Planning in AI(Partial order planning)
Query optimization and processing for advanced database systems
Segmentation in Operating Systems.
contiguous memory allocation.pptx
Context Switching

What's hot (20)

PPT
Classical problem of synchronization
PPTX
Distributed DBMS - Unit 6 - Query Processing
PPT
Multi core-architecture
PPTX
instruction format.pptx
PPTX
Structure of processes ppt
PPT
Disk scheduling
PPTX
Concurrency Control in Distributed Database.
PPTX
Curve and text clipping
PPTX
Operating system Dead lock
PPTX
Deadlock Prevention
PPT
Paging.ppt
PPTX
Operating system memory management
PPTX
Lecture 5 inter process communication
PDF
Unit 3
PPTX
Segmentation in operating systems
PPTX
Frame buffer
PDF
4. THREE DIMENSIONAL DISPLAY METHODS
PPT
Unit 6 interprocessor arbitration
PPTX
serializability in dbms
PPTX
Pci,usb,scsi bus
Classical problem of synchronization
Distributed DBMS - Unit 6 - Query Processing
Multi core-architecture
instruction format.pptx
Structure of processes ppt
Disk scheduling
Concurrency Control in Distributed Database.
Curve and text clipping
Operating system Dead lock
Deadlock Prevention
Paging.ppt
Operating system memory management
Lecture 5 inter process communication
Unit 3
Segmentation in operating systems
Frame buffer
4. THREE DIMENSIONAL DISPLAY METHODS
Unit 6 interprocessor arbitration
serializability in dbms
Pci,usb,scsi bus
Ad

Similar to The structure of process (20)

PPT
Operating System
PPT
Unit 3.1 The Structure of Process, Process Control, Process Scheduling.ppt
PPTX
02-OS-review.pptx
PPTX
Memory Management techniques -ch8_1.pptx
PPT
Bab 4
 
PPT
PPTX
Memory Managment(OS).pptx
PPT
Kernal
PPTX
Main Memory
PDF
Process management
PDF
Ch8 main memory
PPT
Chapter 8 - Main Memory
PPTX
Main Memory Management in Operating System
PDF
muja osjkkhkhkkhkkkfdxfdfddkhvjlbjljlhgg
PPT
Memory management
PDF
SO-Memoria.pdf
PDF
SO-Memoria.pdf
PPT
Chapter 8 : Memory
Operating System
Unit 3.1 The Structure of Process, Process Control, Process Scheduling.ppt
02-OS-review.pptx
Memory Management techniques -ch8_1.pptx
Bab 4
 
Memory Managment(OS).pptx
Kernal
Main Memory
Process management
Ch8 main memory
Chapter 8 - Main Memory
Main Memory Management in Operating System
muja osjkkhkhkkhkkkfdxfdfddkhvjlbjljlhgg
Memory management
SO-Memoria.pdf
SO-Memoria.pdf
Chapter 8 : Memory
Ad

Recently uploaded (20)

PDF
Well-logging-methods_new................
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PPT
Project quality management in manufacturing
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
Geodesy 1.pptx...............................................
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
Digital Logic Computer Design lecture notes
PPTX
Construction Project Organization Group 2.pptx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
PPT on Performance Review to get promotions
PPTX
Sustainable Sites - Green Building Construction
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
DOCX
573137875-Attendance-Management-System-original
Well-logging-methods_new................
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
Project quality management in manufacturing
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
bas. eng. economics group 4 presentation 1.pptx
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Automation-in-Manufacturing-Chapter-Introduction.pdf
Geodesy 1.pptx...............................................
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Operating System & Kernel Study Guide-1 - converted.pdf
Model Code of Practice - Construction Work - 21102022 .pdf
Digital Logic Computer Design lecture notes
Construction Project Organization Group 2.pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPT on Performance Review to get promotions
Sustainable Sites - Green Building Construction
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
573137875-Attendance-Management-System-original

The structure of process

  • 2. 2 Contents Process States and Transitions Layout of System Memory The Context of a Process Saving the Context of a Process Manipulation of the Process Address Space
  • 3. 3 Process States and Transitions 1 2 9 7 34 6 5 8 User Running Preempted Zombie Asleep in Memory Sleep, Swapped Ready to Run, Swapped fork Created Ready to Run in Memory Kernel Running not enough mem (swapping system only) swap out swap in wakeup swap out wakeup enough mem sleep reschedule process preempt return return to user system call, interruptinterrupt, interrupt return exit
  • 4. 4 1. User Running: The process is executing in user mode. 2. Kernel Running: The process is executing in kernel mode. 3. Ready to Run in Memory: The process is not executing but is ready to run as soon as the kernel schedules it. 4. Asleep in Memory: The process is sleeping and resides in main memory. 5. Ready to Run, Swapped: The process is ready to run, but the swapper (process 0) must swap the process into main memory before the kernel can schedule it to execute. 6. Sleep, Swapped: The process is sleeping, and the swapper has swapped the process to secondary storage to make room for other processes in main memory. Process States and Transitions
  • 5. 5 7. Preempted: The process is returning from the kernel to user mode, but the kernel preempts it and does a context switch to schedule another process. 8. Created: The process is newly created and is in a transition state; the process exists, but it is not ready to run, nor is it sleeping. This state is the start state for all processes except process 0. 9. Zombie: The process executed the exit system call and is in the zombie state. The process no longer exists, but it leaves a record containing an exit code and some timing statistics for its parent process to collect. The zombie state is the final state of a process. Process States and Transitions
  • 6. 6 Process Structure Text Data Stack Process consists of 3 regions. Region is a contiguous area of the virtual address space.
  • 7. 7 State of a Process – Process table entry • Contains general fields of processes that must be always be accessible to the kernel. – U area • Contains fields that only need to be accessible to the running process.
  • 8. 8 Data Structures for a Process U Area Process table Per process region table allows independent processes to share regions. text data stack Per Process Region table Region Table Main Memory
  • 9. 9 User Area - U Text Data Stack •Each process has a user area. • User area (U) has a fixed virtual address; it is mapped to different physical address. •Each user area is mapped to a physical memory when process is loaded to memory. U Area Text Data Stack Process A Process B U Area Same virtual address Physical memory Physical memory
  • 10. 10 Process table fields • State field: Identifies Process state. e.g. user running, kernel running, etc. • Fields that allow the kernel to locate the process and u area in Main Memory and Secondary Storage. (Requires while context switch.) – Process size: kernel know how much space to allocate for the process. • User ID • Process ID
  • 11. 11 Process table fields (cont…) • Event descriptor: – Used when the process is in the "sleep" state. (Sleep/Wakeup). • Scheduling parameters: – Allow the kernel to determine the order in which processes move to the states "kernel running" and "user running”. • A Signal field: – keeps the signals sent to a process but not yet handled. • Various timers: process execution time, resource utilization, etc.
  • 12. 12 U Area fields • A pointer to the process table entry. • Real and Effective User IDs • Timer fields: – Execution time in user mode – Execution Time in kernel mode • An error field: keeps error during system call • Return value field: result of system call. • I/O parameters: – Amount of data transfer – Address of source and target etc.
  • 13. 13 U Area (cont...) • The current directory and current root • User file descriptor table • Limit fields – Restrict process size – Restrict size of the file it can write • The control terminal field: – login terminal associated with the process, if one exists • An array – indicates how the process wishes to react to signal. • A permission modes field.
  • 14. 14 Layout of System Memory Text Data Stack •Process consists of 3 regions. •Region is a contiguous area of the virtual address space of a process. Set of instructions, includes text addresses. Data addresses (to access global data variables.) Stack addresses (to access data structures local to subroutine)
  • 15. 15 Regions • Divides virtual address space of a process into logical regions. • Region : Contiguous area of the virtual address space of a process
  • 16. 16 Per Process Region Table (Pregion) • Pregion : region = File table : inode • Each pregion entry points to the kernel region table • Starting virtual (absolute) address of the region • Permission filed: – read-only, read-write, read-execute
  • 17. 17 Kernel Region table • Kernel region table contains the pointer to the page table which keeps the physical memory address
  • 18. 18 Regions 8K 16K 32K b a d c e 4K 8K 32K Text Data Stack Text Data Stack Process A Process B Per Proc Region Tables (Virtual Addresses) Region <Processes and Regions> • A -> 8K B -> 4K reading, region 'a’ • Data and stack region for two processes are private.
  • 19. 19 Pages and Page Tables • Memory Management polices are Paging Segmentation • Pages and page table  Divides Physical Memory in to set of equal sized blocks called pages. Page size range: 512 bytes to 4KB Every memory location is addressed by a pair of (page number, byte offset in page)
  • 20. 20 Pages and Page Tables Logical Page Number Physical Page Number 0 177 1 54 2 209 3 17 Logical page numbers -> Physical page numbers Virtual addresses of a region -> Physical machine addresses
  • 21. 21 Pages and Page Tables • Region : A contiguous range of virtual addresses in a program. – logical page number : index into an array of physical page number • Region table contains the pointer to a table of physical page no. called page table. • Page table contain m/c dependent information such as permission bits(to allow reading & writing page).
  • 22. 22 Pages and Page Tables (contd.) 8K 32K 64K Text Data Stack 541K 783K 986K 897K 87K 552K 727K 941K empty 137K 852K 764K . . . . . . . . 1096K 2001K 433K 333K . . . . . . . . . . . . . . . . Per Proc Region Table Page Tables(Physical Addresses) Virtual Addresses Fig: Mapping Virtual Addresses to Physical Address
  • 23. 23 Layout of Kernel • Kernel executes in the context of a process – virtual memory mapping associated with the kernel is independent of all processes. • Interrupt or system call – user mode -> kernel mode ; OS – user mode OS
  • 24. 24 Layout of Kernel 856K 917K 564K 444K . . . . . . . . 0 1M 2M 4M Kernel Reg Triple 1 Kernel Reg Triple 2 Kernel Reg Triple 3 User Reg Triple 1 User Reg Triple 2 User Reg Triple 3 Address of Page Table No. of Pages in Page TableVirtual Addr 747K 950K 333K . . . . . . . . . . . . 556K 997K 458K 632K . . . . . . . . 0K 4K 3K 17K . . . . . . . . 128K 97K 135K 139K . . . . . . . . 256K 292K 304K 279K . . . . . . . . Process(Region) Page Tables Kernel Page Tables Fig: Changing Mode from User to Kernel
  • 25. 25 U-area (User) • Every process has a private u area. • The two physical address represent the u areas of two processes, – but the kernel accesses them via the same virtual address • A process can access its u area when it executes in kernel mode.(not user mode)
  • 26. 26 U-area (User) 856K 917K 564K 444K Reg Triple 1 2M 4 Address of Page Table Page Tables for U Areas No. of Pages in Page Table Virtual Addr in Process Reg Triple 2 Reg Triple 3(U Area) Proc B 856K 917K 564K 444K Proc A 856K 917K 564K 444K Proc C 856K 917K 564K 444K Proc D Fig: Memory Map of U Area in the Kernel data text
  • 27. 27 Context of a Process • Context of a process – Contents of user address space (user-level context) – Contents of hardware registers (register context) – kernel data structure that relate to the process (system-level context) • User-level context – text, data, user stack, shared memory • register context • Program Counter (PC) – specifies address of the next instruction the CPU will execute. • Processor Status register (PS) – Specifies h/w status of machine. • User Stack Pointer (SP) – contains current address of next entry in kernel or user stack. • General purpose registers – contain data generated by the process during execution.
  • 28. 28 Context of a Process • System-level context • Static Part- – Process table entry, – U area of a Process, – Pregion entries , region tables , page tables • Dynamic Part- – Kernel Stack - Stack Frames, – Set of layers.
  • 29. 29 Context of a Process Dynamic Portion of Context Static Part of System Level context Process Text Data Stack Shared Data Process Table Entry U Area Per Process Region Table User Level context Static Portion of Context Layer 3 Layer 2 Layer 1 Layer 0 Kernel Context (User Level) Kernel Stack for Layer 3 Saved Register Context for Layer2 . . . . . Kernel Stack for Layer 2 Saved Register Context for Layer1 Kernel Stack for Layer 1 Saved Register Context for Layer0 logical pointer to current context layer Fig: Components of the Context of a Process>
  • 30. 30 Saving the Context of a Process • Kernel saves the context of a process – Whenever it pushes a new system context layer. • Interrupts and Exception • System call Interface • Context switch.
  • 31. 31 Interrupts and Exception • System is responsible for handling interrupts – result from hardware (clock, peripheral) – programmed interrupt (software interrupt) – exceptions (page faults) • Integrity of kernel data structure
  • 32. 32 Interrupts and Exception • Sequence of interrupt operation 1. Saves the current register context of the executing process and creates(push) a new context layer. 2. Determine interrupt source and cause. • find interrupt vector. 1. Kernel invokes interrupt handler. 2. Interrupt handler completes it work and return. • Restores previous context layer.
  • 34. 34 System Call Interface • Usual calling sequence – cannot change the mode of a process form user to kernel • Library : have the system call name – User program system call • OS trap • Same as Interrupt Handler
  • 35. 35 Context Switch • Kernel permits a context switch – When a process puts itself to sleep – When it exits – When it returns from a system call to user mode but is not the most eligible process to run – When it returns to user mode after the kernel completes handling an interrupt but is not the most eligible process to run.
  • 36. 36 Context Switch • Ensure integrity and consistency. • Procedure for a context switch. – similar to handling interrupts and system call • Steps for a Context Switch 1. Decide whether to do a context switch, and whether a context switch is permissible now. 2. Save the context of the "old" process. 3. Find the "best" process to schedule for execution, using the process scheduling algorithm. 4. Restore its context.
  • 37. 37 Manipulation of Process Address Space • System calls Manipulate the virtual address space of a process. • The region table entry contains the information necessary to describe a region.
  • 38. 38 Manipulation of Process Address Space • Region Table contains the following entries: – A pointer to the Inode of the file whose contents were originally loaded into the region. – Region type (text, shared memory, private data or stack). – Size of the region. – The location of the region in physical memory. – The status of a region, which may be a combination of • Locked • In demand • In the process of being loaded into memory • Valid, loaded into memory – The reference count, giving the number of processes that reference the region.
  • 39. 39 Manipulation of Process Address Space • Operations on Region: – Locking and unlocking a Region. – Allocating a Region. – Attaching a Region to a Process. – Changing the Size of a Region. – Loading a Region. – Freeing a Region. – Detaching a region from a Process. – Duplicating a Region.
  • 40. 40 Locking and unlocking a Region • Kernel has operations to lock and unlock a region – independent of the operations to allocate and free a region. • Thus the kernel can lock and allocate a region. • Later can unlock it without having to free the region.
  • 41. 41 Allocating a Region • Allocate a new region during fork, exec, shmget (shared memory) system calls.
  • 42. 42 Attaching a Region to a Process • Attach a region to a process during fork, Exec, and shmat system calls. • It connects the region to the process address space.
  • 43. 43 Changing the size of a Region • Kernel invoke growreg to change the size of the region. 1. Process expands size by executing sbrk system call. 2. Stack expand explicitly according to the depth of nested procedure call. • The virtual space of the expanded region shouldn't overlap with others. • The process size shouldn’t exceed the maximum size. • The shared region never increase in size if it is attached to other processes.
  • 44. 44 Changing the size of a Region
  • 45. 45 Loading a Region• Allocate memory to load a file (growreg). • Load a file on demand if on demand paging is supported.
  • 46. 46 Freeing a Region • The region will be freed when it is not attached to any process (ref count =0). • Free inode associated with region using iput.
  • 47. 47 Detaching a Region from a Process • The kernel detaches regions in the exec, exit, and shmdt (detach shared memory). • Decrement process size. • Decrement region reference count..
  • 48. 48 Duplicating a Region • Fork requires the kernel to duplicate data and stack regions. • The Region reference count is incremented in case of shared text & memory, allowing the parent & child processes to share regions. • In case of stack & data regions are copied: 1. Allocate a new region entry. 2. Allocate page map table. 3. Allocate physical memory for the region.