Computer operating system assignment 2
Computer operating system assignment 2
The diagram shows that a process can be in one of the five states: new, ready, running, waiting, or
terminated. The arrows indicate the possible transitions between the states. For example, a new
process becomes ready when it is loaded into the main memory, and a ready process becomes
running when it is selected by the CPU scheduler. A running process can become waiting if it requests
an I/O operation or waits for an event, or it can become ready if it is preempted by another process
or completes its time slice. A waiting process becomes ready when the I/O operation or event is
completed. A running process can also become terminated if it finishes its execution or encounters
an error.
Therefore, a process state transition diagram illustrates that a process always has a state during its
life cycle, and it cannot exist without any state. A stateless process would mean that the process has
no information about its current status or previous history, which is not possible for any program that
is being executed by the CPU. A stateless process would also imply that the operating system has no
way of managing or scheduling the process, which would affect the performance and functionality of
the system. Hence, a process cannot exist without any state.
A physical address and a virtual address are two different kinds of memory addresses used in
computing. A physical address refers to the actual location of a byte of data in the main memory,
such as a RAM chip. A virtual address is an abstraction that is used by the operating system and the
programs to access the memory without knowing the physical address. A virtual address is mapped
to a physical address by a hardware device called a memory management unit (MMU)¹².
The conversion of a virtual address into a physical address by the MMU is also known as address
translation. The MMU uses a data structure called a page table to store the mapping between virtual
and physical addresses. A page table consists of entries that indicate which virtual page (a fixed-size
block of memory) corresponds to which physical frame (a fixed-size block of memory in the main
memory). The MMU also uses a register called the page table base register (PTBR) to store the
physical address of the page table¹².
When a program accesses a virtual address, the MMU performs the following steps to translate it
into a physical address¹²:
- The MMU splits the virtual address into two parts: a page number and an offset. The page number
is used to index the page table, and the offset is used to locate the byte within the page.
- The MMU adds the value of the PTBR to the page number to get the physical address of the page
table entry (PTE) for that page.
- The MMU reads the PTE from the main memory and checks if it is valid. If it is not valid, it means
that the page is not present in the main memory, and a page fault occurs. The operating system then
has to load the page from the secondary storage (such as a disk) into an available frame in the main
memory, and update the PTE accordingly.
- If the PTE is valid, the MMU checks if it has the proper protection and access rights for that page. If
not, it means that the program is trying to access a forbidden or restricted page, and a protection
fault occurs. The operating system then has to handle this exception accordingly.
- If the PTE is valid and has the proper protection and access rights, the MMU extracts the frame
number from the PTE and concatenates it with the offset to get the physical address of the byte.
- The MMU accesses the byte from the main memory using the physical address.
This is how a virtual address is converted into a physical address by the MMU. I hope this helps you
understand this concept better.
Explain "race condition" and state how process synchronization is handled using semaphore? Explain
with algorithms.
A race condition is a situation where the outcome of a program depends on the relative timing or
order of execution of multiple threads or processes that share a common resource. A race condition
can lead to unexpected or incorrect results, such as data corruption, deadlock, or inconsistency. For
example, if two threads try to increment the same variable at the same time, they may both read the
same initial value, perform the increment operation, and write back the same incremented value,
resulting in a loss of one increment. ¹²³
Process synchronization is the technique of coordinating the actions of multiple threads or processes
that access a shared resource, such as a variable, a file, or a device. Process synchronization ensures
that concurrent threads or processes execute in a correct and predictable manner, and avoid race
conditions or other synchronization problems. ⁴⁵
Semaphores can be used to implement mutual exclusion, which is the property that only one thread
or process can access a critical section (a section of code that accesses a shared resource) at a time.
To do this, we can use a binary semaphore (a semaphore that can only have values 0 or 1) as a lock.
The lock is initialized to 1, meaning it is available. Before entering the critical section, a thread or
process must acquire the lock by performing a wait operation on the semaphore. This will decrement
the semaphore value to 0, meaning it is locked. After exiting the critical section, the thread or
process must release the lock by performing a signal operation on the semaphore. This will
increment the semaphore value to 1, meaning it is available again. If another thread or process tries
to acquire the lock while it is already locked, it will be blocked until the lock is released.
The following pseudocode shows how semaphores can be used to implement mutual exclusion:
// Thread or process A
wait(lock); // Acquire the lock
// Critical section
signal(lock); // Release the lock
// Thread or process B
wait(lock); // Acquire the lock
// Critical section
signal(lock); // Release the lock
suppose three people are in line waiting for a department store to open for the big sale when the
door opens all three rush the door , but the door is not big enough for all them to pass through at
once . Describe a solution for adressing this deadlock that will allow three people to pass through the
door . which of the 4 necessary deadlock conditions does your solution break? Explain (15)
One possible solution for addressing this deadlock is to use a semaphore to control the access to the
door. A semaphore is an integer variable that represents the number of available units of a certain
resource. A semaphore has two atomic operations: wait and signal. The wait operation decrements
the semaphore value if it is positive (meaning there are available resources), or blocks the calling
thread or process if it is zero or negative (meaning there are no available resources). The signal
operation increments the semaphore value, indicating that a resource has been released, and wakes
up a waiting thread or process if any. ⁵
In this case, the resource is the door, and the processes are the people. We can initialize a binary
semaphore (a semaphore that can only have values 0 or 1) to 1, meaning the door is available.
Before entering the door, a person must perform a wait operation on the semaphore. This will
decrement the semaphore value to 0, meaning the door is occupied. After exiting the door, the
person must perform a signal operation on the semaphore. This will increment the semaphore value
to 1, meaning the door is available again. If another person tries to enter the door while it is already
occupied, they will be blocked until the door is released.
The following pseudocode shows how semaphores can be used to address this deadlock:
// Person A
wait(door); // Enter the door
// Shop inside
signal(door); // Exit the door
// Person B
wait(door); // Enter the door
// Shop inside
signal(door); // Exit the door
// Person C
wait(door); // Enter the door
// Shop inside
signal(door); // Exit the door
This solution breaks the circular wait condition for deadlock, which states that each process must be
waiting for a resource which is being held by another process, which in turn is waiting for the first
process to release the resource. In this case, there is no circular wait because each person waits for
only one resource (the door), and releases it after using it. Therefore, there is no cycle of waiting
processes.