Professional Documents
Culture Documents
Assignment 2 W Sol
Assignment 2 W Sol
Assignment 2 W Sol
I/O Request
Time allotted for the process to be executed is up
o Even if you have mutex lock
Spawning child process
Interrupt
1. Scheduler schedule
2. Dispatcher for context switching (unload or reload processes needed, appropriate program
counter, context switching)
Kernel threads are loaded to the cpu scheduler (they represent the processes)
1. CPU BURST cpu intensive cycle, just a few millisecond (very short time)
2. IO BURST io burst cycle, have longer burst compared to cpu burst (even in calculations,
there are io burst, move and store to registers are considered io)
TYPES OF SCHEDULER
1. Cooperative/Nonpreemptive
Disadvantage: may cause starvation if one process has a long execution time which will hoard
the cpu and its resources
2. Preemptive
Tasks are forcefully ejected in the CPU
ASSUMPTION
Multiple tasks
Only 1 CPU
FCFS
SJF
SJF+PREEMPTION
PRIORITY SCHEDULING
Best scheduler (because you can mix and match)but is almost impossible to be implemented
due to the many variables to be considered
MULTIPROCESSOR SCHEDULING
O(1) SCHEDULER
Feedback system
longer idle boost prio by -5
lower sleeptime demote prio by +5
PROBLEM: locks and what if the process is just idle and does nothing and you keep
promoting its priority?
LECTURE 8
MUTEX COMPLICATIONS
HOW TO PREVENT OTHER PROCESSES TO INTERRUPT ANOTHER PROCESS EXECUTING IN ITS CRITICAL
CONDITION?
Limitation:
Impl 1 hassle if 3 or more threads wants to use, complexity in while loop and array of flag
Impl 2 most of CPU time is used in waiting, locking mechanism doesn’t work cause multiple
threads wants to acquire the lock at the same time and is exposed to interrupt
Solution: must be implemented as 1 instruction/line of code
4. Atomic Instruction
They are not interruptible during their execution
Imp 1: test and set
You try to lock the value and then check the old state if it is already lock and if it is then
the code will not proceed (will be stuck at the while loop)
Problem: you keep checking the state of lock, possible that threads may starve(no
fairness mechanism in acquiring lock)
Useful only if the critical section is short
You take actual then compare to desired, if it is the same set the lock to the actual value
It’s not as wasteful as test and set but is till prone to busy waiting and still does not have
a concept of queuing
Fetches the value of lock then adds 1 to it but returns the old value
V2 ticketing system analogy, solves the problem that the process may not get a
chance to acquire the lock but busy waiting is still present
If lock is still present and the process doesn’t urgently needs the lock may go back to
ready queue which solves the busy waiting but the problem is context switching, this is
only appropriate for small number of threads.
Soln: have an implementation where the process can sleep (which will not make it go
back to ready queue and will not also waste cycle in busy waiting) and just invoke a
wake mechanism if the process is needed
flaw is that you can fall asleep while you have the lock (deadlock)
soln: setsleep(); checks if it’s okay to sleep first before going to sleep
TIP: don’t let thread fall asleep if you are in critical section
PRODUCER- CONSUMER
Producer produces into a shared data, consumer eats the data from the shared buffer
READER-WRITER
Shared resource, readers look at the contents, writer edits the contents of the resource. Readers
can’t read when writers write
SEMAPORE
wait(s) / sem_wait()
Check if zero or nonzero, if nonzero decrement counter, if zero make the process
sleep/wait
signal(s,TID)
if there are threads waiting, let one thread go, if there are none increment counter
Volatile prevents compiler optimization, code will not be reordered during compilation