CS413: Midterm Exam


1. Mention the advantages and disadvantages of non preemptive scheduling.
 
 
 
 

 
 
 
 

2.Why do system calls use software interrupts instead of procedure calls?
 
 
 
 
 
 
 

 

3.What is the difference between a thread and a process? What is stored in a PCB that represents the state of a thread and a process? What are the advantages of using threads instead of processes and vice versa?
 
 
 
 
 

 
 
 

4. What are the advantages and disadvantages of having a small quantum compared to a large quantum in time sharing scheduling.
 
 
 
 
 
 
 
 
 
 
 

5. Write the code for a test-and-set lock() and unlock(). Mention the advantages and disadvantages of test-and-set locks compared to locks implemented by disabling interrupts.
 
 
 
 
 
 
 
 
 

 
 

6. Write the code for the semaphore signal() and wait() operations. Include lock operations. Why are locks necessary in the semaphore operations?
 
 
 
 

 
 
 

 
 

7. The following program consists of two threads: a client thread and a server thread. First the client thread reads the numbers a and b, wakes up the server thread, and then blocks. When the server thread wakes up, it adds a and b, assigns the result to c, wakes up the client thread, and then goes to sleep. When the client wakes up, prints the value of c and starts all over again. Complete the following code and insert semaphore calls where necessary.
 

 
client_thread()
{
    while (1) {
        a = input();
        b = input();
 
        print c;
 
    }

        } 
 
 
 

 

server_thread()
{
    while (1) {
 
        c = a + b;
 
    }

        }
 

8.The following code implements a concurrent stack. The push() and pop() calls can be called simultaneously by different threads. The push() call blocks until space is available in the stack. The pop() call blocks until data is available in the stack. Insert the necessary semaphore calls to implement this behavior.
 
 
 

const int MaxStack = 20;
int stackPointer;
int stack[ MaxStack ];
 
 
 

void push( int a )
{
 
 
 

    stack[ stackPointer ] = a;
    stackPointer++;
 
 
 
 

}
 
 
 

int pop()
{
 
 
 
 

    stackPointer--;
    int tmp = stack[ stackPointer ];
 
 
 

    return tmp;
}

 
 

9. Implement the read/write lock procedures.  lockRead() and unlockRead() aquire and release the lock in read mode. lockWrite() and unlockWrite() acquire and release the lock in write mode. A thread can acquire the lock in read mode as long as no other thread are holding the lock in write mode. A thread can acquire the lock in write mode as long as no other thread is holding the lock in either read or write mode.
 
 

 
 

lockRead()
{
 
 
 
 
 
 
 
 

}
 
 
 

lockWrite()
{
 
 
 
 
 
 
 
 

}

unlockRead()
{
 
 
 
 
 
 
 
 
 
 

}

unlockWrite()
{
 
 
 
 
 
 
 
 
 
 

}

 

10. Write a program that will execute the command "ls -al | grep hello". Do not do error checking.

int main()
{

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

}