操作系统编程:进程管理(优先级)

//process.h

// C++下,模拟进程调度,只考虑优先级和内层资源占用
// 清翔兔 05/10/21 第三次修改

#include <iostream>
using namespace std;
const int eve_priority = 3;  //优先级减少数/次
const int eve_time = 5;      //CPU运行时间片/次
const int memMax = 100;      //内存空间

// 进程类
class process_item {
public:
  process_item (int id, int mem, int time, int priority);
  bool _ok;       // 是否已经完成
  bool _on;       // 是否已经进入内层
  int _mem;       // 需要内层空间
  int id () {return _id; }
  int priority () {return _priority; }
  int mem () {return _mem; }
  void over () { _ok=1;}  //表示进程结束
  bool ontime() {return _on;}  //检查是否在内层中
  //void init_priority () { _priority = 0;}
  void init_time () { _time = 0;}  //结束时,将时间归0
  int time () {return _time; }
  //一个时间片后,优先级减少
  int down_priority () { _priority -= eve_priority; return _priority;}
  //一个时间片后,时间减少
  int down_time() {_time -= eve_time; return _time;}
  process_item * next() {return _next;}
  void next ( process_item *p ) { _next = p;}
  void print_item ();
private:
 int _id;
 //int _mood;
 int _time;
 int _priority;
 process_item * _next;
};

//进程项目类
class process {
public:
 process():_front(0),_end(0),_num(0),_memNow(0) {} //进程初始化
 int num() { return _num; }
 int memNow() { return _memNow; }
 void up_num () { _num++; }
 void add (int id, int mem, int time, int priority);
 int up_memNow (process_item* item);
 int down_memNow (process_item* item);
 bool in_mem (process_item* item); //进入内存控制
 void print();
 void done(); //进程主函数
 process_item *whichone(); //选择执行进程
 process_item *front() { return _front;}
private:
 process_item *_front ,*_end;
 int _num;
 int _memNow;   //当前已经被占用的内存空间
};


inline int process::up_memNow( process_item* item )   //进入内存后,增加内存
{
 //cout << item->_on << endl; //测试语句
 if (item->_on==0)
 {
 //cout << item->_mem << " " << _memNow ;  //测试语句
 _memNow +=item->_mem;
 item->_on =1;
 }
 return _memNow;
}

inline int process::down_memNow(process_item* item)  //完成后,离开内层
{
 _memNow -= item->_mem;
 item->_on =0;
 return _memNow;
}

bool process::in_mem(process_item* item)  //测试能否进入内存,可以就进入
{
 if ( item->_ok==1 ) return 0;
 if ( item->_on==1 ) return 1;
 int t;
 t= memNow() + item->_mem;
 //cout << item->_mem <<" t: " << t ;
 if ( t > memMax)  return 0;
 else
 {
  up_memNow (item);
  return 1;
 }
}

inline process_item::
process_item(int id, int mem, int time, int priority)
:_id(id),_mem(mem),_time(time),_priority(priority),_next(0),_on(0),_ok(0){}

void process_item::print_item ()
{
 cout << "id:" << _id
   << "/t priority:" << _priority
   << "/t lefttime:" << _time
   << "/t mem:" << _mem;
}

inline process_item *process::whichone() //选择
{
 process_item *m = _front;
 while (!in_mem(m) )
 {
  if (!m->next()) return m;
  m=m->next();
  
 }
 process_item *p = m->next();
 while ( p!=NULL )
 {
  if ( in_mem(p) //是否进了内层?
   && p->priority() > m->priority() ) m=p;
  p = p->next();
 }
 //up_memNow (m);
 return m;
}

inline void process::add (int id, int mem, int time, int priority)
{
 process_item *p=
  new process_item (id, mem, time, priority);
 if ( !_end)
  _front = _end = p;
 else {
  _end->next(p);
  _end = p;
 }
 up_num();
}

void process::print ()
{
 process_item *p = _front;
 while ( p!=NULL )
 {
  p->print_item();
  p = p->next();
 }
 cout << "Num:" << _num << endl;
}

void process::done()
{
 int tot_time=0;
 process_item *m = whichone();
 while ( m->time() > 0 )
 {
  /*
  // 如下考虑优先级不为0
  if ( m->priority() > eve_time)
   m->down_priority();
  else
   m->init_priority();
  */
  m->down_priority(); //降低优先级
  if ( m->time() > eve_time) //检查时间满足
  {
   tot_time += eve_time;
   m->down_time();
  }
  else //对不满足时间的,进行完,并离开内存
  {
   tot_time += m->time();
   down_memNow( m );
   m->init_time();
   m->over();
  }
  m->print_item();
  cout << "/t/tmemNow:" << memNow() << endl;
  m = whichone();
 }
}

//main.h

#include "process.h"

void main ()
{
 process mytask;
 //int id, int mem, int time, int priority
 mytask.add(1,13,10,3);
 mytask.add(2,91,25,7);
 mytask.add(3,27,15,5);
    mytask.done();
}

没有更多推荐了,返回首页