Sunday, December 22, 2019

Operating System: Process and Concurrent Execution - Interview Questions and Solutions

Operating System: Process and Concurrent Execution; 

Questions and Solutions




The concepts of a process and concurrent execution; These concepts are at the very heart of modern operating systems. A process is a program in execution and is the unit of work in a modern time-sharing system. Such a system consists of a collection of processes: Operating-system processes executing system code and user processes executing user code. All these processes can potentially execute concurrently, with the CPU (or CPUs) multiplexed among them. By switching the CPU between processes, the operating system can make the computer more productive. We also introduce the notion of a thread (lightweight process) and interprocess communication (IPC).

Exercises
1. Describe the differences among short-term, medium-term, and long-term scheduling.
Answer:
a. Short-term (CPU scheduler)—selects from jobs in memory those jobs that are ready to execute and allocates the CPU to them.
b. Medium-term—used especially with time-sharing systems as an intermediate scheduling level. A swapping scheme is implemented to remove partially run programs from memory and reinstate them later to continue where they left off.
c. Long-term (job scheduler)—determines which jobs are brought into memory for processing.
The primary difference is in the frequency of their execution. The short-term must select a new process quite often. Long-term is used much less often since it handles placing jobs in the system and may wait a while for a job to finish before it admits another one.

2. Describe the actions taken by a kernel to context-switch between processes.
Answer:
In general, the operating system must save the state of the currently running process and restore the state of the process scheduled to be run next. Saving the state of a process typically includes the values of all the CPU registers in addition to memory allocation. Context switches must also perform many architecture-specific operations, including flushing data and instruction caches.

3. Explain the role of the init process on UNIX and Linux systems in regards to process termination.
Answer:
When a process is terminated, it briefly moves to the zombie state and remains in that state until the parent invokes a call to wait(). When this occurs, the process id as well as entry in the process table are both released. However, if a parent does not invoke wait(), the child process remains a zombie as long as the parent remains alive. Once the parent process terminates, the init process becomes the new parent of the zombie. Periodically, the init process calls wait() which ultimately releases the pid and entry in the process table of the zombie process.

4 Give an example of a situation in which ordinary pipes are more suitable than named pipes and an example of a situation in which named pipes are more suitable than ordinary pipes.
Answer:
Simple communication works well with ordinary pipes. For example, assume we have a process that counts characters in a file. An ordinary pipe can be used where the producer writes the file to the pipe and the consumer reads the files and counts the number of characters in the file. Next, for an example where named pipes are more suitable, consider the situation where several processes may write messages to a log. When processes wish to write a message to the log, they write it to the named pipe. A server reads the messages from the named pipe and writes them to the log file.

5. Consider the RPC mechanism. Describe the undesirable consequences that could arise from not enforcing either the “at most once” or “exactly once” semantic. Describe possible uses for a mechanism that has neither of these guarantees.
Answer:
If an RPC mechanism cannot support either the “at most once” or “at least once” semantics, then the RPC server cannot guarantee that a remote procedure will not be invoked multiple occurrences. Consider if a remote procedure were withdrawing money from a bank account on a system that did not support these semantics. It is possible that a single invocation of the remote procedure might lead to multiple withdrawals on the server.
For a system to support either of these semantics generally requires the server maintain some form of client state such as the timestamp described in the text.
If a system were unable to support either of these semantics, then such a system could only safely provide remote procedures that do not alter data or provide time-sensitive results. Using our bank account as an example, we certainly require “at most once” or “at least once” semantics for performing a withdrawal (or deposit!). However, an inquiry into an account balance or other account information such as name, address, etc. does not require these semantics.

6. Using the program shown in Figure 3.35, explain what the output will be at lines X and Y.
Answer:
Because the child is a copy of the parent, any changes the child makes will occur in its copy of the data and won’t be reflected in the parent. As a result, the values output by the child at line X are 0, -1, -4, -9, -16. The values output by the parent at line Y are 0, 1, 2, 3, 4

