用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c++代码库

c++ 磁盘调度算法模拟

2013-03-11 作者: 小蜜锋举报

[c++]代码库

for(i = 0; i < MAXQUEUE; i++) {
    queue[i].go = -1;
    queue[i].visited = 0;
}

start = 53; //磁头的初始位置
}

//读入磁道号流
void readData() {
    FILE *fp;
    char fname[20];
    int temp, i;

    cout << "请输入磁道号流文件名:";
    strcpy(fname, "7hard.txt");
    cin >> fname;

    if((fp = fopen(fname, "r")) == NULL) {
        cout << "错误,文件打不开,请检查文件名:)" << endl;
    } else {
        while(!feof(fp)) {
            fscanf(fp, "%d ", &temp);
            queue[quantity].go = temp;
            quantity++;
        }
        cout << endl << "---------------------------------------------" << endl;
        cout << "所读入的磁道号流:";
        for(i = 0; i < quantity; i++) {
            cout << queue[i].go << " ";
        }
        cout << endl << "请求数为:" << quantity << endl;
    }
}

//FIFO算法
void FIFO() {
    int i;
    int total = 0;
    int current;

    cout << endl << "---------------------------------------------" << endl;
    cout << "FIFO算法的访问磁道号顺序流:";

    current = start;
    for(i = 0; i < quantity; i++) {
        cout << queue[i].go << " ";
        total += abs(queue[i].go - current);
        current = queue[i].go;
    }
    cout << endl << "磁头移过的柱面数:" << total;
}

//最短寻道优先调度算法
void shortest() {
    int i, j, p;
    int total = 0;
    int current;

    cout << endl << "---------------------------------------------" << endl;
    cout << "最短寻道优先调度算法的访问磁道号顺序流:";

    current = start;
    for(i = 0; i < quantity; i++) {
        p = 0;
        while(queue[p].visited != 0) {
            p++;
        }
        for(j = p; j < quantity; j++) {
            if((queue[j].visited == 0) && (abs(current - queue[p].go) > abs(current - queue[j].go))) {
                p = j;
            }
        }
        cout << queue[p].go << " ";
        total += abs(queue[p].go - current);
        queue[p].visited = 1;
        current = queue[p].go;
    }
    cout << endl << "磁头移过的柱面数:" << total;
}

//电梯算法
void elevator() {
    int i, j, p, flag;
    int total = 0;
    int current;

    cout << endl << "---------------------------------------------" << endl;
    cout << "电梯调度算法" << endl;

    //磁头初始向里
    cout << "磁头初始向里的访问磁道号顺序流:";

    current = start;
    for(i = 0; i < quantity; i++) {
        flag = 1000;
        p = -1;
        for(j = 0; j < quantity; j++) {
            if((queue[j].visited == 0) && (queue[j].go >= current)) {
                if(abs(queue[j].go - current) < flag) {
                    p = j;
                    flag = abs(queue[j].go - current);
                }
            }
        }
        if(p != -1) {
            cout << queue[p].go << " ";
            total += abs(queue[p].go - current);
            current = queue[p].go;
            queue[p].visited = 1;
        } else {
            for(j = 0; j < quantity; j++) {
                if((queue[j].visited == 0) && (queue[j].go < current)) {
                    if(abs(queue[j].go - current) < flag) {
                        p = j;
                        flag = abs(queue[j].go - current);
                    }
                }
            }
            cout << queue[p].go << " ";
            total += abs(queue[p].go - current);
            current = queue[p].go;
            queue[p].visited = 1;
        }
    }
    cout << endl << "磁头移过的柱面数:" << total << endl;

    //磁头初始向外
    for(i = 0; i < quantity; i++) {
        queue[i].visited = 0;
    }
    total = 0;

    cout << "磁头初始向外的访问磁道号顺序流:";

    current = start;
    for(i = 0; i < quantity; i++) {
        flag = 1000;
        p = -1;
        for(j = 0; j < quantity; j++) {
            if((queue[j].visited == 0) && (queue[j].go <= current)) {
                if(abs(queue[j].go - current) < flag) {
                    p = j;
                    flag = abs(queue[j].go - current);
                }
            }
        }
        if(p != -1) {
            cout << queue[p].go << " ";
            total += abs(queue[p].go - current);
            current = queue[p].go;
            queue[p].visited = 1;
        } else {
            for(j = 0; j < quantity; j++) {
                if((queue[j].visited == 0) && (queue[j].go > current)) {
                    if(abs(queue[j].go - current) < flag) {
                        p = j;
                        flag = abs(queue[j].go - current);
                    }
                }
            }
            cout << queue[p].go << " ";
            total += abs(queue[p].go - current);
            current = queue[p].go;
            queue[p].visited = 1;
        }
    }
    cout << endl << "磁头移过的柱面数:" << total;

}

//显示版权信息函数
void version() {
    cout << endl << endl;

    cout << " ┏━━━━━━━━━━━━━━━━━━━━━━━┓" << endl;
    cout << " ┃     磁盘调度算法系统        ┃" << endl;
    cout << " ┠───────────────────────┨" << endl;
    cout << " ┃   (c)All Right Reserved Neo       ┃" << endl;
    cout << " ┃      sony006@163.com          ┃" << endl;
    cout << " ┃     version 2004 build 1122      ┃" << endl;
    cout << " ┗━━━━━━━━━━━━━━━━━━━━━━━┛" << endl;

    cout << endl << endl;
}


void main() {
    int i;

    version();
    initial();

    readData();

    FIFO();

    shortest();

    for(i = 0; i < quantity; i++) {
        queue[i].visited = 0;
    }

    elevator();
}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...