Exemple #1
0
        /*Thuật toán SJF*/
		public void SJF(MainForm mainfrm)
		{
			int index = -1;
			Node head = new Node();
			Node newNode = new Node();

			for (int i = 0; i < this.NumberOfProcess; i++)
			{
				if (!this.Processed[i])
				{
                    // kiểm tra CPU rỗi không
                    if (i != 0)
                    {
                        Node noProcess = new Node("CPU rỗi", -1, 0);
                        this.arrangedProcesses.addNodeAtTheEnd(noProcess);
                    }
					bool flag = true;

                    // tương tự như FCFS
					for (int j = 0; j < this.NumberOfProcess; j++)
					{
						if (!this.Processed[j])
						{
							head = this.Process_List.Head;
							while (head.Number != j)
							{
								head = head.Next;
							}
							if (flag)
							{
								this.TotalTime = head.ArrivalTime;
								index++;
								this.TimeLine[index] = this.TotalTime;
								flag = false;
							}
							if (head.ArrivalTime == this.TotalTime)
							{
								Node node_1 = head.Clone();
								this.ready.addNodeAtTheEnd(node_1);
								this.Processed[j] = true;
							}
						}
					}

                    while (!this.ready.IsEmpty())       // chừng nào DS SS chưa hết
					{
						Node next = this.ready.Head;
						Node p = next;
						int cpuBurstTime = p.CPUBurstTime;

                        // tìm kiếm & sắp xếp các tiến trình 
						while (next != this.ready.Tail)
						{
							if (next.CPUBurstTime < cpuBurstTime)
							{
								cpuBurstTime = next.CPUBurstTime;
								p = next;
							}
							next = next.Next;
						}
						if (next.CPUBurstTime < cpuBurstTime)
						{
							cpuBurstTime = next.CPUBurstTime;
							p = next;
						}

                        // tương tự FCFS
						this.ProcessingTime = cpuBurstTime;
						this.TotalTime += this.ProcessingTime;
						index++;
						this.TimeLine[index] = this.TotalTime;
						newNode = p.Clone();
						this.arrangedProcesses.addNodeAtTheEnd(newNode);
						this.ready.GetNodeP(ref p);
						Node node_2 = this.Process_List.Head;
						
                        for (int k = 0; k < this.NumberOfProcess; k++)
						{
							if (!((node_2.ArrivalTime > this.TotalTime) || this.Processed[node_2.Number]))
							{
								Node node_3 = new Node();
								node_3 = node_2.Clone();
								this.ready.addNodeAtTheEnd(node_3);
								this.Processed[node_3.Number] = true;
							}
							node_2 = node_2.Next;
						}
					}
				}
			}
			this.gantt = new Gantt();
			this.gantt.DrawGantt(this.Process_List, this.TimeLine, this.arrangedProcesses, 
                                 mainfrm.pictureBox2,mainfrm.dtgvSJF, mainfrm.SJFTbx);
		}
Exemple #2
0
        /*Thuật toán FCFS*/
		public void FCFS(MainForm mainfrm)
		{
			int index = -1;             
			Node head = new Node();
			Node newNode = new Node();

			for (int i = 0; i < this.NumberOfProcess; i++)
			{
                if (!this.Processed[i])             // nếu 1 tiến trình i chưa chạy xong
				{
                    // kiểm tra CPU rỗi không
                    if (i != 0)
                    {
                        Node noProcess = new Node("CPU rỗi", -1, 0);
                        this.arrangedProcesses.addNodeAtTheEnd(noProcess);
                    }
                    bool flag = true;       // biến kiểm tra 1 tiến trình đã được sắp xếp chưa

					for (int j = 0; j < this.NumberOfProcess; j++)
					{
                        if (!this.Processed[j])         //  nếu 1 tiến trình j chưa chạy xong
						{
							head = this.Process_List.Head;
                            while (head.Number != j)    // tìm tiến trình thứ j trong danh sách đầu vào
							{
								head = head.Next;
							}

                            if (flag)       // nếu đã được sắp xếp thì thực hiện tính toán
							{
								this.TotalTime = head.ArrivalTime;
								index++;
                                this.TimeLine[index] = this.TotalTime; // cộng vào tổng thời gian
                                flag = false;       // set lại bằng false
							}

                            if (head.ArrivalTime == this.TotalTime) 
							{
								Node node_1 = head.Clone();
                                this.ready.addNodeAtTheEnd(node_1);  // chuyển vào danh sách các tiến trình đã sẵn sàng
                                this.Processed[j] = true;           // đã chạy xong
							}
						}
					}

                    while (!this.ready.IsEmpty())       // chừng nào danh sách sẵn sàng vẫn còn
					{
						Node next = this.ready.Head;
						Node p = new Node();
						
                        newNode = next.Clone();
						this.arrangedProcesses.addNodeAtTheEnd(newNode);
						p = next;
						this.ProcessingTime = next.CPUBurstTime;
						this.TotalTime += this.ProcessingTime;
						index++;
						this.TimeLine[index] = this.TotalTime;
						next = next.Next;
                        this.ready.GetNodeP(ref p);         // lấy Node p ở danh sách sẵn sàng
						Node node_2 = this.Process_List.Head;

						for (int k = 0; k < this.NumberOfProcess; k++)
						{
							if (!((node_2.ArrivalTime > this.TotalTime) || this.Processed[node_2.Number]))
							{
								Node node_3 = new Node();
								node_3 = node_2.Clone();
                                this.ready.addNodeAtTheEnd(node_3);     // add vào cuối danh sách sẵn sàng (quay vòng lại)
                                this.Processed[node_3.Number] = true;   // đã chạy xong
							}
							node_2 = node_2.Next;
						}
					}
				}
			}

            // vẽ biểu đồ Gantt
			this.gantt = new Gantt();
			this.gantt.DrawGantt(this.Process_List, this.TimeLine, this.arrangedProcesses, 
                                 mainfrm.pictureBox1, mainfrm.dtgvFCFS, mainfrm.FCFSTbx);
		}
Exemple #3
0
        /*Thuật toán RR*/
		public void RoundRobin(MainForm mainfrm)
		{
			int index = -1;
			Node head = new Node();
			Node newNode = new Node();
            int quantum = int.Parse(mainfrm.QuantumTbx.Text); // lượng tử thời gian
            // hàm Parse ép kiểu String => số nguyên
			for (int i = 0; i < this.NumberOfProcess; i++)
			{
				if (!this.Processed[i])
				{
                    // kiểm tra CPU rỗi không
                    if (i != 0)
                    {
                        Node noProcess = new Node("CPU rỗi", -1, 0);
                        this.arrangedProcesses.addNodeAtTheEnd(noProcess);
                    }

					head = this.Process_List.Head;
					while (head.Number != i)
					{
						head = head.Next;
					}
					newNode = head.Clone();
                    this.ready.addNodeAtTheEnd(newNode);    // chuyển vào danh sách đã sẵn sàng
					this.Processed[i] = true;
					this.TotalTime = newNode.ArrivalTime;
					index++;
					this.TimeLine[index] = this.TotalTime;
					while (!this.ready.IsEmpty())
					{
						Node p = null;
						if (this.RemainingTime[newNode.Number] <= quantum)
						{
							this.ProcessingTime = this.RemainingTime[newNode.Number];
							p = newNode;
						}
						else
						{
							this.ProcessingTime = quantum;
						}
						Node node_1 = newNode.Clone();
						this.arrangedProcesses.addNodeAtTheEnd(node_1);
						this.TotalTime += this.ProcessingTime;
						index++;
						this.TimeLine[index] = this.TotalTime;
						this.RemainingTime[newNode.Number] -= this.ProcessingTime;
						Node next = this.Process_List.Head;
						for (int j = 0; j < this.NumberOfProcess; j++)
						{
							if (!((next.ArrivalTime > this.TotalTime) || this.Processed[next.Number]))
							{
								Node node_2 = new Node();
								node_2 = next.Clone();
								this.ready.addNodeAtTheEnd(node_2);
								this.Processed[node_2.Number] = true;
							}
							next = next.Next;
						}
						newNode = newNode.Next;
						if (p != null)
						{
							this.ready.GetNodeP(ref p);
						}
					}
				}
			}
			this.gantt = new Gantt();
			this.gantt.DrawGantt(this.Process_List, this.TimeLine, this.arrangedProcesses, mainfrm.pictureBox4, 
								 mainfrm.dtgvRR, mainfrm.RRTbx);
		}