7. What are the benefits and the disadvantages of each of the following? Consider both the system level and the programmer level.
a. Synchronous and asynchronous communication
b. Automatic and explicit buffering
c. Send by copy and send by reference
d. Fixed-sized and variable-sized messages
Answer:
a. Synchronous and asynchronous communication—A benefit of synchronous communication is that it allows a rendezvous between the sender and receiver. A disadvantage of a blocking send is that a rendezvous may not be required and the message could be delivered asynchronously. As a result, message-passing systems often provide both forms of synchronization.
b. Automatic and explicit buffering—Automatic buffering provides a queue with indefinite length, thus ensuring the sender will never have to block while waiting to copy a message. There are no specifications on how automatic buffering will be provided; one scheme may reserve sufficiently large memory where much of the memory is wasted. Explicit buffering specifies how large the buffer is. In this situation, the sender may be blocked while waiting for available space in the queue. However, it is less likely that memory will be wasted with explicit buffering.
c. Send by copy and send by reference—Send by copy does not allow the receiver to alter the state of the parameter; send by reference does allow it. A benefit of send by reference is that it allows the programmer to write a distributed version of a centralized application. Java’s RMI provides both; however, passing a parameter by reference requires declaring the parameter as a remote object as well.
d. Fixed-sized and variable-sized messages—The implications of this are mostly related to buffering issues; with fixed-size messages, a buffer with a specific size can hold a known number of messages. The number of variable-sized messages that can be held by such a buffer is unknown. Consider how Windows 2000 handles this situation: with fixed-sized messages (anything < 256 bytes), the messages are copied from the address space of the sender to the address space of the receiving process. Larger messages (i.e. variable-sized messages) use shared memory to pass the message.

Operating Systems: Virtual Memory - Exam Questions and Solutions


Operating Systems: Virtual Memory

Example Test Questions and Solutions

    Q1. When do page faults occur? Describe the actions taken by the OS when a page fault occurs.

    Sol: A page fault occurs whenever a process tries  to access a  page which is marked as invalid in the page table entry for that page. A page fault generates an interrupt which invokes the operating system code in privileged mode. The OS then examines some internal table (usually kept with the process control block for this process) to determine if the page is on disk. If the page is on disk (i.e. it really is a valid memory reference) the operating system then allocates a free frame, starts the disk I/O to read the desired page into the newly allocated frame, and starts the next process. When the disk I/O is finished, the internal table kept with the process and page tables are updated to indicate that the page is now in memory. The instruction that was interrupted by the illegal address trap is restarted. The process can now access the page as though it had always been in memory.

Q2.   Suppose we have a system with 32 bit virtual addresses, in which the least-significant bits are used to indicate a 10-bit    page offset.

   a) what is the page size in this system?

   b) how many pages would it take to cover the entire virtual  address space?

   c) if you only bought 16MB of physical memory, how many page frames do you have?

   d) if page table entries are 64 bits long, how big of a single-level page table would you require to map all of virtual memory?

Sol:
 a) 2^10 Bytes = 1KB
 b) 2^22 Pages
 c) 16MB / 2^10 = 2^14 (Frames)
 d) 2^22 * 64 = 2^28(bits) = 2^25 (Bytes)



Q3  Suppose we have a demand-paged memory. The page table is held in registers. It takes 8 milliseconds to service a page fault if an empty frame is available or the replaced page is not modified, and 20 milliseconds if the replaced page is modified. Memory access time is 100 nanoseconds.

Assume that the page to replaced is modified 70 percent of the time. What is the maximum acceptable page-fault rate for an effective access time of no more than 200 nanoseconds?

Sol: We know how to find an effective access time (EAT) for a given page-fault rate (p). Here, we have to find 'p' for given 'EAT' so we set up the following equation and solve for 'p':
(Note: 1 millisecond = 1,000,000 nanoseconds = 1e6 nanoseconds)
      EAT = (1-p)*(100) + (p)*(100 + (1-.7)*(8msec) + (.7)*(20msec))  
              = 100 - 100p + 100p + (2.4e6)*p + (14e6)*p
              = 100 + (16.4e6)*p
      200 = 100 + (16.4e6)*p
      p = 100/16.4e6
     

