Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tutorial 2: Homework 1 and Project 1

Similar presentations


Presentation on theme: "Tutorial 2: Homework 1 and Project 1"— Presentation transcript:

1 Tutorial 2: Homework 1 and Project 1

2 Homework 1 Q1. . There are several design goals in building an operating system, for example, resource utilization, timeliness, robustness, and so on. Give an example of two design goals that may contradict one another. A: Consider fairness and real time. Fairness requires that each process be allocated its resources in a fair way, with no process getting more than its fair share. On the other hand, real time requires that resources be allocated based on the times when different processes must complete their execution. A real-time process may get a disproportionate share of the resources.

3 Homework 1 Q2: What is the difference between kernel and user mode? Explain how having two distinct modes aids in designing an operating system. A: Most modern CPUs provide two modes of execution: kernel mode and user mode. The CPU can execute every instruction in its instruction set and use every feature of the hardware when executing in kernel mode. However, it can execute only a subset of instructions and use only subset of features when executing in the user mode. Having two modes allows designers to run user programs in user mode and thus deny them access to critical instructions.

4 Homework 1 Q3: List some differences between personal computer operating systems and mainframes. A: …

5 An answer: Mainframes: PC: Designed for throughput and reliability
Can run multiple OS at the same time (?) Designed to handle hundreds of users concurrently PC: Designed for convenience and user experience (nice gui etc.) Typically has to manage a single user at a time What else?

6 Homework 1 Q4: What is the difference between a trap and an interrupt?
A: A trap instruction switches the execution mode of a CPU from the user mode to the kernel mode. This instruction allows a user program to invoke functions in the operating system kernel.

7 A better answer? Going by stack overflow: A trap is an exception, whereas an interrupt is generated by hardware. When an exception occurs, e.g. a divide by 0, a trap is used to start a kernel routine to resolve the problem. This is synchronous, so user code is stopped until it is resolved. An interrupt can be generated by a disk coming back with data and is managed asynchronously.

8 Homework 1 Q5: When an interrupt or system call transfers control to the operating system, a kernel stack area separate from the stack of the interrupted process is generally used. Why? A: There are several reasons for using a separate stack for the kernel. Two of them are as follows. First, you do not want the operating system to crash because a poorly written user program does not allow for enough stack space. Second, if the kernel leaves stack data in a user program’s memory space upon return from a system call, a malicious user might be able to use this data to find out information about other processes.

9 Homework 1 Q6: Multiple jobs can run in parallel and finish faster than if they had run sequentially. Suppose that two jobs, each needing 10 minutes of CPU time, start simultaneously. How long will the last one take to complete if they run sequentially? How long if they run in parallel? Assume 50% I/O wait. A: If each job has 50% I/O wait, then it will take 40 minutes to complete in the absence of competition. If run sequentially, the second one will finish 80 minutes after the first one starts. With two jobs, the approximate CPU utilization is 1 − accumulate 20 minutes of CPU time, a job must run for 20/0.375 minutes, or about minutes. Thus running sequentially the jobs finish after 80 minutes, but running in parallel they finish after minutes. Thus, each one gets CPU minute per minute of real time. To accumulate 20 minutes of CPU time, a job must run for 20/0.375 minutes, or about minutes. Thus running sequentially the jobs finish after 80 minutes, but running in parallel they finish after minutes.

10 Project 1 From scratch: Your job is to make the timer_sleep function stop busy waiting. Currently it calls thread_yield() Thread_yield() is the command used to put a thread of the ready queue to run. Thus, putting it in that queue just makes it run the loop again (especially if there are no other threads) What we want to do is block (thread_block()) until we wake the thread up again To do this we need to keep track of threads we block, so we can check if they should wake up, and wake them if necessary

11 Project 1 How to start: Timer_sleep(): Waking threads:
You need a list to keep track of the threads. You can initiate this in timer.c (there is an initilisation function there that would work). You probably have to define it somewhere before initiating it Timer_sleep(): When a thread is meant to stop, you want to add it to the list and block the thread (thread_block()) However, you also need to keep track of when it should wake up… Waking threads: You need to store when a thread should wake up. The thread itself should maintain this information (a variable in its structure). You should assign it before you block the thread (start + ticks). There is a little work related to turning off interrupts for the blocked threads, then turning them back on later. Make it handle waking one thread, then worry about making it work for multiple (correct order etc.)

12 Monitors How do java monitors work?
Java uses wait(), notify(), and notifyAll() Wait(): When wait() is invoked, the current thread is suspended. The Java run-time system places the thread in an internal wait queue associated with an object. The JRM is running, not the object* Notify(): A waiting thread is picked at random (if exists) and removed from the internal wait queue. The new thread must re-obtain the synchronization lock for the target object. This will always cause it to block at least until the thread calling notify() releases the lock. It will continue to block if a different thread gets the lock before it. NotifyAll(): Does the same thing as notify but unblocks all threads Other: You can pass an argument to wait() which specifies the maximim time to wait in the queue. If that time passes, notify() is called automatically.

13 Hardware supported concurrency
Swap (a, b) Switch two memory locations Compare and Swap (a, b, c) A different atomic function from Swap Compare a given value with a memory location. If they are the same, modify the memory location with a new value.

14 Mutual Exclusion - Swap
lock = false; // global var want= true; // local var while(want) { swap(lock,want) } // critical section lock = false; If it wants it, it will switch the the lock and the want value. If the lock is taken (lock = true), it will sit there spinning until the lock is released (lock = false). Then it will switch them (lock = true, want = false), and exit the loop into the CS.


Download ppt "Tutorial 2: Homework 1 and Project 1"

Similar presentations


Ads by Google