private void btnSave_Click(object sender, EventArgs e) { int error = 0; try //if theres no exception in this block its going on { p1 = new NewProcess(); p1.name = boxName.Text; p1.ID = Convert.ToInt32(boxID.Text); p1.time = Convert.ToInt32(boxTime.Text); p1.switchTime = Convert.ToInt32(boxSW.Text); form.AddProcess(p1); } catch (Exception) //if theres exception n the try block it'll proccess here { MessageBox.Show("Заполните все поля!"); error = 1; } if (error != 1) { boxName.Text = ""; boxTime.Text = ""; boxSW.Enabled = false; int tempID = Convert.ToInt32(boxID.Text); //auto increment ID field tempID = tempID + 4; boxID.Text = tempID.ToString(); } } //Сохранить
public void AddProcess(NewProcess tempProcess) { //Create Strings of each process value and send to Data Grid View string ID = tempProcess.ID.ToString(); string name = tempProcess.name; string time = tempProcess.time.ToString(); string switch_time = tempProcess.switchTime.ToString(); string[] tempProcessArray = { ID, name, time, switch_time, "Готов" }; dataGridView.Rows.Add(tempProcessArray); //Add the process to processess list processesList.AddLast(tempProcess); }
//----------------Main Round Robin Algorithm Method------------------- public void runRoundRobin(ref NewProcess[] multiNewProcesses, int quantum) { int hv = 0, wh = 0, ww = 0; //for drawing rect; theyre like x, y, x. //Assign Each Process Its Execution Time foreach (var NewProcess in multiNewProcesses) { //RemaingTime = Total Time @ when process starts execution //because of the switch time (время переключения) its given in % so like if, for example, sw =10% from time, so remaining time = NewProcess.remainingTime = NewProcess.time - (NewProcess.time * NewProcess.switchTime / 100); drawRect(NewProcess.name, 0 + ww, (pictureBox1.Height - 30), Brushes.DarkKhaki); //show names of processes ww += 80; //in order to draw them with spaces, its like margin-right } while (true) { //Close loop on default, if the value is not changed due to available processes executions bool executionFinished = true; //Loop through all processes until the loop ends foreach (var NewProcess in multiNewProcesses) { if (NewProcess.remainingTime == 0) { NewProcess.status = "Завершен"; updateDataGridView(dataGridView, multiNewProcesses); } //Check if the process has any burst time left else if (NewProcess.remainingTime > 0) { //Continue the loop, as a process is executing now and we need to recheck for others executionFinished = false; //Check if the process remaining time is greater than quantum if (NewProcess.remainingTime > quantum) { //Process Status to Running as its Under Execution NewProcess.status = "Выполняется"; updateDataGridView(dataGridView, multiNewProcesses); executionTimer(quantum); //Remove the quantum time from the remaining time NewProcess.remainingTime = NewProcess.remainingTime - quantum; //Swap Process to Ready State after execution and continue for next NewProcess.status = "Готов"; updateDataGridView(dataGridView, multiNewProcesses); string str = (NewProcess.remainingTime).ToString(); drawRect(str, 0 + hv, (pictureBox1.Height - 70) - wh, Brushes.BlueViolet); //draw rects with remaining times } //Only runs when the process is on its last cpu burst cycle else { //Process Status to Running as its Under Execution NewProcess.status = "Выполняется"; updateDataGridView(dataGridView, multiNewProcesses); executionTimer(NewProcess.remainingTime); //Set remaining time to 0, as the last cpu burst ended NewProcess.remainingTime = 0; //Change Process Status to Completed NewProcess.status = "Завершен"; updateDataGridView(dataGridView, multiNewProcesses); string str = (NewProcess.remainingTime).ToString(); drawRect(str, 0 + hv, (pictureBox1.Height - 70) - wh, Brushes.BlueViolet); } } NewProcess last_one = multiNewProcesses.Last(); // this is for building rects in columns, so if its the last process so the next one is gonna be set above the first one if (NewProcess.ID == last_one.ID) { hv = 0; wh += 30; // y } else { hv += 80; // x } } //When All Processes have completed their execution if (executionFinished == true) { break; } } }