Example #1
0
        private ProcessTableEntry ContextSwitch(int iEnteringProcessId)
        {
            ProcessTableEntry out_process = null;

            if (CPU.ActiveProcess != -1)
            {
                out_process                = m_dProcessTable[CPU.ActiveProcess];
                out_process.Console        = CPU.ActiveConsole;
                out_process.ProgramCounter = CPU.ProgramCounter;
                out_process.LastCPUTime    = CPU.TickCount;
                if (m_spPolicy is RoundRobin || m_spPolicy is PrioritizedScheduling)
                {
                    if (!out_process.Done)
                    {
                        m_spPolicy.AddProcess(iEnteringProcessId);
                        CPU.RemainingTime = out_process.Quantum;
                    }
                }
            }
            ProcessTableEntry in_process = m_dProcessTable[iEnteringProcessId];

            CPU.ActiveProcess      = iEnteringProcessId;
            CPU.ProgramCounter     = in_process.ProgramCounter;
            CPU.ActiveConsole      = in_process.Console;
            CPU.ActiveAddressSpace = in_process.AddressSpace;
            if (m_spPolicy is RoundRobin || m_spPolicy is PrioritizedScheduling)
            {
                CPU.RemainingTime = in_process.Quantum;
            }
            return(out_process);
        }
Example #2
0
        private ProcessTableEntry ContextSwitch(int iEnteringProcessId)
        {
            ProcessTableEntry newProcess = m_dProcessTable[iEnteringProcessId];

            if (CPU.ActiveProcess != -1) // there is a process in the background at the moment
            {
                ProcessTableEntry oldProcess = m_dProcessTable[CPU.ActiveProcess];

                if (oldProcess == newProcess) // we are trying to swittch between same process.. no need to waist time for updating...
                {
                    if ((m_spPolicy is RoundRobin))
                    {
                        CPU.RemainingTime = oldProcess.Quantum; //updaating the quantom of the SAME process
                    }
                    return(oldProcess);
                }



                if (oldProcess.Name != "idle" && oldProcess.Done == false && oldProcess.Blocked == false)
                {
                    oldProcess.outOfBlock = CPU.TickCount;       // this is for knowing the time that a process is ready

                    m_spPolicy.AddProcess(oldProcess.ProcessId); // adding the oldProcess to the schedualing policy because we switched it with the newProcess
                }


                //updating the max starvation for the last function in "Program.cs"
                if (CPU.TickCount - oldProcess.outOfBlock > oldProcess.MaxStarvation)
                {
                    oldProcess.MaxStarvation = (CPU.TickCount - oldProcess.outOfBlock);
                }

                /* this is for RoundRobin only!!! */
                object obj = m_spPolicy as RoundRobin;
                if (obj != null)
                {
                    CPU.RemainingTime = newProcess.Quantum; //updating remaining time of the cpu to be the quantom of the new process
                }



                oldProcess.ProgramCounter = CPU.ProgramCounter;        //update the line, which the old process stop, in the table
                CPU.ActiveProcess         = iEnteringProcessId;        // update the id of the new process as the running process
                CPU.ProgramCounter        = newProcess.ProgramCounter; // put the PC of new process in the cpu so the new process continue from there
                CPU.ActiveAddressSpace    = newProcess.AddressSpace;   //update address space
                CPU.ActiveConsole         = newProcess.Console;        //update console situation

                return(oldProcess);
            }
            else // we are the first program
            {
                CPU.ActiveProcess      = iEnteringProcessId; // update the id of the new process as the running process
                CPU.ProgramCounter     = newProcess.ProgramCounter; // put the PC of new process in the cpu so the new process continue from there
                CPU.ActiveAddressSpace = newProcess.AddressSpace;   //update address space
                CPU.ActiveConsole      = newProcess.Console;        //update console situation
                return(null);
                //return null because we've been asked to return the returning movie but now no movie should return.
            }
        }
Example #3
0
        public void CreateProcess(string sCodeFileName)
        {
            Code code = new Code(sCodeFileName);

            m_dProcessTable[m_cProcesses]           = new ProcessTableEntry(m_cProcesses, sCodeFileName, code);
            m_dProcessTable[m_cProcesses].StartTime = CPU.TickCount;
            m_spPolicy.AddProcess(m_cProcesses);
            m_cProcesses++;
        }
Example #4
0
        public OperatingSystem(CPU cpu, Disk disk, SchedulingPolicy sp)
        {
            CPU                  = cpu;
            Disk                 = disk;
            m_dProcessTable      = new Dictionary <int, ProcessTableEntry>();
            m_lReadRequests      = new List <ReadTokenRequest>();
            cpu.OperatingSystem  = this;
            disk.OperatingSystem = this;
            m_spPolicy           = sp;

            //"idle" process here
            IdleCode idleCode = new IdleCode();

            m_dProcessTable[m_cProcesses] = new ProcessTableEntry(m_cProcesses, "idle", idleCode);
            m_cProcesses++;
        }
Example #5
0
        public OperatingSystem(CPU cpu, Disk disk, SchedulingPolicy sp)
        {
            CPU                  = cpu;
            Disk                 = disk;
            m_dProcessTable      = new Dictionary <int, ProcessTableEntry>();
            m_lReadRequests      = new List <ReadTokenRequest>();
            cpu.OperatingSystem  = this;
            disk.OperatingSystem = this;
            m_spPolicy           = sp;

            IdleCode idleCode = new IdleCode();

            m_dProcessTable[IDLE_PROCESS_ID]           = new ProcessTableEntry(IDLE_PROCESS_ID, "idle", idleCode);
            m_dProcessTable[IDLE_PROCESS_ID].StartTime = CPU.TickCount;
            m_spPolicy.AddProcess(m_cProcesses);
            m_cProcesses++;
        }
        public override int NextProcess(Dictionary <int, ProcessTableEntry> dProcessTable)
        {
            if (active_proccess.Count == 0)
            {
                return(-1);
            }

            foreach (int id in active_proccess)
            {
                ProcessTableEntry proccess = dProcessTable[id];
                if (proccess.ProcessId != 0 && !proccess.Done && !proccess.Blocked)
                {
                    return(id);
                }
            }
            return(-1);
        }
Example #7
0
        public override int NextProcess(Dictionary <int, ProcessTableEntry> dProcessTable)
        {
            if (active_proccess.Count == 0)
            {
                return(-1);
            }
            int id = active_proccess.Dequeue();
            ProcessTableEntry proccess = dProcessTable[id];


            if (!proccess.Done && !proccess.Blocked)
            {
                proccess.Quantum = quantum;
                return(id);
            }

            return(-1);
        }
Example #8
0
        public void Interrupt(ReadTokenRequest rFinishedRequest)
        {
            ProcessTableEntry curProcess = m_dProcessTable[rFinishedRequest.ProcessId];
            int NumOfProcess             = rFinishedRequest.ProcessId;

            //when the token is null, EOF has been reached.
            //we write the value to the appropriate address space of the calling process.
            if (rFinishedRequest.Token == null)
            {
                curProcess.AddressSpace[rFinishedRequest.TargetVariable] = double.NaN;
            }
            else
            {
                //translate the returned token into a value (double).
                double token = Convert.ToDouble(rFinishedRequest.Token);

                curProcess.AddressSpace[rFinishedRequest.TargetVariable] = token;
            }

            curProcess.Blocked    = false;                  //so we will not stuck in the "idle" later on...
            curProcess.outOfBlock = CPU.TickCount;          //the process isn't blocked any more... hence, its ready. so we save the time... for later calculate max starvation

            if (m_dProcessTable[NumOfProcess].Done != true) //didn't finish so i need to schedual via policy
            {
                m_spPolicy.AddProcess(NumOfProcess);
            }


            //activate the next request in queue on the disk.
            if (m_lReadRequests.Count > 0)
            {
                //need to activate next request...
                Disk.ActiveRequest = m_lReadRequests[0];
                m_lReadRequests.RemoveAt(0);
            }


            if (m_spPolicy.RescheduleAfterInterrupt())
            {
                ActivateScheduler();
            }
        }
Example #9
0
        public void Interrupt(ReadTokenRequest rFinishedRequest)
        {
            //implement an "end read request" interrupt handler.
            //translate the returned token into a value (double).
            //when the token is null, EOF has been reached.
            //write the value to the appropriate address space of the calling process.
            //activate the next request in queue on the disk.

            ProcessTableEntry active_process = m_dProcessTable[rFinishedRequest.ProcessId];
            String            var_name       = rFinishedRequest.TargetVariable;
            String            var_value      = rFinishedRequest.Token;

            if (var_value == null)
            {
                active_process.AddressSpace[var_name] = double.NaN;
            }

            else
            {
                active_process.AddressSpace[var_name] = Convert.ToDouble(var_value);
            }

            m_dProcessTable[rFinishedRequest.ProcessId].Blocked = false;
            m_spPolicy.AddProcess(rFinishedRequest.ProcessId);
            m_lReadRequests.Remove(rFinishedRequest);
            if (m_lReadRequests.Count > 0)
            {
                ReadTokenRequest awaiting_procees_request = m_lReadRequests.First();
                //m_dProcessTable[awaiting_procees_request.ProcessId].Blocked = true;
                Disk.ActiveRequest = awaiting_procees_request;
            }
            if (m_spPolicy.RescheduleAfterInterrupt())
            {
                ActivateScheduler();
            }
        }