preface

In programming, several processes are always in the ready state at the same time. In order to make each process in the system run methodically, some scheduling strategy must be selected to select one process to occupy the processor. In this experiment, a simulation algorithm of single processor scheduling is designed to deepen the understanding of processor scheduling algorithm.

requirements

  1. The simulation scheduler is designed according to the time slice rotation algorithm.
  2. Output process scheduling.

Thought analysis

Because this experiment simulates processor scheduling according to processor scheduling algorithm, it is not exactly the same with the real processor scheduling process. For example, there is no interruption (time slice is set to 1), and the process is not really running, but printing its running time on the screen. Therefore, the process information should be presented in a file. The file information is as follows:

Process ID Estimated Arrival time Running time Priority 0 0 3 2 1 2 6 4 2 4 4 0 3 6 5 3 4 8 2 1Copy the code

Here’s the general idea:

  1. Set up three queues: PCB queue, ready queue and completion queue. PCB queue: Saves the process that will enter the system. Since no interrupt is implemented, the process to be put into the system must be given before the program runs. Ready queue: The time at which a process enters the system and is put into a ready queue for scheduling. Completion queue: Puts the “run” process into the completion queue.

  2. The process runs by printing information on the screen. For a process scheduled using the rotation algorithm, the following information should be printed: process occupation of the processor sequence, the start time and end time of each occupation of the processor.

  3. Calculate the process turnaround time T and weighted turnaround time W.

The flow chart

The implementation code

  1. ProcessControlBlock.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OperatingSystemExperiment.Exp1 {
    enum ProcessStatus {
        Ready,
        Run,
        Finish
    }

    /// <summary>
    ///Process control block PCB
    /// </summary>
    class ProcessControlBlock {
        /// <summary>
        ///Process of no.
        /// </summary>
        public int ID;

        /// <summary>
        ///Process status
        /// </summary>
        public ProcessStatus Status;

        /// <summary>
        ///Process arrival time
        /// </summary>
        public int ArriveTime;

        /// <summary>
        ///Estimated running time
        /// </summary>
        public int Time;

        /// <summary>
        ///Run time
        /// </summary>
        public int RunTime = 0;

        /// <summary>
        ///Waiting time
        /// </summary>
        public int WaitTime;

        /// <summary>
        ///priority
        /// </summary>
        public int Priority;

        /// <summary>
        ///Link pointer
        /// </summary>
        public ProcessControlBlock Next;

        /// <summary>
        ///The start time
        /// </summary>
        public int StartTime;

        /// <summary>
        ///The end of time
        /// </summary>
        public int FinishTime;

        public void Run()
        {
            this.Status = ProcessStatus.Run;

            if (RunTime >= Time) {
                this.Status = ProcessStatus.Finish;
                return;
            }

            this.RunTime++;
        }

        public void Wait()
        {
            this.WaitTime++;
        }

        public override string ToString() => String.Format("{0} {1} {2}", ID, StartTime, FinishTime); }}Copy the code
  1. CentralProcessUnit.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace OperatingSystemExperiment.Exp1 {
    class CentralProcessUnit {
        private List<ProcessControlBlock> PCBList = new List<ProcessControlBlock>();
        private Queue<ProcessControlBlock> FinishQueue = new Queue<ProcessControlBlock>();
        private Queue<ProcessControlBlock> ReadyQueue = new Queue<ProcessControlBlock>();

        public CentralProcessUnit()
        {
            LoadPcbList();
        } 

        /// <summary>
        ///Build process list
        /// </summary>
        /// <param name="count">Number of processes</param>
        public static void GenerateProcessList(int count)
        {
            var processListFile = Path.Combine(Environment.CurrentDirectory, "process_list.txt");
            var writer = new StreamWriter(processListFile);
            var rnd = new Random(DateTime.Now.Millisecond);
            for (var i = 0; i < count; i++) {
                var runTime = rnd.Next(1.10);
                writer.WriteLine("{0} {1} {2} {3}", i, Math.Pow(2, i), runTime, rnd.Next(0.4));
            }

            writer.Close();
        }

        /// <summary>
        ///Loading PCB List
        /// </summary>
        private void LoadPcbList()
        {
            var processListFile = Path.Combine(Environment.CurrentDirectory, "process_list.txt");
            var reader = new StreamReader(processListFile);
            while(! reader.EndOfStream) {var line = reader.ReadLine();
                var procInfo = line.Split(' ');
                PCBList.Add(new ProcessControlBlock {
                    ID = int.Parse(procInfo[0]),
                    ArriveTime = int.Parse(procInfo[1]),
                    Time = int.Parse(procInfo[2]),
                    Priority = int.Parse(procInfo[3])}); }}/// <summary>
        ///The CPU run
        /// </summary>
        public void Run()
        {
            var times = 0;
            while (true) {
                // If all processes run, exit the loop
                if (FinishQueue.Count == PCBList.Count) {
                    break;
                }

                // Iterate over the list of processes
                foreach (var p in PCBList) {
                    // Determine whether a new process has joined based on the process arrival time, and then set the process state to ready
                    if (p.ArriveTime == times++) {
                        Console.WriteLine("Time: {0}, process {1} arrived", times, p.ID);
                        p.Status = ProcessStatus.Ready;
                    }

                    // Add the ready process to the ready list
                    if (p.Status == ProcessStatus.Ready) {
// console. WriteLine(" time: {0}, process {1} join ready list ", times, p.id);
                        ReadyQueue.Enqueue(p);
                    }

                    // If the ready queue is empty, the next loop is entered
                    if (ReadyQueue.Count == 0) {
// console. WriteLine(" time: {0}, no ready process, enter next loop ", times);
                        continue;
                    }

                    // Run a process from the ready queue
                    var currentProcess = ReadyQueue.Dequeue();
                    Console.WriteLine("Time: {0}, running process {1}", times, p.ID);
                    currentProcess.Run();

                    // Add the completed process to the completion list
                    if (currentProcess.Status == ProcessStatus.Finish) {
                        Console.WriteLine("Time: {0}, process {1} completed, total running time: {2}", times, p.ID, p.RunTime);
                        FinishQueue.Enqueue(currentProcess);
                    }
                    else
                        currentProcess.Status = ProcessStatus.Ready;
                }
            }
        }
    }
}
Copy the code
  1. Main.cs
