Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Data.List
- data Process = Process { pid :: String
- ,arrival :: Int
- ,computing :: Int } deriving (Show)
- idle = Process{ pid="IDLE", arrival = -1, computing = -1 }
- instance Eq Process where
- Process { computing = a } == Process { computing = b } = a == b
- instance Ord Process where
- compare x y
- | computing x < computing y = LT
- | computing x > computing y = GT
- | otherwise = EQ
- data State = State { new :: [Process]
- ,run :: Process
- ,ready :: [Process]
- ,time :: Int
- ,chart :: String }
- instance Show State where
- show s = "-- new" ++ "\n" ++ showLst (new s) ++ "\n" ++
- "-- run" ++ "\n" ++ show (run s) ++ "\n" ++
- "-- ready" ++ "\n" ++ showLst (ready s) ++ "\n" ++
- "-- time: " ++ show (time s) ++ "\n" ++
- "-- chart: " ++ show (chart s)
- showLst :: (Show a) => [a] -> String
- showLst [] = ""
- showLst [x] = show x
- showLst (x:xs) = show x ++ "\n" ++ showLst xs
- -- will return tuple of form (ready, new)
- split :: Int -> [Process] -> ([Process], [Process])
- split t [] = ([], [])
- split t xs = (ready, new)
- where
- ready = filter ((<= t) . arrival) xs
- new = filter ((> t) . arrival) xs
- -- will update the state's ready queue
- update_ready :: State -> State
- update_ready s = s { new = b, ready = (ready s) ++ a }
- where
- (a, b) = split (time s) (new s)
- update_run :: State -> State
- update_run s
- | ready s == [] = s
- | computing (run s) <= 0 = s { run = (ready s) !! 0, ready = drop 1 (ready s) }
- | otherwise = s
- update_time :: State -> State
- update_time s = s { time = (time s) + 1, chart = (chart s) ++ " " ++ (pid updated), run = updated }
- where
- updated = update_computing (run s)
- update_computing :: Process -> Process
- update_computing p = p { computing = (computing p ) - 1 }
- fcfs_run :: State -> State
- fcfs_run s
- | ready s == [] && new s == [] && computing (run s) <= 0 = s
- | otherwise = fcfs_run (update_time (update_run (update_ready s)))
- ps = [
- Process{pid="1", arrival = 0, computing = 6},
- Process{pid="2", arrival = 2, computing = 6},
- Process{pid="3", arrival = 4, computing = 5},
- Process{pid="4", arrival = 12, computing = 4},
- Process{pid="5", arrival = 16, computing = 3},
- Process{pid="6", arrival = 19, computing = 6}
- ]
- example = State { new=ps
- ,run=idle
- ,ready=[]
- ,time=0
- ,chart="" }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement