Ejemplo n.º 1
0
 public SpinLock(SimulatorThread thread, int timeout = int.MaxValue)
 {
     this.locked  = false;
     this.process = null;
     this.thread  = thread;
     this.timeout = timeout;
 }
Ejemplo n.º 2
0
 public int Down(ref SimulatorProcess process)
 {
     if (!insideProcesses.Contains(process) && !waitingProcesses.Contains(process))
     {
         if (slots > 0)
         {
             process.AllocatedResources.Add(resource);
             insideProcesses.Add(process);
             process.OwnsSemaphore       = true;
             process.WaitingForSemaphore = false;
             process.PreviousState       = process.CurrentState;
             process.CurrentState        = EnumProcessState.WAITING_SEMAPHORE;
         }
         else
         {
             waitingProcesses.Enqueue(process);
             process.RequestedResources.Add(resource);
             process.OwnsSemaphore       = false;
             process.WaitingForSemaphore = true;
             process.PreviousState       = process.CurrentState;
             process.CurrentState        = EnumProcessState.WAITING_SEMAPHORE;
         }
     }
     else
     {
         MessageBox.Show("Can not capture a process that has already been captured by the semaphore");
         return(int.MinValue);
     }
     return(slots);
 }
Ejemplo n.º 3
0
 public SpinLock(SimulatorProcess process, int timeout = int.MaxValue)
 {
     this.locked  = false;
     this.process = process;
     this.thread  = null;
     this.timeout = timeout;
 }
        private async Task <int> RemoveProcessFromReadyQueue(SimulatorProcess proc)
        {
            List <SimulatorProcess> tempList = osCore.Scheduler.ReadyQueue.ToList();

            tempList.Remove(proc);
            osCore.Scheduler.ReadyQueue = new Queue <SimulatorProcess>(tempList);
            return(0);
        }
        private async void btn_Remove_Waiting_Click(object sender, RoutedEventArgs e)
        {
            SimulatorProcess selectedProcess = (SimulatorProcess)lst_WaitingProcesses.SelectedItem;

            if (selectedProcess == null)
            {
                MessageBox.Show("Please Select a Process");
                return;
            }
            await RemoveProcessFromWaitingQueue(selectedProcess);
            await UpdateInterface();
        }
        private void btn_Show_PCB_Waiting_Click(object sender, RoutedEventArgs e)
        {
            SimulatorProcess selectedProcess = (SimulatorProcess)lst_WaitingProcesses.SelectedItem;

            if (selectedProcess == null)
            {
                MessageBox.Show("Please Select a Process");
                return;
            }
            ProcessControlBlockWindow window = new ProcessControlBlockWindow(this, new LinkedListNode <ProcessControlBlock>(selectedProcess.ControlBlock));

            window.Show();
        }
Ejemplo n.º 7
0
 public SimulatorThread(ThreadFlags flags)
 {
     ownerProcess        = flags.ownerProcess;
     childThreads        = flags.childThreads;
     controlBlock        = flags.controlBlock;
     executionUnit       = flags.executionUnit;
     currentState        = flags.currentState;
     previousState       = flags.previousState;
     threadPriority      = flags.threadPriority;
     ownsSemaphore       = flags.ownsSemaphore;
     waitingForSemaphore = flags.waitingForSemaphore;
     resourceStarved     = flags.resourceStarved;
     allocatedResources  = flags.allocatedResources;
     requestedResources  = flags.requestedResources;
     terminated          = flags.terminated;
     lotteryTickets      = flags.lotteryTickets;
 }
        private async void btn_Resume_Waiting_Click(object sender, RoutedEventArgs e)
        {
            SimulatorProcess selectedProcess = (SimulatorProcess)lst_WaitingProcesses.SelectedItem;

            if (selectedProcess == null)
            {
                MessageBox.Show("Please Select a Process");
                return;
            }
            if (osCore.Scheduler.RunningProcess != null)
            {
                osCore.Scheduler.ReadyQueue.Enqueue(osCore.Scheduler.RunningProcess);
            }
            await RemoveProcessFromWaitingQueue(selectedProcess);

            osCore.Scheduler.RunningProcess = selectedProcess;
            await UpdateInterface();
        }
