Download presentation
Presentation is loading. Please wait.
Published byMay Terry Modified over 9 years ago
1
Getting Started on Lab 1 ksu@andrew
2
Requirements MigratableProcess TransactionalFile{Input, Output}Stream ProcessManager MigratableProcesses
3
MigratableProcess A simple interface which extends two other interfaces: Runnable: – void run(); – Given an object, can start a new thread Serializable: – Can marshal/unmarshal the object – Does NOT serialize volatile (and transient) fields You’ll also want void suspend(); declared
4
TransactionalFile{Input, Output}Stream Traditional file input/output Pseudocode: – File opened once – Read: Read n bytes from file descriptor What’s the problem here?
5
TransactionalFile{Input, Output}Stream A transactional read Read: – Open file each time for read on first open initialize i := 0 – Read n bytes starting from i – i = i + n – Close file State kept in class, not file descriptor
6
ProcessManager The meat of the project Several important parts to this: – Command line parser (robust!!!) – Launching a process – Joining terminated threads – Lastly, the distributed part
7
Command Line Parsing Look at String.split() – Breaks a string apart – “This lab is fun” => {“This”, “lab”, “is”, “fun”} Make sure your parser is robust – Should not crash on unrecognized input – Test it yourself, because we will :D
8
Launching a process Object to be run given to you as a String What do we do now? – The Reflection API!!! Can use it to get the class from a String – Class myClass = Class.forName(“Foo”); Can also get the constructor – Constructor myCtor = myClass.getConstructor() Thread t = new Thread(myCtor.newInstance(…));
9
Joining a thread Threads have a join function Waits for that thread to die – Can be used to wait for threads to finish Be sure to maintain your list of running “processes” as well
10
The “Distributed” Part: Communication Can use Sockets – 2-way communication – “Socket” class in Java We want to send objects across the network – Here’s where the “serializable” comes into play – “Object{Input, Output}Stream”
11
The “Distributed” Part: Master/Slave Master – accepts incoming connections from slaves (does not need to know their hostname ahead of time!) Slave – connects to master – Must know hostname ahead of time – The “-c [masterhostname]” flag
12
The “Distributed” Part: Load Balancing Every 5 seconds or so, you’ll want to check the load across all nodes Redistribute the load Use the suspend here – Suspend process – Package it up – Send to slave node to run
13
Migratable Processes There’s a Grep example on the course website These must have a run() and a suspend() run() – Called to start the process – Called for both the initial start as well as resuming suspend() – Called to tell a process to pause – Way for a process to gracefully save its state – Blocks until the run() is finished suspending (why??)
14
Migratable Processes To write one, wrap the main work loop with a check on a “suspending” variable Don’t forget to make it volatile (why?) When loop breaks, save state, then restore suspending to old value – This will wake up the suspend() call
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.