示例#1
0
 public Process(string name, Process parent, int priority)
 {
     Name = name;
     Parent = parent;
     Priority = priority;
     HeldResources = new Dictionary<string, int>();
 }
示例#2
0
        private void DestroyProcess(Process process)
        {
            // Cancel pending resource requests.
            if (process.WaitingNode != null)
            {
                var resource = resources[process.WaitingResourceName];
                Node<AccessRequest>.Remove(ref resource.WaitingList, process.WaitingNode);
                process.WaitingNode = null;
                process.WaitingResourceName = null;
            }

            // Release all resources.
            foreach (var kvPair in process.HeldResources)
                resources[kvPair.Key].Available += kvPair.Value;
            process.HeldResources.Clear();

            // Remove from ready queue.
            if (process.ReadyNode != null)
            {
                Node<Process>.Remove(ref readyQueue[process.Priority], process.ReadyNode);
                process.ReadyNode = null;
            }

            // Remove from master process list.
            processes.Remove(process.Name);
        }
示例#3
0
        private void OnInit(InitCommand command)
        {
            // Clear state.
            processes.Clear();
            readyQueue[0] = readyQueue[1] = readyQueue[2] = null;
            resources.Clear();
            resources["R1"] = new Resource("R1", 1);
            resources["R2"] = new Resource("R2", 2);
            resources["R3"] = new Resource("R3", 3);
            resources["R4"] = new Resource("R4", 4);
            resources["IO"] = new Resource("IO", 1);

            // Start the Init process.
            var init = new Process("Init", null, 0) {Status = ProcessStatus.Ready};
            foreach (var resource in resources.Values)
                init.HeldResources[resource.Name] = 0;
            processes.Add(init.Name, init);
            var readyQueueNode = new Node<Process>(init);
            init.ReadyNode = readyQueueNode;
            Node<Process>.AddToBack(ref readyQueue[0], readyQueueNode);
        }
示例#4
0
        private void OnCreate(CreateCommand command)
        {
            // Check for duplicate name.
            if (processes.ContainsKey(command.ProcessName))
            {
                output.WriteLine(Purpose.Error, "A process named \"{0}\" already exists.", command.ProcessName);
                return;
            }

            // Create new process as child of currently running process,
            // or of no process if there's no running process in the system.
            var parent = GetReadyProcess();
            var child = new Process(command.ProcessName, parent, command.Priority);
            foreach (var resource in resources.Values)
                child.HeldResources[resource.Name] = 0;
            processes.Add(child.Name, child);

            // Add to end of parent's linked list of children.
            if (parent != null)
            {
                var childListNode = new Node<Process>(child);
                Node<Process>.AddToBack(ref parent.ChildList, childListNode);
            }

            // Add to end of ready queue for proper priority.
            var readyQueueNode = new Node<Process>(child);
            child.ReadyNode = readyQueueNode;
            Node<Process>.AddToBack(ref readyQueue[child.Priority], readyQueueNode);

            child.Status = GetReadyProcess() == child
                ? ProcessStatus.Running
                : ProcessStatus.Ready;
            if (parent != null && child.Status == ProcessStatus.Running)
                parent.Status = ProcessStatus.Ready;
        }
示例#5
0
 private bool IsChildOf(Process parent, Process possibleChild)
 {
     if (parent.ChildList == null)
         return false;
     var current = parent.ChildList;
     do
     {
         if (current.Data == possibleChild)
             return true;
         if (IsChildOf(current.Data, possibleChild))
             return true;
     }
     while ((current = current.Next) != parent.ChildList);
     return false;
 }
示例#6
0
        private void DestroyProcessTree(Process root)
        {
            // Destroy all children...
            Node<Process>.VisitAll(root.ChildList, DestroyProcessTree);

            // ... before destroying root.
            DestroyProcess(root);
        }
示例#7
0
 public AccessRequest(Process process, int amount)
 {
     Process = process;
     Amount = amount;
 }