Lesson 4: Process and Process management
- process: Instance of an executing program.
Visual metaphor
Toy shop visual metaphor: A process is like an order of toys:
- state of execution
- completed toys, waiting to be built
- program counter, stack
- completed toys, waiting to be built
- parts and temporary holding area
- plastic pieces, containers
- data, register state occupies state in memory
- plastic pieces, containers
- may require special hardware
- glue guns
- sewing machine
- I/O devices like discs or network devices.
What is a process
- process represents the execution state of an active application. (may be running, waiting on input like user input to type in certain notes, waiting for another process that's currently running on the CPU.)
OS manages hardware on behalf of applications;
applications == program on disk, flash memory...(static entity)
process == State of a program when executing loaded in memory ( active entity)
What does a process look like?
Type of state:
- text and data
- static state when process first loads
- heap
- dynamically created during execution
stack
- grows and shrinks ~ LIFO queue.
address space == "in memory" representation of a process.
Page tables == mapping of virtual to physical addresses
Address space and Memory management
- Parts of virtual address space may not be allocated
- may not be enough physical memory for all state
For each process, the OS must maintain some info regarding the process address space (page table for instance), then use this info to maintain mappings between the virtual address and physical location. Also to check the validity of accesses of memory to make sure a process is actually allowed to perform a memory access.
Quiz
- Virtual address get mapped to physical memory address by a page
- Two identical virtual addr can refer to diff locations in physical memory
Process Execution state
How does the OS know what a process is doing?
- program counter
- CPU registers
- stack pointer
PCB (Process control block)
- PCB created when Process is created
- certain filed are updated when process state changes (mem limits, etc)
- other fields change too frequently
How is a PCB used?
Context Switch
What is a Context Switch?
- Context Switch == switching the CPU from the context of one process to the context of another.
- They are expensive!
- Directly costs: number of cycles for load and store instructions
- Indirectly cost: COLD cache! cache misses! (P1's cache be replaced to make room for P2, the next time P1 is scheduled to execute, its data is not in the cache! COLD!)
- Therefore, we want to limit how frequently context switching is done!
- Accessing data from cache --> much faster (order of cycles) than access memory (hundreds of cycles)!
Hot Cache: when the data we need is present in the cache
Cold Cache: when the data we need is not present in the cache
Quiz 2
Process Lifecycle
Process can be running or idle and many other states:
- if context switch is performed (interrupt) -> move back to ready state
- may need to initiate some longer operation, (reading data from disk or wait on some event like a timer or input from keyboard) -> waiting state->when completes -> ready
- when finish all --> ternimated!
Quiz 3
Process creation
Mechanisms for process creation:
fork ==
- copies the parent PCB into new child PCB
- child continues execution at instruction after fork
exec ==
- replace child image
- load new program and (child's PCB will point to values or describe values that describe this new program.) start from first instruction. (in particular, the program counter for the child will now point to the first instruction of the new program)
what we usually do to actually creating a new program is like we call a folk --> creates initial process, and then we call an exec, which replaces the child's image (the image that was created in the fork) with the image of this new program.
Quiz 4
- the parent of all process in UNIX-based OS: init
- the parent of all App process in Android OS: ZYGOTE
Role of the CPU Scheduler
A CPU scheduler determines which one of the currently ready processes will be dispatched to the CPU to start running, and how long it should run for.
OS must: BE EFFICIENT!!
preempt (抢占)== interrupt and save current context;
schedule == run scheduler to choose next process
dispatch == dispatch process and switch into its context
Length of Process
How long should a process run for? $$Tp = 10*T{sched}$$ that 91% of CPU spent on useful work!
if $$Tp = T{sched}$$, only 50% of CPU time spend on useful work
How frequently should we run the scheduler?
Timeslice == time Tp allocated to a process on the CPU
Scheduling design decisions:
- what are appropriate timeslice values?
- metrics to choose next process to run.
How I/O affect schedule?
Quiz
Inter Process Communication (IPC)
IPC mechanisms:
- transfer data/info between address spaces
- maintain protection and isolation
- provide flexibility and performance
- Message-passing IPC:
- OS provides communication channel, like shared buffer
- Processes
- write(send)/ read(recv) message to/from that shared communication channel
- Benefit of Message-passing IPC: OS manages the channel, OS provides APIs, the exact same system calls for writing/sending data or reading/recv data
- Downside: the Overheads! (copy from P1 to channel and to P2)
Shared Memory IPC:
- OS established a shared channel and maps it into process address space
- Process directly read/write from this memory
- OS is completely out of the way! (main advantage! also cause the downside)
- Downside: OS no longer supports fixed and well defined APIs how this particular shared memory region is used. --> it's usage sometimes becomes more error prone, may need to re-implement code to use this shared memory
Quiz
Shared memory - based communication VS Message passing communication:
SM: individual data exchange may be cheap, (don't require the data is copied in and out of the kernel); However the actual operation of mapping memory between two processes is expensive!