CSC 330 - Exam Two Flashcards
Describe race conditions
Race conditions: when two or more processes attempt to share the same resource simultaneously. The process that writes to the resource last will succeed.
Example:
• Process A reads variable X and stores it in variable Y
• Process B reads variable X and stores it in variable Z
• Process A updates Y and stores it back into X
• Process B updates Z and stores it back into X
• Process B wins
Describe critical sections
Critical sections: the code in a process that accesses and uses a shared resource. Every process that needs to access a shared resource has its own critical section. It is a technique to avoid race conditions. There are four conditions for a proper solution:
• Only one process can be executing its critical section at a time (mutual exclusion)
• No assumptions can be made about speed or number of CPUs for the entire system
• No process outside a critical section can block another process
• No process should have to wait indefinitely to enter its critical section
Describe semaphores
Semaphores are integer variables; they can be binary semaphores (which have a value of 0 or 1) or counting semaphores (which can have any integer value). They are manipulated with two atomic operations – Wait (P) and Signal (V).
- Wait – If the semaphore value is greater than 0, decrement it and continue. If the semaphore value is 0, wait until it becomes 1, decrement it and continue.
- Signal – Add 1 to the value of the semaphore.
Describe mutexes
Mutexes are simplified semaphores; they are equivalent to the binary semaphore. They can be locked or unlocked.
Describe monitors
Monitors are programming language constructs that implement mutual exclusion; they are an encapsulation of data and methods that manipulate the data.
Only one process can be executing a monitor method at any time. If a process is in the monitor and another calls one of the methods, the second process will be suspended until the first process completes. Processes inside the monitor must leave the monitor before doing anything that will cause a suspend.
Monitors are more reliable than semaphores because the compiler handles the mutual exclusion.
Name and fully explain the four necessary and sufficient conditions for deadlock
- Mutual Exclusion – Each resource is available or allocated to exactly one process
- Hold and wait – A process holding resources can request and wait for additional resources
- Circular wait – A circular chain of processes each holding a resource and waiting for a resource held by the next process in the chain
- No preemption – Resources allocated to a process cannot be forcibly taken away
Deadlock can occur if the four conditions are present simultaneously.
Name and fully explain the four ways to prevent deadlock
• Attack mutual exclusion – avoid assigning resources unless absolutely necessary; have few processes that are allowed to request a resource.
• Attack hold and wait – prevent a process from holding one or more resources while requesting other resources at the same time. Require processes to request all of the resources needed at the start. Processes must temporarily release all held resources before requesting new resources.
Problems:
¬ Processes may not know all of the resources needed at the start
¬ Resources will be tied up longer than necessary
• Attack circular wait – allow processes to only have one resource at a time. Globally order the resources and require that a process cannot make a request for a resource that is numbered less than the largest number of a held resource. Processes can request a lower numbered resource if they release higher numbered resources. Typically, there are a lot of resources that would need to be numbered.
• Attack no preemption – taking away some resources can be problematic but daemons can help. Some resources can’t be virtualized in this way.
Deadlock can be prevented if one of the four necessary and sufficient conditions for deadlock are prevented
Describe how deadlock can be avoided
Deadlock can be avoided by carefully deciding whether to grant access to an available resource. The idea is to only grant resources that will result in the system staying in a safe state.
- A state is safe if there is a scheduling order that the processes can be run to completion – if the scheduling condition cannot be met, the resource state is unsafe
- The bankers algorithm can be invoked by the OS to check if the current resource-allocation state is a safe state
Describe how deadlock can be detected
If there is one resource of each type, you can construct a resource graph, search the graph for cycles and the processes in a cycle are deadlocked
If there are multiple resources of each type, you can use the detection algorithm over the array structures. When the algorithm is done processing, the processes that are unmarked are deadlocked.
Deadlock detection involves maintaining information of the current resource allocation state and invoking an algorithm that uses the information of the resource allocation state to detect if deadlock has occurred.
Describe how deadlock can be recovered from
- Preemption – resource is taken away from a process and the process is suspended. When the process that gets the preempted resource completes, the resource is returned to the original process.
- Rollback – The operating system periodically creates a checkpoint for each process; many checkpoints are held per process. When a resource is needed to break a deadlock, one of the processes holding it is rolled back to before it acquired it.
- Killing processes – A process in the deadlock cycle (or not in the deadlock cycle but possessing needed resources) is killed and restarted. Processes killed should be ones that can be restarted without bad effects.
Describe what a file system does
Provides users the ability to put data into files and to find and use files that have been previously created. The file management system does not have any knowledge about how the data in a file is organized. The file management system will typically store files in nonvolatile memory.
Files stored will need to be accessed and found again at some later time. A user who wants a file can ask if a file with a certain name exists in a particular folder (directory).
Name and describe the file attributes
A file attribute is additional information an operating system keeps about a file
- Size – how much data is stored in a file
- Location – where the file is stored on the computer; which disk it is on
- Type – what kind of data is stored in the file – denoted by the file extension; contents, read-only flag, hidden flag, system flag, archive flag, temporary flag
- Permissions – who can access the file and what they are permitted to do with it
- Creator – who created the file
- Owner – who owns the file
- Time stamps – when the file was created; when it was last modified; when it was last accessed
Name and discuss the four ways of implementing files
- Contiguous allocation – files are allocated a contiguous series of blocks on the disk. The last block will typically have waste because files are rarely an even number of blocks long. This is easy to implement – only need to record the size and starting address. it can cause disk fragmentation. Files that increase in size have to be moved to a new and larger space.
- Linked list allocation – the first word of each block of the file contains the address of the next block of the file. The directory only stores the address of the first file block. Sequential access of the file is fine, but random access is slow. Program block reads may need data from multiple disk blocks.
- Linked list allocation with a table – a table in memory with the same number of entries as blocks on the disk. If the first block of a file is stared at location X on the disk, location X in the table stores the location of the second block of the disk. These are called file allocation tables (FAT). Random access is faster because the chain is in memory - not on disk. For large disks, the table is very large.
- I-nodes – an I-node contains file attributes and a list of block addresses (pointers to file blocks). The I-node is a fixed size so the last block address points to a block with more pointers to file blocks.
Discuss issues in disk space management
Block size –
•Too small and most files will span multiple blocks.
•Too large and a lot of space will be wasted in the last block of a file.
Free blocks –
•A list of available blocks is kept. While this list can be large, that also means there are a lot of free blocks to hold it.
•Bitmap is kept. One bit per block with one representing a free block.
Disk quotas –
• Limit on how much data a user can store
• Quota table keeps track of hard and soft limits and current usage
Caching –
• Need to maintain file integrity – blocks modified in cache need to be written out to disk.
• Blocks read ahead
Fragmented disks –
• Free space becomes distributed around the disk so file blocks get spread out which reduces performance
• Defragmentation programs help to condense the free space and the blocks of a file
Discuss the two causes for disk I/O delays
- Seek time – The time it takes to move the read/write head to the correct track
- Rotational latency – The time it takes for the disk to rotate the correct sector under the read/write head