Operating system experiment -- process scheduling

Posted by kennethl on Mon, 07 Mar 2022 05:54:46 +0100

Implementation of process scheduling algorithm
Summary:
The process scheduling algorithm is realized, including FCFS (first in first out), SPF (short process priority), HRRF (highest response ratio priority method) and HPF (priority method). A process management simulation system that allows n processes to run concurrently is designed. The system includes simple process control and synchronization, and its process scheduling algorithm can be selected arbitrarily.

Initial value:
Process name arrival time service time priority
a 0 4 0
b 1 3 1
c 2 5 2
d 3 2 3
e 4 4 4
Algorithm description
FCFS (first in first out): select the process that enters the queue first from the ready queue every time, and then run until the process exits or is blocked, and then continue to select the first process from the queue and run.

SPF (short process first): select the process with the shortest service time from the ready queue every time, and then run until the process exits or is blocked, and then continue to select the process with the shortest service time from the queue and run.

HRRF (highest response ratio priority method): each time the process is scheduled, the response ratio priority is calculated first, and then the process with the highest response ratio priority is put into operation. The response ratio calculation formula is response ratio = (process execution time + process waiting time) / process execution time.

HPF (priority method): select the process with the highest priority from the ready queue every time, and then run until the process exits or is blocked, and then continue to select the process with the highest priority from the queue and run.
FCFS:
Algorithm:
//First come, first served
void FCFS_run()
{
//Set current system time
int cur_svc_time = 0;
//Initialization parameters
init();
while(1)
{
//Judge whether there are tasks entering the queue at the current time and join the queue
get_arv_task();
//Judge whether there is a task running at present. If not
if(_cur_task == nullptr || cur_svc_time == 0)
{
//Get first come tasks
_cur_task = get_first_task();
if(_cur_task != nullptr)
{
//Perform the task
STD:: cout < < process < <_ cur_ task->get_ Name() < "start execution - current time is:" < <_ time << “s\n”;
//Calculate system time
cur_svc_time = _cur_task->get_svc_time();
_cur_task->set_start_time(_time);
}
}
//There is no task. The output is completed
if(_cur_task == nullptr && _cur_num == _task_num)
{
STD:: cout < < "execution completed! \ n";
break;
}
//Calculate the remaining running time of the current task
if(cur_svc_time > 0)cur_svc_time–;
//System time + 1
_time++;
//The current process has finished running
if(cur_svc_time == 0 && _cur_task != nullptr)
{
STD:: cout < < process < <_ cur_ task->get_ Name() < < "execution completed - current time is:"<<_ time << “s\n”;
_cur_task->set_fns_time(_time);
}
}
}
Gantt chart:

Execution result:

Average waiting time: 5.4
Weighted turnaround time:
Process name weighted turnaround time
a 1.0
b 2.0
c 2.0
d 5.5
e 3.5
SPF:
Algorithm:
//Short process priority
void SHR_run()
{
int cur_svc_time = 0;
init();
while(1)
{
get_arv_task();

        if(_cur_task == nullptr || cur_svc_time == 0)
        {
			//Get the shortest task
            _cur_task = get_min_task();
            if(_cur_task != nullptr)
            {
                std::cout << "process" << _cur_task->get_name() << "Start execution - the current time is:" << _time << "s\n";
                cur_svc_time = _cur_task->get_svc_time();
                _cur_task->set_start_time(_time);
            }
        }
        if(_cur_task == nullptr && _cur_num == _task_num)
        {
            std::cout << "completion of enforcement!\n";
            break;
        }

        if(cur_svc_time > 0)cur_svc_time--;
        _time++;
        if(cur_svc_time == 0 && _cur_task != nullptr)
        {
            std::cout << "process" << _cur_task->get_name() << "Execution completed - the current time is:" <<_time << "s\n";
            _cur_task->set_fns_time(_time);
        }
    }
}

Gantt chart:

results of enforcement

Average waiting time: 4.4
Weighted turnaround time:
Process name weighted turnaround time
a 1.0
b 2.67
c 3.2
d 1.5
e 2.25
HRRF:
Algorithm:
//High response ratio
void HRRF_run()
{
int cur_svc_time = 0;
init();
while(1)
{
get_arv_task();
update_res_ratio();

        if(_cur_task == nullptr || cur_svc_time == 0)
        {
			//Get the task with the highest response ratio
            _cur_task = get_max_res_ratio();
            if(_cur_task != nullptr)
            {
                std::cout << "process" << _cur_task->get_name() << "Start execution - the current time is:" << _time << "s\n";
                cur_svc_time = _cur_task->get_svc_time();
                _cur_task->set_start_time(_time);
            }
        }
        if(_cur_task == nullptr && _cur_num == _task_num)
        {
            std::cout << "completion of enforcement!\n";
            break;
        }

        if(cur_svc_time > 0)cur_svc_time--;
        _time++;
        if(cur_svc_time == 0 && _cur_task != nullptr)
        {
            std::cout << "process" << _cur_task->get_name() << "Execution completed - the current time is:" <<_time << "s\n";
            _cur_task->set_fns_time(_time);
        }
    }
}

Gantt chart:

Execution result:

Average waiting time: 4.4
Weighted turnaround time:
Process name weighted turnaround time
a 1.0
b 2.67
c 3.2
d 1.5
e 2.25
HPF:
Algorithm:
//Priority algorithm
void HPF_run()
{
int cur_svc_time = 0;
init();
while(1)
{
get_arv_task();

        if(_cur_task == nullptr || cur_svc_time == 0)
        {
			//Get the top priority
            _cur_task = get_max_pri();
            if(_cur_task != nullptr)
            {
                std::cout << "process" << _cur_task->get_name() << "Start execution - the current time is:" << _time << "s\n";
                cur_svc_time = _cur_task->get_svc_time();
                _cur_task->set_start_time(_time);
            }
        }
        if(_cur_task == nullptr && _cur_num == _task_num)
        {
            std::cout << "completion of enforcement!\n";
            break;
        }

        if(cur_svc_time > 0)cur_svc_time--;
        _time++;
        if(cur_svc_time == 0 && _cur_task != nullptr)
        {
            std::cout << "process" << _cur_task->get_name() << "Execution completed - the current time is:" <<_time << "s\n";
            _cur_task->set_fns_time(_time);
        }
    }
}

Gantt chart:

Execution result:

Average waiting time: 5.4
Weighted turnaround time:
Process name weighted turnaround time
a 1.0
b 5.67
c 2.6
d 3.5
e 1.0

Topics: network Operating System security Web Security