CSC264 ยท 2081 ยท 5 marks
Write short notes on: a. Virtual Memory b. Race Condition
Write short notes on: a. Virtual Memory b. Race Condition [5]
Quick answer
Virtual Memory is a memory management technique in which the operating system uses a portion of the hard disk as an extension of RAM, thereby increasing the effective size of usable memory beyond the physical RAM available.
Short Notes: Virtual Memory and Race Condition
a. Virtual Memory (2.5 marks)
Key Concepts:
- When the kernel needs memory for a new process but RAM is full, it writes the contents of a currently unused block of memory to the hard disk. This process is called swapping out.
- When the original contents are needed again, they are read back into memory (swapped in).
- The portion of the hard disk used for this purpose is called swap space.
Working Mechanism:
- A segment number is used to find the segment descriptor.
- A check is made whether the segment's page table is in memory; if not, a segment fault occurs.
- The page table entry for the requested virtual page is examined; if the page is not in memory, a page fault is triggered.
- If the page is in memory, the main memory address is extracted from the page table entry.
- The offset is added to the page origin to give the final main memory address, and the read/write takes place.
Advantages and Disadvantages:
| Advantage | Disadvantage |
|---|---|
| Programs larger than physical RAM can run | Disk access is slower than RAM, reducing speed |
| More processes can be accommodated | Excessive swapping (thrashing) degrades performance |
In Linux, swap space can be a swap partition (faster) or a swap file within the normal file system.
b. Race Condition (2.5 marks)
A Race Condition is a situation that occurs when two or more processes (or threads) read or write some shared data concurrently, and the final result depends on the precise order in which the processes execute.
Key Points:
- Race conditions typically occur inside a critical section, where multiple threads access shared resources.
- The result of execution differs according to the order in which threads or processes run, making the output unpredictable and incorrect.
Example Scenario:
- Two processes share a common variable (e.g., a counter).
- Process A reads the value, and before it writes back the updated value, Process B also reads the same old value.
- Both processes write their result, and one update is lost, producing an incorrect final value.
Illustration:
Shared variable: count = 5
Process A: reads count (5), computes count+1 = 6
Process B: reads count (5), computes count+1 = 6 <-- reads before A writes
Process A: writes count = 6
Process B: writes count = 6 <-- should be 7, but is 6 (incorrect!)
Prevention:
Race conditions can be prevented using synchronization mechanisms such as:
- Mutex locks
- Semaphores
- TSL (Test and Set Lock) instruction -- a shared lock variable is set to 1 before entering the critical section and reset to 0 after leaving, ensuring mutual exclusion.
In summary, race conditions arise due to uncontrolled concurrent access to shared data, and must be carefully managed to ensure correctness of programs.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.