Q1. Problem 5.2 from the AOS text

Provide two programming examples of multithreading that would not improve performance over a single threaded environment.

Answer:

(1) Any kind of sequential program is not a good candidate to be threaded. An

example of this is a program that calculates an individual tax return.

(2) Another example is a "shell" program such as the C-shell or Korn shell. Such a program must closely monitor its own working space such as open files, environment variables, and current working directory.

5.3 Problem 5.2 from the AOS text

What are the two differences between user-level threads and kernel level threads? Under what circumstances is one better than the other?

Answer:

(1) User-level threads are supported above the kernel and implemented by a thread library at the user level Kernel threads are supported directly by the operating system.

(2) User threads are created, scheduled and managed by the thread library in the user space.

Kernel threads are created, scheduled and managed by the kernel in the kernel space.

(3) Every user thread belongs to a process.

Kernel threads need not be associated with a process.

Since user level threads do not require the involvement of the kernel they are fast to create and manage as compared to kernel threads which are managed by the operating system and hence are slower to create and manage.

If the kernel is single threaded and a user-level thread performs a blocking system call the entire process will be blocked, even if other threads are available for running within the application. A kernel thread will not have the above problem because the kernel will schedule another thread for execution.

Also in case of a multiprocessor environment, the kernel can schedule threads on different processors.

 

Problem 6.2 from the AOS text.

Define the difference between preemptive and non-preemptive scheduling. State why strict

non-preemptive scheduling is unlikely to be used in a computer center.

Answer:

Preemptive scheduling allows a process to be interrupted in the midst of its execution, taking the CPU away and allocating it to another process. In the case of non-preemptive scheduling once a process has the CPU it yields control of the CPU only when it enter the waiting state from the running state(for eg. In case of a I/O request) or when the process terminates.

In a computer centre , the processes being served belong to different categories .Some of the processes belong to students, some to professors, some processes are created by the staff for network and resource management . These processes are generally assigned different priorities(the student processes invariably end up with the lowest priority!!) .Also preemption is necessary as a computer centre has a large number of users and the administrator would not like a single user to hog up the CPU for a long period of time.

Problem 6.4 from the AOS text.

Suppose that the following processes arrive for execution at the times indicated. Each

process will run the listed amount of time. In answering the questions, use nonpreemptive

scheduling and base all decisions on the information you have at the time the decision

must be made.

Process Arrival Time Burst Time

P1 0.0 8

P2 0.4 4

P3 1.0 1

a. What is the average turnaround time for these processes with the FCFS scheduling

algorithm?

b. What is the average turnaround time for these processes with the SJF scheduling al-gorithm?

c. The SJF algorithm is supposed to improve performance, but notice that we chose to

run process P1 at time 0 because we did not know that two shorter processes would

arrive soon. Compute what the average turnaround time will be if the CPU is left

idle for the first 1 unit and then SJF scheduling is used. Remember that processes P1

and P2 are waiting during this idle time, so their waiting time may increase. This

algorithm could be known as future-knowledge scheduling.

Answer:

Turnaround time = execution time + time spent waiting

  1. P1 waiting time = 0 execution time = 8 turnaround time = 8

P2 waiting time = 7.6 execution time = 4 turnaround time = 11.6

P3 waiting time = 11 execution time = 1 turnaround time = 12

Therefore average turnaround time for the three processes is 10.53

b.

Since the scheduling is SJF the processec will be scheduled in the order P1 P3 P2.

P1 waiting time = 0 execution time = 8 turnaround time = 8

P3 waiting time = 7 execution time = 1 turnaround time = 8

P3 waiting time = 8.6 execution time = 4 turnaround time = 12.6

Therefore average turnaround time for the three processes is 9.53

c.

Since the scheduler waits for 1 second before executing the processes will be scheduled in the order

P3 P2 P1

P3 waiting time = 0 execution time = 1 turnaround time = 1

P2 waiting time = 1.6 execution time = 4 turnaround time = 5.6

P1 waiting time = 6 execution time = 8 turnaround time = 14

Therefore average turnaround time for the three processes is 6.86

 

5. Semaphores Suppose a building has a limit on the number of people that may be in the building at one time due to a fire code. Suppose this is a very popular place to visit, so the number of people inside must be monitored closely. Further, suppose that this building has more than one entrance and exit. Construct an algorithm that could be used to control a set of turnstiles that would ensure that the room was allowed to be filled but was never allowed to exceed its legal capacity.

Answer:

class TurnstileController
{
    public:
        void Enter();
        void Exit();
        int PrintCurrentOccupants();
        
    private:
        // The number of occupants in the room
        int       occupants;
        
        // A counting semaphore used to track the remaining
        // capacity of the room
        Semaphore capacityRemaining;
        
        // A mutex that only allows one person to enter the room
        // at a time
        Semaphore entranceMutex;
        
        // A mutex that only allows one person to exit the room
        // at a time
        Semaphore exitMutex;
} 

TurnstileController::TurnstileController()
{
    occupants = 0;
    capacityRemaing->value = MAX_CAPACITY;
    entranceMutex->value = 1;
    exitMutex->value = 1;
}

TurnstileController::Enter()
{
    entranceMutex->Wait();
    capacityRemaining->Wait();
    occupants = occupants + 1;
    entranceMutex->Signal();
    
}

TurnstileController::Exit()
{
    exitMutex->Wait();
    occupants = occupants - 1;
    capacityRemaining->Signal();
    exitMutex->Signal();
}