Topic 11 – Context switching Flashcards
in regards to the context block:
this is a record of what state the process is in such as running, ready or blocked, it is used to decide how the process should be treated.
in regards to the context block what is the:
process state
in 7 steps describe how
buffer control is possible by using a semaphore
the steps that describe this are:
- a buffer is empty and has a size of 10 (the semaphore value is 10)
- a process wishes to deposit an item so makes a SEM_WAIT system call
- since the semaphore is not 0 the process can deposit the item in the buffer
- the semaphore value is now decremented to 9
- steps 2 - 5 will reapeat untill the semaphore reaches 0
- a process wishes to deposit an item so makes a SEM_WAIT system call
- the semaphor count is 0 and so the process is put into a waiting state until another process removes an item from the buffer and makes the system call SEM_SIGNAL so that the semaphore can be incremented by 1
note
this example does not take into account an event such as two threads colliding at a buffer address (race condition) for this to be mitigated a seperate semaphore would be needed for mutual exclusion (mutex)
the main feature of the processor that the operating system takes advantage of to achieve this is:
the fact that a hardware interrupt will pause the execution of any process then switch to kernel mode and hand control of the CPU to the operating system
the hardware interrupt in question is received from a hardware timer
what is the main feature of the processor that the operating system takes advantage of to achieve
pre-emptive multitasking
describe in 5 steps the
stack pointer
during an interrupt without a context switch
this without a context switch
- an interrupt occurs
- process A instruction pointer is placed on top the stack pointer
- the routine is placed inside the instruction pointer and is executed
- when the routine is finished it will call the RETURN instruction
- process A instruction pointer is pulled of the stack pointer and execution commences where it was paused
in regards to the context block:
this records information such as a processes running time (quantum) and other information that can be used by the scheduler so that it can decide the order of execution
in regards to the context block what is the:
process priority
what are the two actions that can occur that would
cause a process to be transitioned and control of the CPU handed back to the operating system
these actions include:
- Transition because of a system call: for example when an application program makes an I/O request or goes to sleep
- Transition because of an interrupt: for example when the hardware timer interrupts
For either of these the process will change state and the OS will handle the events
two use cases of this are:
- mutual exclusion (mutex) - this can be used by a thread to tell other threads that it is currently executing a critical region of code and that the resource must not be acceessed this avoids scenerios such as a race condition
- buffer control - this can also be used as a counter for example how many units are available in a buffer
name 2 uses of the
semaphore
name 6 pieces of information that would be found in the
context block
for a process
this would record details for a process such as:
- Process Identifier (PID)
- the process state
- the process priority
- saved register contents
- information about the processes memory space
- information about files
while the processor is in kernel mode what 2 items does the operating system have access to
while the processor is in this mode the opearting system can access:
- all instructions including privileged instructions
- all data structures including the context block
in summary these problems occure when:
- There is a shared resource
- There are critical regions of code acting on those shared resources
in two summary points when do Concurrency problems such as the race condition occur
this system call essentially asks the OS if it can enter a critical region of code. If the semaphore count is 0 then the process is placed into a waiting state. If the semaphore count is not 0 then the semaphore count is set to 0 and the process enters a critical region of code
describe the semaphore system call
SEM_WAIT
with this type of state the operating system knows that the process is currently using the CPU
if a
process is in a running state
what does the operating system know about the process
these actions include:
- Transition because of a system call: for example when an application program makes an I/O request or goes to sleep
- Transition because of an interrupt: for example when the hardware timer interrupts
For either of these the process will change state and the OS will handle the events
what are the two actions that can occur that would
cause a process to be transitioned and control of the CPU handed back to the operating system
this would record details for a process such as:
- Process Identifier (PID)
- the process state
- the process priority
- saved register contents
- information about the processes memory space
- information about files
name 6 pieces of information that would be found in the
context block
for a process
if a process is in this state then the operating system knows that it is waiting on an I/O operation to complete
if a
process is in a blocked state
what does the operating system know about the process
this can only occur after a system call has been made or a hardware interrupt signal has been received.
in either case the processor will switch to kernel mode and the OS will gain control of the CPU, allowing it a chance to check the quantum of any running processes and run its scheduler if any quantum has expired
when can context switching occur
describe the semaphore system call
SEM_SIGNAL
this system call essentially tells the operating system that it is finished executing its critical region of code and the semaphore count can be incremented so that another process may enter its critical region of code.
this is a place in the source code that acts on a shared resource. If these actions are not carried out correctly and in full then the value of the shared resource could become false
what is a
critical region
of code
this is a solution to multitasking
within this solution the operating system has control over when it will use the CPU.
it does this by setting a hardware timer that will interrupt any process
describe what
pre-emptive multitasking
is
this is the term given to a semaphore when it is used for the purpose of mutual exclusion
note
semaphores have many use cases within the operating system
what is a
mutex
in regards to the context block what is the:
process priority
in regards to the context block:
this records information such as a processes running time (quantum) and other information that can be used by the scheduler so that it can decide the order of execution
this is the act of the currently running process being replaced by another process
note
since all data within the CPU registers will be replaced it is said that the CPU is now executing in a different “context”
what is
context switching
if a
process is in a ready state
what does the operating system know about the process
if a process is in this state then the operating system knows that it is waiting to be rescheduled to use the CPU
execpt for the semaphore system calls SEM_WAIT and SEM_SIGNAL
what other system calls are there that are associatied with the semaphore
There are also system calls that allow the creation and destroying of different semaphores so that different semaphores can be used by different applications
this is a diagram that expresses the transition and states of a process.
- ovals show the state of a process
- arrows show the direction and transition of a process from one state to another
notes
- All states and transitions should be labelled with there appropriate state or transition.
- These are important for design as well as understanding
describe a
state diagram and its components
in regards to the context block what is the:
Information about the process’s memory space
in regards to the context block:
this is information about what memory the process has allocated to it.
information recorded would include the address range of the memory
the steps include:
- Process A running
- Process A makes SEM_WAIT system call
- Semaphore count is 0
- Process A placed in waiting state
- Process B makes SEM_SIGNAL system call
- Semaphore count is incremented by 1
- Process A state changed from waiting to ready
- Semaphore count set to 0
- Process A is rescheduled and runs its critical code
- Process A finishes and makes SEM_SIGNAL system call
- Semaphore count is incremented by 1
If step 3 had a count of 1 then execution would jump to step 8
describe in 11 steps what happens when:
- process B is running critical code on a resource
- process A tries to access the same resource
and a mutex is in use by both processes
this works in the following steps:
- the opearating system has control of the CPU
- it will set a hardware timer
- a new process is scheduled to use the CPU
- the hardware timer sends a hardware interrupt signal to the processor
- the processor switches to kernel mode and the interrupt service routine for the timer is executed on the CPU
- since the interrupt service routine for the timer is part of the operating system it can call on the scheduler if any process in a running state has quantum that has expired
describe how
pre-emptive multitasking
works in 6 steps
two purposes of this are:
- for use by the scheduler - so that it can determine which process should be executed next
- for use during a context switch - so that a state of a process can be restored or saved (such as the state of registers or the memory a process has allocated to it)
name 2 purposes for the
context block
name 1 benefit and disadvantage of
pre-emptive multitasking
a benefit of this is:
If code is buggy or malicious then the operating system will always be able to regain control of the CPU
a disadvantage of this is:
that a process could be interrupted at a critical point in its execution and data could be left inconsistent. this is especially true of concurrent processes that are sharing data (race condition)
describe in 8 steps the
stack pointer
during an interrupt with a context switch
this with a context switch
- an interrupt occurs
- process A has its instruction pointer placed on top the stack pointer
- the routine is placed inside the instruction pointer and executed
- the routine detects that process A has expired quantum and the OS scheduler is called
- the operating system must make a context switch from process A to process B
- the stack pointer register has its contents changed, now the stack pointer holds process B stack pointer
- the routine finishes execution and calls the RETURN instruction
- process B instruction pointer is pulled of the stack pointer and into the instruction pointer and execution commences where it was paused
this is a phenomenon that occurs when two or more concurrent threads are sharing a resource
its a problem that lies where they both are carrying out tasks on that resource but neither has finished its task fully
the outcome is that depending on the order of who fnishes arrying out the task on that resource determines the final value of that resource
the final value of the resource will most likely be incorrect
describe what the
race condition
is
if a process is in this state then the operating system knows that it is waiting to be rescheduled to use the CPU
if a
process is in a ready state
what does the operating system know about the process
outline the state of a process and any transitions for the following sceneraio
a process makes an I/O system call where it will be blocked and then rescheduled
for this scenerio the steps are as follows:
- the process A is in a running state
- the process A makes an I/O system call to the operating system
- processor mode switches to kernel mode
- the operating system foresees the I/O operation taking some time
- the process A is placed in a blocked state
- a context switch occurs and another process is given control of the processor
- processor mode switches to user mode
- a hardware interrupt is received by the processor from the I/O device telling the processor that it is ready
- processor mode switches to kernel mode
- context switch occurs and the interrupt service routine for the I/O device is executed
- the process A is unblocked and placed into a ready state until it is rescheduled
what is a
semaphore
this is a service that is built into the operating system.
this enables the control and access of a shared resource so that threads sharing such a resource can avoid errors such as a race condition
what is
context switching
this is the act of the currently running process being replaced by another process
note
since all data within the CPU registers will be replaced it is said that the CPU is now executing in a different “context”