Liham Pangaplikasyon

Topics: Virtual memory, Deadlock, Operating system Pages: 22 (6908 words) Published: September 10, 2012
Chapter I
1. Race Condition
Race Condition occurs when two threads access a shared variable at the same time. The first thread reads the variable, and the second thread reads the same value from the variable. Then the first thread and second thread perform their operations on the value, and they race to see which thread can write the value last to the shared variable. The value of the thread that writes its value last is preserved, because the thread is writing over the value that the previous thread wrote.


for ( int i = 0; i < 10000000; i++ )
x = x + 1;

If you had 5 threads executing this code at once, the value of x WOULD NOT end up being 50,000,000. It would in fact vary with each run. This is because, in order for each thread to increment the value of x, they have to do the following: (simplified, obviously)

Retrieve the value of x
Add 1 to the value of x
Store the value of x

Any thread can be at any step in this process at any time, and they can step on each other when a shared resource is involved. The state of x can be changed by another thread during the time between x is being read and when it is written back. Let's say a thread retrieves the value of x, but hasn't stored it yet. Another thread can also retrieve the samevalue of x (because no thread has changed it yet) and then they would both be storing the samevalue (x+1) back in x!

Thread 1: reads x, value is 7
Thread 1: add 1 to x, value is now 8
Thread 2: reads x, value is 7
Thread 1: stores 8 in x
Thread 2: adds 1 to x, value is now 8
Thread 2: stores 8 in x

Race conditions can be avoided by employing some sort of locking mechanism before the code that accesses the shared resource:

for ( int i = 0; i < 10000000; i++ )
//lock x
x = x + 1;
//unlock x

Here, the answer comes out as 50,000,000 every time.


Chapter II
2. Virtual Memory
Virtual memory is a feature of an operating system that enables a process to use a memory (RAM) address space that is independent of other processes running in the same system, and use a space that is larger than the actual amount of RAM present, temporarily relegating some contents from RAM to a disk, with little or no overhead. 2.1 Virtual memory Page table 

Virtual memory Page table  is the data structure used by a virtual memory system in a computer operating system to store the mapping between virtual addresses and physical addresses. Virtual addresses are those unique to the accessing process. Physical addresses are those unique to the hardware, i.e., RAM. The role of page table in operating systems that use virtual memory, every process is given the impression that it is working with large, contiguous sections of memory. In reality, each process' memory may be dispersed across different areas of physical memory, or may have been paged out to a backup storage (typically the hard disk). When a process requests access to its memory, it is the responsibility of the operating system to map the virtual address provided by the process to the physical address where that memory is stored. The page table is where the operating system stores its mappings of virtual addresses to physical addresses.

2.2 Page Replacement Algorithm

This Algorithm is used to specify which page is to written on to disk, when a new page needs to be allocated.

2.3 Different Page Replacement Algorithms:

2.3.1 Not recently used- The not recently used (NRU), sometimes known as the Least Recently Used (LRU), page replacement algorithm is an algorithm that favours keeping pages in memory that have been recently used. This algorithm works on the following principle: when a page is referenced, a referenced bit is set for that page, marking it as referenced. Similarly, when a page is modified (written to), a modified bit is set. The setting of the bits is usually done by the...
Continue Reading

Please join StudyMode to read the full document

You May Also Find These Documents Helpful

  • Liham Pangkaibigan Essay
  • Liham Aplikasyon Sample Essay
  • Liham Ni Pinay Mula Sa Japan Essay

Become a StudyMode Member

Sign Up - It's Free