Exemple #4
0
        /* Hàm tạo */
		public Algorithm(LinkedList processesList)
		{
			this.NumberOfProcess = processesList.Length();
			this.Processed = new bool[this.NumberOfProcess];

			for (int i = 0; i <= this.NumberOfProcess - 1; i++)
			{
                this.Processed[i] = false;      // tất cả các tiến trình chưa chạy
			}

			this.RemainingTime = new int[this.NumberOfProcess];
			Node head = processesList.Head;

			while (head != processesList.Tail)
			{
				this.RemainingTime[head.Number] = head.CPUBurstTime;
				head = head.Next;
			}
			this.RemainingTime[head.Number] = head.CPUBurstTime;
			this.ready = new LinkedList();
			this.arrangedProcesses = new LinkedList();
			this.Process_List = processesList;
			this.TimeLine = new int[300];
			this.ProcessingTime = 0;
			this.TotalTime = 0;
			this.gantt = new Gantt();
		}
Exemple #5
0
        /*Thuật toán SJF có ưu tiên*/
        public void SRTF(MainForm mainfrm)
        {
            int index = -1;
            Node head = new Node();
            Node newNode = new Node();

            for (int i = 0; i < this.NumberOfProcess; i++)
            {
                if (!this.Processed[i])
                {
					// kiểm tra CPU rỗi không
					if (i != 0)
					{
						Node noProcess = new Node("CPU rỗi", -1, 0);
						this.arrangedProcesses.addNodeAtTheEnd(noProcess);
					}

                    bool flag = true;
                    // tương tự như 2 thuật toán FCFS & SJF
                    for (int j = 0; j < this.NumberOfProcess; j++)
                    {
                        if (!this.Processed[j])
                        {
                            head = this.Process_List.Head;
                            while (head.Number != j)
                            {
                                head = head.Next;
                            }
                            if (flag)
                            {
                                this.TotalTime = head.ArrivalTime;
                                index++;
                                this.TimeLine[index] = this.TotalTime;
                                flag = false;
                            }
                            if (head.ArrivalTime == this.TotalTime)
                            {
                                Node node_1 = head.Clone();
                                this.ready.addNodeAtTheEnd(node_1);
                                this.Processed[j] = true;
                            }
                        }
                    }

                    while (!this.ready.IsEmpty())   // chừng nào DS SS chưa hết
                    {
                        Node next = this.ready.Head;
                        Node p = next;
                        int remaining = this.RemainingTime[p.Number];     

                        while (next != this.ready.Tail)
                        {
                            if (this.RemainingTime[next.Number] < remaining)    
                            {
                                remaining = this.RemainingTime[next.Number];
                                p = next;
                            }
                            next = next.Next;
                        }
                        if (this.RemainingTime[next.Number] < remaining)
                        {
                            remaining = this.RemainingTime[next.Number];
                            p = next;
                        }

                        int total = this.TotalTime + remaining;
                        Node node_2 = this.Process_List.Head;
                        bool flag2 = false;             // kiểm tra đã được sắp xếp chưa?    

                        for (int k = 0; k < this.NumberOfProcess; k++)
                        {
                            if (!((node_2.ArrivalTime > total) || this.Processed[node_2.Number]))
                            {
                                Node node_3 = new Node();
                                node_3 = node_2.Clone();
                                this.ready.addNodeAtTheEnd(node_3);
                                this.Processed[node_3.Number] = true;
                                this.ProcessingTime = node_2.ArrivalTime - this.TotalTime;
                                this.TotalTime += this.ProcessingTime;
                                index++;
                                this.TimeLine[index] = this.TotalTime;
                                this.RemainingTime[p.Number] -= this.ProcessingTime;
                                newNode = p.Clone();
                                this.arrangedProcesses.addNodeAtTheEnd(newNode);    // copy vào danh sách các tiến trình đã sắp xếp
                                flag2 = true;
                                break;
                            }
                            node_2 = node_2.Next;
                        }

                        if (!flag2) // nếu chưa được sắp xếp thì sắp xếp
                        {
                            this.ProcessingTime = this.RemainingTime[p.Number];
                            this.RemainingTime[p.Number] = 0;
                            this.TotalTime += this.ProcessingTime;
                            index++;
                            this.TimeLine[index] = this.TotalTime;
                            newNode = p.Clone();
                            this.arrangedProcesses.addNodeAtTheEnd(newNode);
                            this.ready.GetNodeP(ref p);
                        }
                    }
                }
            }
            this.gantt = new Gantt();
            this.gantt.DrawGantt(this.Process_List, this.TimeLine, this.arrangedProcesses, 
                                 mainfrm.pictureBox3, mainfrm.dtgvSRTF, mainfrm.SRTFTbx);
        }