COMP 3511: Lecture 6
Date: 2024-09-19 14:59:46
Reviewed:
Topic / Chapter:
summary
βQuestions
Notes
Process Concept (cont.)
-
Process state
- traditional Unix: each process = 1 thread
- process state current activity of that process
- as process executes: it changes states
- new: process being created / allocating resource
- ready: process waiting to be assigned to CPU
- π¨βπ« remember ready queue and CPU core!
- running: instructions being fetched & executed
- waiting: process waiting for event to occur
- terminated: process finished execution / deallocating resource
- β how do process manager work? how much CPU resource does it take?
- π¨βπ« will be discussed later (whole chapter)
- different things might happen in running state
exit
: finished all tasksinterrupt
: after each line of instruction: checks interrupt signal- if detected: add interrupt routine to process queue
I/O || event
: I/O service, etc: are much slower than CPU- so the process gives up CPU itself, and waits for signal (from IO / etc.)
- traditional Unix: each process = 1 thread
-
Process control block (PCB)
- each process: corresponds to 1 PCB
- i.e. info. associated w/ each process
- aka: task control block
- includes following info:
process state
: running / waiting, etc.program counter
: location of instruction to be fetched nextCPU registers
: accumulators, index resisters, stack pointers, general-purpose registers, condition-code informationCPU scheduling info.
: priorities, pointers to scheduling queue, scheduling parametersmemory-management info.
: memory allocated to the process- complicated data structure: paging & segmentation tables
- π¨βπ« for now, consider it as a pointer to program data structure
- π¨βπ« program: simple binary; process: dynamic!
accounting info.
: CPU used, clock time elapsed, time limits- simple statistics
IO status info.
: IO devices allocated to process- e.g. list of open files
- each process: corresponds to 1 PCB
-
Threads
- we assumed: a process w/ single thread of execution
- thread: represented by: PC, registers, execution flags, stack
- modern OS: allow a process w/ multiple threads
- thus can perform parallel execution
- e.g. browsers: contacting web server vs. displaying image, etc.
- w/ multi-core systems: multiple threads of process: can run in parallel
- process: can consist of 1 / multiple threads
- running within the same address space (e.g. sharing code & data)
- each thread: w/ its own, unique state
- multi-threads: process providing concurrency & active components
- we assumed: a process w/ single thread of execution
-
Process representation in Linux
- Linux: storing list of processes in circular doubly LL
- aka task list
- each element in task list: of type struct
task_struct
- i.e. PCB
pid t_pid; long state; unsigned int time_slice; struct task_struct *parent /* TBD */ struct list_head children /* TBD */ struct files_struct *files /* TBD */ struct task_struct *parent /* TBD */
- Linux: storing list of processes in circular doubly LL
Process Scheduling
-
Process schedule
- primary object of multiprogramming: keep CPU busy
- as it's a scars resource
- process scheduler: OS mechanism selecting a process
- from available processes inside main memory
- scheduling criteria: CPU utilization, job response time, fairness, real-time guarantee, latency optimization...
- degree of multiprogramming: no. of processes currently residing in memory
- determines resource consumption
- processes: roughly either:
- I/O bound process: spends more time w/ IO than computations
- many short CPU bursts
- CPU bound process: generates IO requests infrequently
- more time doing computations; few, very long CPU bursts
- I/O bound process: spends more time w/ IO than computations
- primary object of multiprogramming: keep CPU busy
-
Scheduling queue
- OS: maintains different scheduling queues of processes
- ready queue: set of processes in main memory;
- ready & waiting to execute
- wait queue: set of processes waiting for an event
- ready queue: set of processes in main memory;
- processes: migrate among various queues during lifetime
- OS: maintains different scheduling queues of processes
-
Representation of process scheduling
-
Schedulers
- long-term scheduler (job scheduler)
- for scientific & heavy computation
- selects which processes to be brought to memory
- for mainframe minicomputers (not PC)
- made of job queue: hosts job submitted to mainframe computers
- short-term scheduler (CPU scheduler)
- selects a process from a ready queue to be executed next & allocate CPU for it
- short-term scheduler: invoked frequently (ΞΌs, must be fast)
- long-term scheduler: invoked infrequently (s,min, may be slow)
- long term scheduler: dictates degree of multiprogramming
- mainframe computer system:
- long-term scheduler (job scheduler)
-
Medium term scheduling
- if you underestimated the program resource:
- swap out (not terminating) some programs
- entire process information (not just PCB)
- and use the freed up resource to accelerate & give more space
- swap out (not terminating) some programs
- for PC: you can simply kill the resources
- for mainstream: it's all services that's paid
- mostly not implemented for PC
- if you underestimated the program resource:
-
Inter-process CPU switch
- context switch: when CPU switches from one process to another
- sample OS task
- save state into PCB-0
- reload state from PCB-1
- save state into PCB-1
- reload from PCB-0
- ...
- sample OS task
- context: must be saved (in PCB / in each thread of process)
- typically: including registers, PC, stack pointer
- context-switch time: an overhead
- system: doesn't do useful work during it
- takes longer if OS & PCBs are complex
- switching speed: depends on
- memory speed
- no. of registers copied
- existence of a special instructions to load & store all registers
- speed: highly dependent on underlying hardware
- e.g. some processors: provide multiple sets of registers
- context switch: simply changing the pointer to a different register set
- context switch: when CPU switches from one process to another
Dual-mode Operation
-
Dual mode operation
- allows OS to protect itself & other system components
- π¨βπ« MS-DOS didn't have it; Windows have
- user mode: has less access / permission to hardware than the kernel mode
- can be easily extended to multiple modes!
- β sudo = executing as the lowermost user?
- dual-mode operation provides: basic means of OS protection & users
- from errant / bad users
- all access: must be strictly controlled by well-defined OS API
- switching between modes: constant
- e.g. (user) API call to open -> (kernel) open -> (user) displays
-
Three types of mode transfer
- 3 common ways to access kernel mode from user mode
- system call:
- interrupt:
- trap / exception:
Operations on Processes
-
Operations on processes
- processes in most systems: execute concurrently;
- created & deleted dynamically; thus OS must support
- process creation: running a new program
- process termination: program execute completes / terminate
- parent process: can create children processes
- children: can create other child processes
- tree of processes!
- π¨βπ« shell: also a process!
- waiting for your input
- all commands running on it: children process of shell
desktop
: can be a parent process- process started on it (e.g. by double click): its children process
- the grandpa:
root
process
-
Tree of processes on Linux system
-
Process creation
- most OS: identify process according to unique process identifier
- or:
pid
, usually an integer
- or:
- child process: needs certain resources
- CPU time, memory, files, IO devices to accomplish task
- parent process: also pass along initialization dat (input)
- to child process
- parent & children: share "almost" all resources
- three choices possible on how to share the resource
- parent & children: share almost all resources:
fork()
- children: share only subset of parent's resources:
clone()
- parent & children: share no resources
- parent & children: share almost all resources:
- two options for execution after process creation
- parent: continues to execute concurrently with its children
- parent: wait until some / all its children to terminate
- again, two choices of address space for a new child process
- child process: duplicates of parent (same program & data) - Unix/Linux
- child process: has a new program loaded into it (different program & data) - Windows
- UNIX examples
fork()
: creates a new process; duplicates entire address space of the parent- both processes: continue execution at the next instruction after
fork()
- both processes: continue execution at the next instruction after
exec()
: maybe used afterfork()
: to replace child process's address space w/ a new program- loading a new program
- parent: can call
wait()
to wait for a child to finish
- most OS: identify process according to unique process identifier