Advertisement
Guest User

Untitled

a guest
Jun 14th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Data.List
  2.  
  3. data Process = Process { pid        :: String
  4.                         ,arrival    :: Int
  5.                         ,computing  :: Int } deriving (Show)
  6.                        
  7. idle = Process{ pid="IDLE", arrival = -1, computing = -1 }
  8.  
  9. instance Eq Process where
  10.     Process { computing = a } == Process { computing = b } = a == b
  11.  
  12. instance Ord Process where
  13.     compare x y
  14.         | computing x < computing y     = LT
  15.         | computing x > computing y     = GT
  16.         | otherwise                     = EQ
  17.  
  18. data State = State { new      :: [Process]
  19.                     ,run      :: Process
  20.                     ,ready    :: [Process]
  21.                     ,time     :: Int
  22.                     ,chart    :: String }
  23.  
  24. instance Show State where
  25.     show s =    "-- new" ++ "\n" ++ showLst (new s) ++ "\n" ++
  26.                 "-- run" ++ "\n" ++ show (run s) ++ "\n" ++
  27.                 "-- ready" ++ "\n" ++ showLst (ready s) ++ "\n" ++
  28.                 "-- time: " ++ show (time s) ++ "\n" ++
  29.                 "-- chart: " ++ show (chart s)
  30.  
  31. showLst :: (Show a) => [a] -> String
  32. showLst []      = ""
  33. showLst [x]     = show x
  34. showLst (x:xs)  = show x ++ "\n" ++ showLst xs
  35.  
  36. -- will return tuple of form (ready, new)
  37. split :: Int -> [Process] -> ([Process], [Process])
  38. split t []    = ([], [])
  39. split t xs    = (ready, new)
  40.     where
  41.         ready = filter ((<= t) . arrival) xs
  42.         new = filter ((> t) . arrival) xs
  43.  
  44. -- will update the state's ready queue
  45. update_ready :: State -> State
  46. update_ready s = s { new = b, ready = (ready s) ++ a }
  47.     where
  48.         (a, b) = split (time s) (new s)
  49.  
  50. update_run :: State -> State
  51. update_run s
  52.     | ready s == []                 = s
  53.     | computing (run s) <= 0        = s { run = (ready s) !! 0, ready = drop 1 (ready s) }
  54.     | otherwise                     = s
  55.  
  56. update_time :: State -> State
  57. update_time s = s { time = (time s) + 1, chart = (chart s) ++ " " ++ (pid updated), run = updated }
  58.     where
  59.         updated = update_computing (run s)
  60.  
  61. update_computing :: Process -> Process
  62. update_computing p = p { computing = (computing p ) - 1 }
  63.  
  64. fcfs_run :: State -> State
  65. fcfs_run s
  66.     | ready s == [] && new s == [] && computing (run s) <= 0    = s
  67.     | otherwise                                                 = fcfs_run (update_time (update_run (update_ready s)))
  68.  
  69. ps = [
  70.         Process{pid="1", arrival = 0, computing = 6},
  71.         Process{pid="2", arrival = 2, computing = 6},
  72.         Process{pid="3", arrival = 4, computing = 5},
  73.         Process{pid="4", arrival = 12, computing = 4},
  74.         Process{pid="5", arrival = 16, computing = 3},
  75.         Process{pid="6", arrival = 19, computing = 6}
  76.     ]
  77.  
  78. example = State { new=ps
  79.                  ,run=idle
  80.                  ,ready=[]
  81.                  ,time=0
  82.                  ,chart="" }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement