Friday, December 11, 2009

Sample Programs

Currently we have two ideas for the syntax of creating sections of code with deadlines.

First is to create a new clause. The grammar would be like this:
S => 'deadline' '(' Int ')' '{' S_list '}' 'catch' '{' S_list '}'

The second would be to create them like coroutines. The grammar would be like this:
E => 'deadline' '(' E ',' E ',' E ')'
Where the first argument is a function with code that needs to execute within the deadline, the second is a catch or failure routine if it does not meet the deadline, and the last is the amount of time it has to execute.


Using the first syntax, here are some programs that we would like to demonstrate.

1. Infinite-loop detection

def x = input()
x = native string.atoi(x)
deadline(100){
    while (x > 0){ // We forget to check if x is positive
        print x
        x = x - 1
    }
}
catch{
    print "Infinite loop detected"
}


2. I/O Timeout

deadline(10000){
    def n = input()
}
catch{
    def n = 10
}
print n

3. Emergency Shutdown

// define classes
def Object = {}
Object.new = lambda(self,o) {
    o = cond(o != null, o, {})
    o.__mt = self
    self.__index = self
    o
}
// A device is connected to the hub.
def Device = Object:new()
// Device.shutdown is a method that safely shutsdown the device.
Device.shutdown = lambda(self){
    def i = 0
    // Waste some time in a loop to emulate a delay.
    while (i < 100){
        i = i + 1
    }
    print "Device has safely shutdown"
}

// Add devices to the hub
def i = 0
while (x > i){
    deviceHub[i] = Device:new()
}

// Error detected in system. We need to shut everything down.

// First try to shut everything down safely
deadline(500){
    for device in listIter(deviceHub){
        device:shutdown()
    }
}
// If it takes too long, immediately cut power to everything to prevent further damage.
catch{
    print "Cutting power to all devices. Devices may be left in a bad state."
}
print "All devices shutdown."

No comments:

Post a Comment