Q4. Consider a program that generates a sequence of virtual address references that correspond to the following sequence of page references:

      1,2,3,4,1,2,5,6,1,3,1,2,7,6,3,2,1,2,3,6

   (i.e., first it references an address in page #1, then an address in page #2, then an address in page #3, etc.)

   show how pages are populated in physical frames over time, and indicate where page faults occur, for each of the following cases:

   a) LRU page replacement, for each subcase of:
       i) one frame, ii) three frames, iii) 5 frames, iv) 7 frames

   b) FIFO page replacement, for each subcase of:
       i) one frame, ii) three frames, iii) 5 frames, iv) 7 frames

   c) Optimal page replacement, for each subcase of:
       i) one frame, ii) three frames, iii) 5 frames, iv) 7 frames


   Did you see an instance of Belady's anomaly? 

Sol: a) 1 frame: (^ indicates a page fault)
    1 2 3 4 1 2 5 6 1 3 1 2 7 6 3 2 1 2 3 6
    ---------------------------------------
    1 2 3 4 1 2 5 6 1 3 1 2 7 6 3 2 1 2 3 6
    ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
    3 frames:
    1 2 3 4 1 2 5 6 1 3 1 2 7 6 3 2 1 2 3 6
    ---------------------------------------
    1 1 1 4 4 4 5 5 5 3   3 7 7 7 2 2     2
       2 2 2 1 1 1 6 6 6   2 2 2 3 3 3     3
          3 3 3 2 2 2 1 1   1 1 6 6 6 1     6
     ^ ^ ^ ^ ^ ^ ^ ^ ^ ^   ^ ^ ^ ^ ^ ^     ^
    5 frames:
    1 2 3 4 1 2 5 6 1 3 1 2 7 6 3 2 1 2 3 6
    ---------------------------------------
    1 1 1 1       1 1    1      1
       2 2 2       2 2    2      2
          3 3       3 6    6      6
             4       4 4    3      3
                      5 5    5      7
      ^ ^ ^ ^     ^ ^    ^       ^
    7 frames:
    1 2 3 4 1 2 5 6 1 3 1 2 7 6 3 2 1 2 3 6
    ---------------------------------------
    1 1 1 1       1 1             1
       2 2 2       2 2             2
          3 3       3 3             3
             4       4 4             4
                      5 5             5
                         6             6
                                        7
    ^ ^ ^ ^       ^ ^             ^
 b) 1 frame:
    Same as a)

    3 frames:
    1 2 3 4 1 2 5 6 1 3 1 2 7 6 3 2 1 2 3 6
    ---------------------------------------
    1 1 1 2 3 4 1 2 5 6   1 3 2 7 6 3     2
       2 2 3 4 1 2 5 6 1   3 2 7 6 3 2     1
          3 4 1 2 5 6 1 3   2 7 6 3 2 1     6
    ^ ^ ^ ^ ^ ^ ^ ^ ^ ^   ^ ^ ^ ^ ^ ^     ^
    5 frames:
    1 2 3 4 1 2 5 6 1 3 1 2 7 6 3 2 1 2 3 6
    ---------------------------------------
    1 1 1 1     1 2 3     4 5   6
       2 2 2     2 3 4     5 6   1
          3 3     3 4 5     6 1   2
             4     4 5 6     1 2   7
                    5 6 1     2 7   3
    ^ ^ ^ ^     ^ ^ ^     ^ ^   ^
    7 frames:
    Same as a)

 c) 1 frame:
    Same as a)

    3 frames:
    1 2 3 4 1 2 5 6 1 3 1 2 7 6 3 2 1 2 3 6
    ---------------------------------------
    1 1 1 1       1 1    1       7 6       1       6
       2 2 2       2 2    2       2 2       2       2
          3 4       5 6    3       3 3       3       3
    ^ ^ ^ ^       ^ ^    ^        ^ ^       ^       ^
    5 frames:
    1 2 3 4 1 2 5 6 1 3 1 2 7 6 3 2 1 2 3 6
    ---------------------------------------
    1 1 1 1      1 1             1
       2 2 2      2 2             2
          3 3      3 3             3
             4      4 6             6
                     5 5             7
    ^ ^ ^ ^      ^ ^             ^
    7 frames:
    Same as a)

 No cases exhibited Belady's anomaly for this sequence of virtual address references.


