调度算法的实现
Implementation of scheduling algorithm
我有一项任务需要开发进程调度程序的模拟。我已经设置了大部分,但我一直在弄清楚实际安排流程的逻辑。现在我创建了一个结构,用于保存进程及其 pid、周期计数和内存需求。我使用了一个向量来保存所有 100 个进程,因此我可以根据需要对它们进行排序,并在我安排它们时删除进程。
对于 5 个处理器,我刚刚创建了一个 int 数组,但我猜处理器应该有循环计数和内存限制?在那种情况下可能是另一个结构?
我实际上如何为这 5 个处理器分配进程?有人可以帮我弄清楚吗?我只需要实现一个简单的FCFS算法即可。
void init_process_list(vector<process> &p_list) {
generator generate; // Random number generator class
process p;
for(int i = 0; i < process_count; i++) {
p.process_id = i;
p.cpu_cycles = generate.rand_num_between(cycle_lbound, cycle_ubound);
p.mem_footprint = generate.rand_num_between(mem_lbound, mem_ubound);
p_list.push_back(p);
}
}
// Initialize processor array
void init_processor_list(int *processor_list) {
for(int i = 0; i < processor_count; i++)
processor_list[i] = 0;
}
void schedule_processes(vector<process> &p_list, int *processor) {
}
int main() {
vector<process> process_list;
int cpu[processor_count];
init_process_list(process_list);
init_processor_list(cpu);
schedule_processes(process_list, cpu);
}
应该在描述进程调度算法的Windows Internals. I have read a very similar book(没有英文版)中描述。
看看你是否看懂了下面的内容。
#include <iostream>
#include <vector>
#include<thread>
#include <future>
int global_processorID =0;
int global_processID =0;
struct Process
{
int cycles;
int memory;
int pid;
Process()
{
pid = global_processID++;
cycles = rand()%1000;
memory = rand()%100;
}
};
class Processor
{
public:
Processor():processing(false)
{
this->processorID = global_processorID++;
}
bool processsing()
{
return processing;
}
void process(Process& process)
{
processing= true;
std::async(std::launch::async,[&]
{
std::cout<<"Processor ID: "<<this->processorID<<" ,started Processing for cycles"<<process.cycles<<" ,process id :"<<process.pid<<std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(process.cycles*10));
std::cout<<"Done Processing: "<<process.pid<<std::endl;
processing=false;
});
}
int processorID;
private:
bool processing;
};
class scheduler
{
public:
void addPrcessor(Processor& aprocessor)
{
processor.push_back(&aprocessor);
}
void prcess(Process& process)
{
getNextAvilableProcessor()->process(process);
}
Processor* getNextAvilableProcessor()
{
for(auto& a : processor)
{
if(!a->processsing())
return a;
}
std::cout<<"All processors are busy, Waiting for next avilable processor\n";
while(true)
{
for(auto& a : processor)
{
if(!a->processsing())
return a;
}
}
}
private:
std::vector<Processor*> processor;
};
int main()
{
int i =257;
int*ptr = &i;
std::vector<Process> processes(100);
scheduler scheduler;
Processor one;
Processor two;
Processor three;
Processor four;
scheduler.addPrcessor(one);
scheduler.addPrcessor(two);
scheduler.addPrcessor(three);
scheduler.addPrcessor(four);
for(auto& process : processes)
{
scheduler.prcess(process);
};
}
我有一项任务需要开发进程调度程序的模拟。我已经设置了大部分,但我一直在弄清楚实际安排流程的逻辑。现在我创建了一个结构,用于保存进程及其 pid、周期计数和内存需求。我使用了一个向量来保存所有 100 个进程,因此我可以根据需要对它们进行排序,并在我安排它们时删除进程。
对于 5 个处理器,我刚刚创建了一个 int 数组,但我猜处理器应该有循环计数和内存限制?在那种情况下可能是另一个结构?
我实际上如何为这 5 个处理器分配进程?有人可以帮我弄清楚吗?我只需要实现一个简单的FCFS算法即可。
void init_process_list(vector<process> &p_list) {
generator generate; // Random number generator class
process p;
for(int i = 0; i < process_count; i++) {
p.process_id = i;
p.cpu_cycles = generate.rand_num_between(cycle_lbound, cycle_ubound);
p.mem_footprint = generate.rand_num_between(mem_lbound, mem_ubound);
p_list.push_back(p);
}
}
// Initialize processor array
void init_processor_list(int *processor_list) {
for(int i = 0; i < processor_count; i++)
processor_list[i] = 0;
}
void schedule_processes(vector<process> &p_list, int *processor) {
}
int main() {
vector<process> process_list;
int cpu[processor_count];
init_process_list(process_list);
init_processor_list(cpu);
schedule_processes(process_list, cpu);
}
应该在描述进程调度算法的Windows Internals. I have read a very similar book(没有英文版)中描述。
看看你是否看懂了下面的内容。
#include <iostream>
#include <vector>
#include<thread>
#include <future>
int global_processorID =0;
int global_processID =0;
struct Process
{
int cycles;
int memory;
int pid;
Process()
{
pid = global_processID++;
cycles = rand()%1000;
memory = rand()%100;
}
};
class Processor
{
public:
Processor():processing(false)
{
this->processorID = global_processorID++;
}
bool processsing()
{
return processing;
}
void process(Process& process)
{
processing= true;
std::async(std::launch::async,[&]
{
std::cout<<"Processor ID: "<<this->processorID<<" ,started Processing for cycles"<<process.cycles<<" ,process id :"<<process.pid<<std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(process.cycles*10));
std::cout<<"Done Processing: "<<process.pid<<std::endl;
processing=false;
});
}
int processorID;
private:
bool processing;
};
class scheduler
{
public:
void addPrcessor(Processor& aprocessor)
{
processor.push_back(&aprocessor);
}
void prcess(Process& process)
{
getNextAvilableProcessor()->process(process);
}
Processor* getNextAvilableProcessor()
{
for(auto& a : processor)
{
if(!a->processsing())
return a;
}
std::cout<<"All processors are busy, Waiting for next avilable processor\n";
while(true)
{
for(auto& a : processor)
{
if(!a->processsing())
return a;
}
}
}
private:
std::vector<Processor*> processor;
};
int main()
{
int i =257;
int*ptr = &i;
std::vector<Process> processes(100);
scheduler scheduler;
Processor one;
Processor two;
Processor three;
Processor four;
scheduler.addPrcessor(one);
scheduler.addPrcessor(two);
scheduler.addPrcessor(three);
scheduler.addPrcessor(four);
for(auto& process : processes)
{
scheduler.prcess(process);
};
}