namespace OperatingSystemExperiment.Exp1
{
    public class Main
    {
        public static void Run()
        {
            CentralProcessUnit.GenerateProcessList(5);
            newCentralProcessUnit().Run(); }}}Copy the code

The results

  • The generatedprocess_list.txtContent:
1 2 4 8 0 3 8 6 3 4 16 4 1Copy the code
  • Console output time: 1, running process 0 time: 2, running process 1 time: 3, running process 2 time: 4, running process 3 time: 5, running process 4 time: 6, running process 0 time: 7, running process 1 time: 8, running process 2 time: 9, Process 3 arrival time: 9, Run process 3 time: 10, run process 4 time: 11, run process 0 time: 12, run process 1 time: 13, run process 2 time: 14, run process 3 time: 15, run process 4 time: 16, run process 0 time: 17, run process 1 time 17, Process 1 is finished running, total running time: 3 time: 18, Process 2 time: 19, Process 3 time: 20, Process 4 time: 21, Process 0 time: 23, Process 2 time: 24, Process 3 time: 25, Process 4 time: 25, Process 4 has finished running, total running time: 4 time: 26, Process 0 time: 28, Process 2 time: 29, Process 3 time: 31, Process 0 time: 33, Process 2 time: 34, Process 3 time: 34, process 3 is finished, total running time: 6 time: 36, process 0 time: 38, process 2 time: 41, process 0 time: 41, process 0 time: 8, total running time: 43, process 2 time: 43, total running time: 8

Welcome to talk to me

  • Play code studio: live.bilibili.com/11883038
  • Wechat official account: DealiAxy
  • Zhihu: www.zhihu.com/people/deal…
  • Blog: blogging. Deali. Cn
  • Jane: www.jianshu.com/u/965b95853…