Ejemplo n.º 9
0
 /// <summary>
 /// This function releases a process from the semaphore
 /// </summary>
 /// <param name="process"> a reference to the process to release</param>
 /// <returns>the number of free allocation slots available after releasing the process</returns>
 public int Up(ref SimulatorProcess process)
 {
     if (insideProcesses.Contains(process))
     {
         insideProcesses.Remove(process);
         process.AllocatedResources.Remove(resource);
         process.WaitingForSemaphore = false;
         process.OwnsSemaphore       = false;
         process.PreviousState       = process.CurrentState;
         process.CurrentState        = EnumProcessState.UNKNOWN;
     }
     else
     {
         MessageBox.Show("Process must be captured by the semaphore before being released");
         return(int.MinValue);
     }
     CaptureNextProcess();
     return(slots);
 }
 private async void btn_Queue_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         SimulatorProcess selectedProcess = (SimulatorProcess)lst_RunningProcesses.SelectedItem;
         if (selectedProcess == null)
         {
             MessageBox.Show("Please Select A Process");
             return;
         }
         osCore.Scheduler.ReadyQueue.Enqueue(osCore.Scheduler.RunningProcess);
         osCore.Scheduler.RunningProcess = null;
         await UpdateInterface();
     }
     catch (Exception ex)
     {
         System.Console.WriteLine(ex.StackTrace);
         MessageBox.Show("An error occurred while moving the selected process to the ready state");
     }
 }
        private async void btn_Wait_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                SimulatorProcess selectedProcess = (SimulatorProcess)lst_RunningProcesses.SelectedItem;
                if (selectedProcess == null)
                {
                    MessageBox.Show("Please Select A Process");
                    return;
                }
                Stopwatch s        = new Stopwatch();
                int       waitTime = (int)cmb_Wait_Period.SelectedItem * 1000;
                osCore.Scheduler.WaitingQueue.Enqueue(selectedProcess);
                await UpdateInterface();

                osCore.Scheduler.RunningProcess = null;
                s.Start();
                while (s.ElapsedMilliseconds < waitTime)
                {
                    await UpdateInterface();
                }
                s.Stop();
                selectedProcess =
                    osCore.Scheduler.WaitingQueue.Where(x => x.ProcessID == selectedProcess.ProcessID).FirstOrDefault();
                await RemoveProcessFromWaitingQueue(selectedProcess);

                if (selectedProcess != null)
                {
                    osCore.Scheduler.ReadyQueue.Enqueue(selectedProcess);
                }
                await UpdateInterface();
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.StackTrace);
                MessageBox.Show("An error occurred while moving the selected process to the waiting state");
            }
        }
        private async void btn_CreateNewProcess_Click(object sender, RoutedEventArgs e)
        {
            long delayMills = 0;

            if (chk_DelayedProcess.IsChecked != null && chk_DelayedProcess.IsChecked.Value)
            {
                if (rdb_DelaySecs.IsChecked != null && rdb_DelaySecs.IsChecked.Value)
                {
                    delayMills = ((Convert.ToInt64(cmb_Arrival_Delay.SelectedValue) * 1000));
                    await Delay(delayMills);
                }
                else if (rdb_DelayTicks.IsChecked != null && rdb_DelayTicks.IsChecked.Value)
                {
                    delayMills = (long)(Convert.ToInt64(cmb_Arrival_Delay.SelectedValue) * sld_ClockSpeed.Value);
                    await Delay(delayMills);
                }
                else
                {
                    MessageBox.Show("Invalid Delay Time Unit Selected");
                }
            }
            if (osCore == null)
            {
                CreateOsCore(); // try to create the OS Core
                if (osCore == null)
                {
                    MessageBox.Show("Failed To create OS Core");
                    return;
                }
            }
            // if the OS Core was successfully created

            ProcessFlags?processFlags = CreateProcessFlags();  // create the process flags

            if (processFlags == null)
            {
                MessageBox.Show("Failed To create process invalid flags");
                return;
            }
            SimulatorProcess proc = osCore.CreateProcess(processFlags.Value);            // create the process

            processes.Add(proc);                                                         // add the process to the OS
            if (osCore.Scheduler == null)                                                // if the scheduler is null try to create it
            {
                SchedulerFlags?schedulerFlags = CreateSchedulerFlags(globalFlags.Value); // create scheduler flags
                if (schedulerFlags != null)                                              // if the flags are valid
                {
                    osCore.Scheduler = new Scheduler(schedulerFlags.Value);              // create a new Scheduler
                }
                else
                {
                    MessageBox.Show("Scheduler flags are invalid");
                }

                if (osCore.Scheduler == null)
                {
                    MessageBox.Show("Scheduler is NULL");
                    return;
                }
            }
            osCore.Scheduler.ReadyQueue.Enqueue(proc); // add the process to the ready queue
            await dispatcher.InvokeAsync((Func <Task <int> >) UpdateInterface);
        }