context of process
TRANSCRIPT
![Page 1: Context of Process](https://reader030.vdocuments.us/reader030/viewer/2022020520/577cc7601a28aba711a0bfa8/html5/thumbnails/1.jpg)
Process and Memory ManagementThe lifetime of a process is between a fork and an exit (process state-transition diagram, states created 8 to zombie 9), and is largely spent sleeping, running or waiting to be scheduled.
The kernel's process table holds a list of all processes. ps shows most of the information about a P.
Some info about a P is accessible over its lifetime and some accessible to the kernel only when the P runs
Process table entry fields: state -running, sleeping,.. size and location of u area & pregion entry -
for swapping & switching UIDs user identifiers permitting sending
signals between processes PIDs unique process identifiers event descriptor - address of event to wakeup
a sleeping process scheduling parameters determine access to
CPU - system priority : user priority (PRI:NI)
array of signals not yet handled while swapped out
execution and kernel utilisation times
u area (user area) fields: pointer to process table entry UIDs to permit access rights time spent executing in user and kernel
modes an array containing signal handlers login terminal associated with the process error from a system call return value from a system call I/O parameters: amount of data to transfer,
the address of the source (or target) data array in user space, file offsets for I/O, etc.
current and root directory user file descriptor table records the files the
process has open size limits of a process and of a file it can
write permission of files the process creates
Context of a Unix process1. user-level2. register3. system-level
1. User-level context1. process text (instructions)2. data3. user stack4. any shared memory
2. Register context1. program counter - next instruction to run2. processor status register3. pointer to user or kernel stacks4. general registers
3. System-level context:static part - for the P's lifetime1. P table entry2. u area3. Per Process region (Pregion) entries, region tables, page tables
dynamic part - changes over P's lifetime1. kernel stack of current layer2. restorable registers of previous system context layers
document.doc 1
logical pointerto current contextlayer
Components of the Context of a Process
User Level Context
Static Part ofSystem Level Context
TextDataStack
Shared Data
Process Table EntryU Area
PerProcessRegion Table
Static Portion of Context
Layer 2
Kernel ContextLayer 0
Layer 1
Layer 3
(User stack)
Kernel Stack for Layer 1Saved Register Context
for Layer 0
Kernel Stack for Layer 2Saved Register Context
for Layer 1
Kernel Stack for Layer 3Saved Register Context
for Layer 2
Dynamic Portion of Context
:
121
321
21 1
push push push pop pop pop
![Page 2: Context of Process](https://reader030.vdocuments.us/reader030/viewer/2022020520/577cc7601a28aba711a0bfa8/html5/thumbnails/2.jpg)
A P mainly runs in user mode, so mainly operates in the user stack. When there is a system call or the P is involved in an interrupt, then the P runs in kernel mode and these instructions are protected by operating in the kernel stack. The figure shows the dynamic portion of the context: user and kernel stacks
Kernel pushes a context layer, on a 1. interrupt2. system call3. context switch
kernel pops a context layer when1. kernel returns from handling interrupt2. P returns to user mode after system call3. context switch
Context stack of a P can have 7 layers - a push for: each of the 5 prioritised interrupts, + a system call, + the user-level:
clock interruptdisk interrupttty interruptdevice interruptsoftware interruptsystem calluser-level
1. Interrupts are from hardware, software and users1. push current P context on stack2. handle interrupt3. pop (restore previous) context of same P
2. System callsare library functions, which issue an interrupt to change execution from user mode to kernel mode 1. push current P context on stack2. handle system call3. pop (restore previous) context of same P
3. Context switchingKernel allows a P to switch context, on:1. exit2. sleep3. return to user mode after a system call, but another P should run (priority, fairness)4. return to user mode after an interrupt, ditto
Kernel checks integrity of all data structures and locks before a switch1. push current P context on stack2. determine which P to schedule3. pop (restore previous) context of the scheduled P
document.doc 2
Interrupt Sequence
Disk Interrupt..............
Software Interrupt.........
Make System Call..........
Executing User Mode
Example of Interrupts
Kernel Context Layer 3 Execute Disk Interrupt
Handler
Save Register Contextof Software Interrupt
HandlerKernel Context Layer 2
Execute Software Interrupt Handler
Save Register Contextof Sys Call
Kernel Context Layer 1Execute Sys Call
Save Register Contextfor user layer
User Context Layer 0
(User Level)
User Context Layer 0
(User Level)
Typical Context Layers of a Sleeping Process
Kernel Context Layer 2
Execute Code for Context Switch
Save Register Context of Sys Call
Kernel Context Layer 1 Execute Sys Call
Save Register Context User Level
Invoke Sleep Algorithm.....
Make System Call .........
Executing User Mode
![Page 3: Context of Process](https://reader030.vdocuments.us/reader030/viewer/2022020520/577cc7601a28aba711a0bfa8/html5/thumbnails/3.jpg)
Process1 Process2 interruptI1I2I3I4suspend I1& sleep I2
I3I4suspend I1
I2resume I3
Wake & I5resume I6I5 suspend I6 & sleepI7I8 Wake &I9 resumeend I7
I8I9I10end
document.doc 3
I1I2I3I4I5I6I7I8I9I0I1
I1I2I3I4
I1I2I3I4I5
User process Interrupt/trap
User process interrupted by 2 routines in sequence
I1I2I3I4I5I6I7I8I9I0I1
I1I2I3I4
I1I2I3
User process interrupted. Interrupt routines interrupted
User process Interrupt/trap
I1I2I3I4I5I6I7I8I9
I1I2I3I4I5I6I7I8I9I10
I1I2I3
User process2 Interrupt/trap
2 concurrent user processes timeslicingprocess2 is interrupted
User process1
Processes/interrupts running.Context switch. Interrupt.
Process1
Process2
Process3
Process4
Processes wait for events2 processes can be woken by the same event
Key input
Disk input
Wait child death
![Page 4: Context of Process](https://reader030.vdocuments.us/reader030/viewer/2022020520/577cc7601a28aba711a0bfa8/html5/thumbnails/4.jpg)
The figure shows many items mentioned above. 2 Ps share text, ie they run the same code, eg after a forkThe global kernel process table has entries for each P; each P has its u area; each P has a Pregion (a P's text, data & stack regions); Pregions are mapped to the global region table; which is mapped to virtual and on to physical memory.
The entries of the region table have the following fields: size location of physical memory type: text, private data, shared memory or stack status: locked, used, loading into memory and/or valid pointer to the inode (the file) which originally uses the region count of processes which use the region
The malloc and free C functions and the new and delete C++ operators use the brk() system call to change the data region size.Shared memory system calls (shmget, shmat, shmdt, shmctl) enable communication by attaching to the same region of memory in the global region table. Attached memory is accessed via pointers, so communication speed is very high, and overheads low.
fork() creates an identical duplicate process
sproc() creates a lightweight process, which is nearly identical to its parent. The new child process shares the virtual address space of the parent process, rather than simply being a duplicate. The parent and the child each have their own program counter value and stack pointer, but all the text and data regions are visible to both processes. Such lightweight process have similar overheads to full processes.
exec() overlays a new process image on an old process. The new process image is constructed from an ordinary, executable file, so is often used to replace the child's text in a fork(). There can be no return from a successful exec() because the calling process image is overlaid by the new process image.
pthreads are POSIX threads API that enable portable parallel processing, see man pthread_create
3 differences between threads and processes:
1. A P has its own set of state information, eg its own effective user ID and set of open file descriptorsThreads exist within a process and do not have distinct copies of these state values. Threads share the single state belonging to their process
2. Normally, each P has a unique address space of memory segments which are accessible only to that process. (Ps created with sproc() share address space).
Threads within a process always share the single address space belonging to their process
3. Ps are scheduled by the kernel. A change of process requires 2 context switches
document.doc 4
stack 44K
data 32K
text 20K
B
A
stack 54K
data 28K
text 20K
Process memory
Region tablePregion tableProcess table
Process data structures
u areas
![Page 5: Context of Process](https://reader030.vdocuments.us/reader030/viewer/2022020520/577cc7601a28aba711a0bfa8/html5/thumbnails/5.jpg)
Threads are scheduled by code that operates largely in the user address space, without kernel assistance. Thread scheduling is faster than process scheduling.
Attribute POSIX Threads UNIX Processes
Source portability Standard interface, portable between vendors ISO/IEC 9945-1:1996
fork() is a UNIX standard
Creation overhead Relatively small Quite large
Block/Unblock (Dispatch) Overhead
Few microseconds Many microseconds
Address space Shared Separate
Memory-mapped files and arenas
Shared Explicit sharing only
Mutual exclusion objects Mutexes and condition variables; POSIX semaphores; message queues
semaphores and locks; POSIX semaphores; message queues
Files, pipes, and I/O streams Shared single-process file table Separate file table
Signal masks and signal handlers
Each thread has a mask but handlers are shared
Each process has a mask and its own handlers
Resource limits Single-process limits Limits apply to each process separately
Process ID One PID applies to all threads PID per process
Multi-threads share the context, but have their own stack
-------------------------------------------------------------------------------------------------------------------------------Tutorial Exercises 1. Explain what happens to the stacks of a process when the following happen: the process issues a system call a time slice while a process issues a system call, it is interrupted by a software, a device, a tty, a disk and a clock
interrupt
2. Explain the major differences between a Unix process and a POSIX thread
document.doc 5
One processOne thread
user stack
kernel stack
user addrs space
process control block
One processMany threads
user addrs space
process control block
thread contrl blockuser stack
kernel stack
thread contrl blockuser stack
kernel stack
thread contrl blockuser stack
kernel stack
Threads A unix process can be said to have 1 thread of execution
POSIX threads pthreadsare run within a process