#!/usr/bin/python
#
# Example code for UNSW cs3231 Operating Systems
#
# OS161 thread and semaphore emulation, with a deadlock detection algorithm

# Python modules
import sys

# Our modules
import sim161    # The os161 emulation code
import deadlock  # The deadlock detection algorithm

# Say hello
print "cs3231 Lock Example Code starting"

# check for commandline arguments
if( len(sys.argv) < 2 ):
    print "Usage: %s <algorithm> <param> ..." % sys.argv[0]
    print "<algorithm> is the module name implementing the algorithm to"
    print "test for deadlock. Do not need the '.py' on the module name"
    print "<param> is passed through to the algorithm"
    sys.exit(1)

# Import the algorithm module dynamically
# If there is something wrong with the module, this will barf
# with a traceback so you can debug!
algname = sys.argv[1]
# Strip off the '.py'?
if( algname[-3:] == ".py" ):
    algname = algname[:-3]
algorithm = __import__(algname)

# Call the algorithm
# It will create the necessary threads and semaphores
# This creates a sim161 thread on the 'main' function in the
# algorithm module. This also passes on command-line arguments
sim161.thread_create( "main", algorithm.main, sys.argv[2:] )

# Wait for the simulator to exit
sim161.wait_for_exit()

# Say goodbye
print "cs3231 Lock Example Code exiting"
