Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1 - Create a new abstract class UserlandProcess that implements Runnable. This will be the base class for every test program that we write. Your

1-Create a new abstract class "UserlandProcess" that implements Runnable. This will be the base class for every test program that we write. Your userland process should have a Java Thread and a Semaphore. The thread will allow your user program to run, the semaphore will stop in when we should cooperate. One more member - a Boolean that indicates that our quantum is expired.
You will need some methods:
void requeststop()- sets the boolean indicating that this process' quantum has expired. abstract void main()- will represent the main of our "program". boolean isstopped()- indicates if the semaphore is 0. boolean isDone()- true when the Java thread is not alive. void start()- releases (increments) the semaphore, allowing this thread to run. oid stop()- acquires (decrements) the semaphore, stopping this thread from running.. void run()- acquire the semaphore, then call main
void cooperate()- if the boolean is true, set the boolean to false and call os. switchprocess()
2-Test Programs
Create two new classes derived from UserlandProcess - HelloWorld and GoodbyeWorld. You will have to create a "main" method (because of Runnable). In each, make an infinite loop that just prints "Hello World" or "Goodbye world". Make sure that each calls cooperate() inside the loop. If you forget this, your OS will never switch processes. We will also create a Userland process called the idle process. It runs an infinite loop of cooperate() and Thread.sleep(50).
3-Create the OS class. Everything in the OS class will be static, so we never need to create an instance of it. It will have a private static reference to the one and only instance of the Kernel class.
The OS class has a fundamental problem to solve - it is the gateway between the userland thread and the kernel thread. Consider what has to happen if your user program calls a kernel function.
But how does the kernel, an independent thread, know what to do? How does it know if you want to open() a file or fork()? The answer is that we will have a shared data area. OS will "leave a note" for the kernel - "here is what I want you
do to".
Let's lay out that data area. We need:
An enum of what function to call (I called this "CallType")
A static instance of that enum (I called this "currentCall")
A static array list of parameters to the function; we don't know what they will be, so we will make it an arraylist of Object.
The return value. In a similar way, we don't know what the return value type will be, so make it a static Object.
For each kernel call, we will have to:
Reset the parameters.
Add the new parameters to the parameter list.|
Set the currentCall.
Switch to the kernel (more on this later)
Cast and return the return value.
Create two methods in OS:
public static int createProcess(UserlandProcess up)
Make an enum entry for createProcess, and follow the steps above
public static void startup(UserlandProcess init)
Creates the Kernel() and calls createProcess twice - once for "init" and once for
the idle process..
4-Create a Kernel class with a member of type Scheduler (we will make this later). Kernel will have some things very similar to the Userland process - the thread and semaphore and the start() method. Write an appropriate constructor to initialize these, then call thread.start().
The run() method is an infinite loop:
while (true)
mxSemaphore,acquice()- to see if I should be running
switch on QS currentCall - for each of these, call the function that implements them call run() on the next process to run (we will see this in the scheduler).
Implement the run() method. The only "currentCall" values we are expecting is CreateProcess and SwitchProcess (more on these in the scheduler, below).
5-Scheduler Class
Finally, create the Scheduler. The scheduler will have a private LinkedList to hold the list of process that the scheduler knows about, a private instance of the Timer class (java.util.Timer there are a few timer classes in Java) and a public reference to the UserlandProcess that is currently running. The constructor should schedule (using the timer) the interrupt for every 250ms. Inside the interrupt, call requestStop() on the currently running process.
The scheduler needs two methods:
public int CreateProcess(UserlandProcess up)
public void SwitchProcess()
CreateProcess should add the userland process it to the list of processes and, if nothing else is running, call switchProcess() to get it started. SwitchProcess job, overall, is to take the currently running process and put it at the end of the list. It then takes the head of the list and runs it. Two possible corner cases:1) Nothing is currently running (we are at startup). We just dont put null on our list. 2) The user process is done() we just dont add it to the list.You might ask runs it? Thats actually easy we are just going to set currentlyRun" to the new process. create a Main class , Call OS.Startup() with a new HelloWorld then CreateProcess() with new GoodbyeWorld(). add a sleep to hello and goodbye world. Thread.sleep(50)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transactions On Large Scale Data And Knowledge Centered Systems Xxiv Special Issue On Database And Expert Systems Applications Lncs 9510

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Hendrik Decker ,Lenka Lhotska ,Sebastian Link

1st Edition

366249213X, 978-3662492130

More Books

Students also viewed these Databases questions

Question

Please make it fast 8 7 1 .

Answered: 1 week ago