All Lectures Flashcards
What are the goals of an OS?
- Allow user to use applications
- Allow applications to use computer resources
- Allow users and applications to interact
What are the three key abstractions of an OS?
Process
-Instance of a program
Memory (Address) Space
-The range of memory locations that a process can access
File
-A stream of data
How does each layer in the system implement its set of services?
It implements its services by using the layer below it and providing its services to the layer above it.
Identify and describe the two modes of the CPU
Supervisor (Kernel) mode:
- Access to all hardware, I/O and memory
- Can execute ALL CPU instructions
User mode:
- Program running on CPU is restricted from accessing all devices or memory
- Can only execute a restricted set of CPU instructions
What is the kernel?
O/S that is loaded into memory when the computer starts up.
Resides in memory at all times
Runs in supervisor mode
What are the roles that the kernel plays?
- protects the hardware and processes
- manages hardware and resources
- provides an execution environment for the processes
- creates and runs processes
- services process requests
Define process
A process is a running instance of a program in memory.
How does the kernel get control back from a process?
Through system calls and interrupts
What happens when there is an interrupt?
CPU switches to kernel mode. CPU uses an interrupt id to index into the interrupt table. Invokes interrupt handler, switches back to user mode, returns to where the process was before the interrupt.
What is the purpose of interrupts?
- Help the kernel gain control of the CPU
- Notify the kernel of I/O
- Help processes make requests to the kernel in a controlled way
- Notify the kernal of errors
How does the kernel get control of the CPU if no interrupts occur?
The timer.
What is the importance of the timer?
The timer allows the kernel to gain control of the CPU on a regular basis. No process can monopolize the CPU because kernel can switch to another process.
How do processes make requests to the kernel?
Using a trap (software interrupt).
What is context switching?
When an interrupt handler runs the current process has to be context switched out and the kernel context has to be context switched in
How does context switching work?
The handler copies all registers from CPU to memory, initializes all registers to what the kernel expects, switches to the kernel stack, runs the handler
To switch back:
Switches to the processes stack, copies registers from memory to the CPU, returns from where the process left off
What is a process composed of?
CPU context
•Memory space
•Thread of executtion
Describe the CPU context of a process
General registers,
Program counter
Special purpose registers
Describe the memory space of a process
Stack: local variables and return addresses
Heap: dynamically allocated memory
Data: global and static variables
Code: program code
Describe the 5 process states.
New: process created Ready: ready to run Running: running on the CPU Blocked: waiting for a service Terminated: killed and needs to be cleaned up
Describe the various process transitions
New->ready: admitted to scheduler
Ready->running: dispatched by the scheduler
Running->ready: interrupt due to timer or I/O
Running->blocked: service request (system call)
Blocked->ready service request completion
Running-> terminated: exit or kill
How is a new process created?
Process creation is initiated by a process and performed by the kernel.
Parent process makes a fork() call and then the kernel
-makes a copy of the parent
-allocates a new PCB and PID
-copies parent’s PCB to new PCB
-sets new PID
-Marks new process as ready
-Sets return value of parent process to child PID
-Sets return value of child process to 0
What is the ready queue?
An abstract queue which stores pointers to PCBs in the order that the processes are scheduled to run
What is a scheduler?
The scheduler is responsible for deciding which process to run next. The scheduler is called by the OS whenever a process related event occurs
What is the goal of the scheduler?
Maximize CPU utilization and Throughput. Minimize turnaround time, waiting time, and response time
What is non-preemptive scheduling vs preemptive scheduling?
Non-preemptive scheduling does not use a timer, and preemptive scheduling uses a timer.
What is meant by CPU Bound vs I/O Bound
A process is CPU bound if it predominantly does computation and little I/O blocking and lots of running.
A process is I/O Bound if it predominantly does I/O and little computation and lots of blocking and little running.
Describe a FIFO scheduler and its characteristics
First come first serve
- simple implementation
- non preemptive
- indiscriminate between CPU and I/O bound processes
- susceptible to the convoy effect
Describe a SJF scheduler and its characteristics
Shortest job first: priority of job is based on its CPU burst length
- Probably optimal
- nonpreemptive
- hard to implement
What is an exponential moving average and why would we need to know that>
An exponential moving average is a weighted average so that the most recent term has more weight than all previous terms:
Sum all (1-a)^i * atn tn is the nth term, a is 1/2
We can use an exponential moving average to predict the CPU burst time.
What is priority based scheduling?
Single priority ready queue of all processes. Priority of job assigned by user. Process gets to run until it requests a kernel service.
- nonpreemptive
- predictable
What are the problems with non-preemptive schedulers?
Starvation: low priority or long processes may never get to run
What is RR scheduling?
Round Robin scheduling. FIFO queue where all processes run until there is an interrupt, the process ends, or they use up their quantum of time (timer goes off)
- simple implementation
- indiscriminate between CPU and I/O bound processes
- high waiting time,
- convoy effect
Describe MLFB scheduler and its characteristics
System adjusts priority based on CPU burst. All processes start out in the highest priority queue, if they use up their quantum they get kicked to the next queue down. The lowest queue is scheduled using RR.
- all processes get to run
- I/O bound processes get priority
- used in many systems today
What is parallel execution vs concurrent execution?
Parallel execution is when 2 or more operations are performed simultaneously.
Concurrent execution is when 2 or more operations appear to be performed simultaneously
What is the definition and benefits of cooperating processes?
Processes are cooperating if they can affect of be affected by other processes.
Benefits include:
- information sharing
- modularity
- computation
- security
What are the requirements for process cooperation?
IPC
Process synchronization
Describe the composition of a thread
A thread comprises
- a single thread of execution
- cpu context
- stack
- shares remaining memory with all other threads
Why use threads?
Scalability: easier to use multiprocessor systems
Sharing; easier to share resources and data in concurrent setting
Economy: easier and cheaper to create a thread
Responsiveness: easier to create more responsive applications
How do threads access resources?
Resources are allocated to the process.
All threads belonging to the same process can access the resource.
How is a thread created?
A stack is allocated from the heap of an existing process (cheap). A TCB is initialized.
What is a critical section?
The piece of code in which race conditions can occur.
What is destructive interference?
Concurrent operations resulting in incorrect program behaviour
What is a race condition?
situations that can result in destructive interference
What are our solution goals to the critical section problem (4 goals)
Mutual exclusion
Scheduler independent
Allows progress
Starvation free
What is mutual exclusion?
No two threads/processes may be simultaneously in a critical section
What is scheduler independent?
No scheduling assumptions may be made about threads/processes
What is allows progress
No thread/process outside a critical section may block other processes
what is starvation free
No process should have to wait forever
What is a lock?
A lock is a mutual exclusion mechanism to protect critical sections that prevent other threads from entering if another thread is present. Allows threads to wait and eventually enter.
What is a spin lock?
A spin lock is a lock mechanism that requires the thread to spin in a loop testing a condition of some sort.
What are the advantages and disadvantages of letting the OS/System be responsible for entering/leaving critical sections?
Advantages:
- no race conditions in critical sections
- programmers don’t have to debug critical sections
- code becomes simpler
Disadvantages:
- code becomes system dependent
- routines are more expensive because of more system calls
What’s wrong with locks?
Many implementations do not allow a thread to acquire its own lock. Many implementations require that the thread that owns the lock release it.
What is a semaphore?
An integer variable instead of a boolean lock variable.
Represents number of pending wakeups and sleeping processes.
Two atomic operations: acquire, release.
What makes monitors unique from other abstractions?
Other abstractions such as semaphores or locks are language independent.
All you need to do to acquire/release a lock or semaphore is to call a function.
Monitors abstract locks and semaphores from the user.
What is a condition variable?
A condition variable is declared inside a monitor and represents queues of processes/threads waiting for a specific condition to occur.
Supports two operations: wait and signal
How does a condition variable work?
When a process needs to wait for something to occur it performs a wait on the condition variable and gets added to the condition variable’s queue. Then it leaves the monitor and is put to sleep.
Once the condition is met the process meeting the condition performs a signal on the variable which wakes the process at the head of the queue. Then the current process leaves the monitor allowing the next woken process to enter.
What are some conditions for deadlock?
Mutual exclusion:
Processes are hogging the resources
Hold and Wait:
A process will attempt to acquire all resources it needs and hold them and wait for all other resources to become available
No Preemption:
Processes can not be forced to release a resource
Circular Wait:
There is a set of processes and resources such as there is a cycle in the resource allocation graph that can not be broken.
What are the four ways to handle deadlock?
Do nothing.
Deadlock prevention.
Deadlock Avoidance.
Deadlock Detection and Recovery.
What are some options we have for preventing hold and wait?
- Allow each process to hold only one resource at a time. Requires a lot of excess copying.
- Processes must be allocated all their resources at the start of execution. Requires programmers to know up front all resources that may be required
- Processes may not own any resources when requesting resources. Processes may starve due to the combination of resources not being made available
How can we prevent preemption?
If a process is holding resources and requests another resource that is not available, then all of its resources are released and added to the request list. Some resources cannot be preempted. Starvation could occur.
Preempt resources only if they are requested by another process… but then run into the same problems as in option 1
How can we prevent circular wait?
Order resources and require that they be acquired in ascending order. Easy to implement but requires that developers write their code to follow the order.
What is a safe state?
A system is in a safe state if the sequence of resource requests by executing processes will not lead to deadlock (no cycles in the resource graph).