Tutorial 2 – Computer Systems
Copyright By PowCoder代写 加微信 powcoder
Life Cycle
1 Process creation
2 Process termination
3 Process states
processes get
System initialisation
Processes are initalised
by system on boot-up
1 Process creation
Process creation
system call
When running process
needs another process
to do its job
User requests a new
process to be created
User requests new
process to be created
Initalisation of batch
A running process
invokes new processes
following a schedule
terminate?
Normal exit
Program done running
2 Process termination
Error exit
Error defined by
e.g. segfaults
Fatal error
OS terminates program
when error is not caught
Killed by another
Kill command
3 Process state
What states can
a process have?
When are interrupts
generated?
When a device to CPU needs
attention from CPU, it
generates an interrupt to get
Async with current operation:
processor saves current
environment and returns back
Interrupts
Services the interrupt
Saves status of current
Execute a return from the
Synchronous with current
operation: if the event occurs
at the same point every time
within the program with the
same data and memory
Interrupt handler deals
with the interrupt
Pseudo-interrupt is
generated from a
process within the CPU
What if a process has
multiple tasks to
perform independently
of each other?
A thread is a basic unit of cpu
utilisation. Threads have
shared local memory between
Less time taken to create
new thread
Less time to terminate
Less time to switch since
there is no call to kernel
Less time to communicate
between threads
Shared local memory
means that security and
integrity issues arise
Can’t predict anything
about timing of processes
Advantages Disadvantages
Thread management is not done by kernel
Fast to manage and create as no kernel privileges required, easy
to implement
When one thread performs blocking operation, entire process
blocked (dependent)
There is no preemption, a user level thread cannot randomly be
stopped allow another more high priority thread to run, for
In a user level thread…
User level vs.
kernel level
Kernel threads are directly supported by operating system
Slower to manage and create than user level threads
When one thread performs blocking operating, another can
keep running (independent)
In a kernel level thread…
Issues with
Interrupts can lead to race
conditions
A race condition is when the
output depends on the order in
which processes happen
You don’t know when an
interrupt is going to happen
You don’t know about other
processes that exist in the
Process 1 requests
resource B from process
Resource B is locked while
process 2 is running.
Process 2 requires
resource A from process 1
to finish running.
Resource A is locked while
process 1 is running.
When each thread depends on an
event by the other thread other
to complete its own task
Avoiding deadlocks
Ask questions like what does it mean to remove or
change a resource?
Where does the resource go?
What can the thread now depend on instead?
Draw a resource graph of programming logic to fix
interdependency
Requirements to avoid race
conditions
No two processes may be simultaneously inside their
critical regions
No assumptions may be made about speeds or the
number of CPUs
No process running outside its critical region may block
other processes
No process should have to wait forever to enter its
critical region
Avoiding race conditions
An example strict alternation with busy waiting
A lot of wasted CPU usage
Priority inversion problem: low priority processes may
Issues with busy waiting?
Scheduling
algorithms
First come first served
Processes scheduled in
order of which they are
Shortest process first
Processes scheduled in
order of time of
Shortest remaining
Processes with least
remaining time executed
Round Robin
Each process executed
for a certain amount of
time (a quantum)
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com