Q5.  What is the cause of thrashing? How could a system detect thrashing? Once it detects it, what can it do to eliminate the problem?

Sol: In simple words, thrashing is a situation where whenever a process needs to run, it swaps out some other processes working set to disk. This happens when all the processes working set sizes sum to larger then the amount of physical memory available in the system.

Thrashing  can be detected by the system when the CPU utilization starts decreasing and the number of page faults increases considerably.

To eliminate the problem, the system can either decrease the degree of multiprogramming or can use a local  (or priority) replacement algorithm.

Q6.   Give reasons why the page size in a virtual memory system should be neither too large or too small.

As the page size grows, more and more bits are used in the offset field, meaning the size of a page table can shrink.  Also, there are cache size ramifications. This is an advantage of having larger page size. However, as the page size grows, there is more and more fragmentation (since there are going to be more and more pages brought in that are not fully utilized).  It also takes a longer amount of time to bring in a very large page, which can be bad if you are not using much data on that page. This is why smaller page size is preferable.


Q7. (20 points) Consider the two-dimensional array A:

            int A[100][100];

            where A[0][0] is at location 200, in a paged memory system with pages of size 200 bytes. Each int type needs 4 bytes and A is stored in row-major order (as in C/C++). A small process is in page 0 (locations 0 to 199) for manipulating the array; thus, every instruction fetch will be from page 0. For three page frames, how many page faults are generated by the following array-initialization loops, using LRU replacement, and assuming frame 0 has the process in it and the other two frames are initially empty?

Sol:In this system, each page holds 50 integers and thus one row of A needs 2 pages and entire A needs 2 *100 = 200 pages.

(a)    for (int I=0; I< 100; I++)
for (int J=0; J < 100; J++)
    A[I][J]=0;

In this case, the array A is accessed row by row and thus each row generates 2 page faults as the first reference to a page always generates a page fault. Using LRU, it will generate 200 page faults.

 (b)   for (int I=0; I< 100; I++)
for (int J=0; J < 100; J++)
    A[J][I]=0;

In this case, the array A is accessed column by column and thus the process references 100 pages in each outside loop (I), which is the working set of the program. But we only have 2 frames, and thus each array reference will generate a page fault. Using LRU, it will generate 100 * 100 = 10,000 page faults.

This example shows that a well-written program can be much faster than a program is not carefully written.

Q8. A virtual memory system has a page size of 1024 words, eight virtual pages, and four physical page frames. The page table is as follows:

Virtual page Number  Page Frame Number

        0                        1
        1                        0
        2                        3
        3                        -
        4                        -
        5                        2
        6                        -
        7                        -
            

            a. What is the size of the virtual address space? (How many bits in a virtual address?)

                        13 bits

            b. What is the size of the physical address space? (How many bits in a physical address?)

                        12 bits

            c. What are the physical addresses corresponding to the following decimal virtual addresses: 0, 3728, 1023, 1024, 1025, 7800, 4096?

To solve this problem, you will have to first find the Virtual page number corresponding to the Virutal address, then find the corresponding  frame number from table given above and add the offset value to find out the actual physical address. If the Virtual page is not mapped to any physical frame number, this indicates a page fault.

                        0       000 00 0000 0000        01 00 0000 0000   
                        3728    011 10 1001 0000        Page Fault
                        1023    000 11 1111 1111        01 11 1111 1111
                        1024    001 00 0000 0000        00 00 0000 0000
                        1025    001 00 0000 0001        00 00 0000 0001
                        7800    111 10 0111 1000        Page Fault
                        4096    100 00 0000 0000        Page Fault