C语言的磁盘调度算法

发布于 2024-11-26 10:23:42 字数 177 浏览 4 评论 0原文

我正在尝试用 C 语言学习磁盘调度算法(SCAN 和 C-SCAN)的实现,有人可以参考这些用 C 语言实现的良好来源吗?或者建议我用 C 语言对其进行编程?

进一步深入:- *目标是编写一个程序来优化磁盘访问,将磁盘上的一组不连续的页面读取到内存,为此,我正在执行磁盘调度。

*我想指示磁盘读取页面的顺序

I am trying to learn implementation of Disk Scheduling Algorithms (SCAN and C-SCAN) in the C language, can somebody please refer to good sources of implementations of these in the C Language or advise me on programming them on C?

Further Into:-
*Objective is to write a program to optimize disk access to read a non contiguous set of pages on disk to memory, for this, I am performing disk scheduling.

*I would want to instruct the disk on read sequence of the pages

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

饮湿 2024-12-03 10:23:42

以有效方式调度磁盘请求的逻辑应该在磁盘本身的域中考虑!优化从磁盘扇区读取数据的顺序不太可能是可移植的或高效的。

但是,如果您有一个 npages * PAGE_SIZE 的文件,则在为您自己的磁盘内部读取提供服务时,您可以尝试提高应用程序性能。给定:

#define PAGE_SIZE ...
#define MAX_PAGE_READ ...

struct read_req {
    unsigned int page;
    /* any other book-keeping required */
};

您可以在内部按 page 对请求进行排序(并且可以选择合并相邻页面):

/* qsort-comparer for two read_req structs */
int cmp_req(const void *a, const void *b)
{
    unsigned int pageA = ((struct read_req*)a)->page,
                 pageB = ((struct read_req*)b)->page;
    return pageA == pageB ? 0 : pageA > pageB ? 1 : -1;
}

int service_reads(struct read_req *reqs, size_t nreqs)
{
    size_t ii = 0;

    /* sort read requests in ascending order */
    qsort(reqs, nreqs, sizeof(reqs[0]), cmp_req);

    while (ii < nreqs)
    {
        unsigned int start = reqs[ii].page;
        size_t size = 1;

        while (++ii < nreqs)
        {
            if (reqs[ii].page != (start + size)) break;

            /* expand our read to include the next page,
             * and break if we've read too much
             */
            if (++size == MAX_PAGE_READ) break;
        }

        your_read_logic(start, size);
    }
}

The logic for scheduling disk requests in an efficient fashion should be considered in the domain of the disk itself! Optimizing the order you read data from disk sectors is not likely to be portable or efficient.

However, if you have a file of npages * PAGE_SIZE, you could attempt to improve your applications performance when servicing your own internal reads from disk. Given:

#define PAGE_SIZE ...
#define MAX_PAGE_READ ...

struct read_req {
    unsigned int page;
    /* any other book-keeping required */
};

You could internally sort the requests by page (and optionally coalesce neighbor pages):

/* qsort-comparer for two read_req structs */
int cmp_req(const void *a, const void *b)
{
    unsigned int pageA = ((struct read_req*)a)->page,
                 pageB = ((struct read_req*)b)->page;
    return pageA == pageB ? 0 : pageA > pageB ? 1 : -1;
}

int service_reads(struct read_req *reqs, size_t nreqs)
{
    size_t ii = 0;

    /* sort read requests in ascending order */
    qsort(reqs, nreqs, sizeof(reqs[0]), cmp_req);

    while (ii < nreqs)
    {
        unsigned int start = reqs[ii].page;
        size_t size = 1;

        while (++ii < nreqs)
        {
            if (reqs[ii].page != (start + size)) break;

            /* expand our read to include the next page,
             * and break if we've read too much
             */
            if (++size == MAX_PAGE_READ) break;
        }

        your_read_logic(start, size);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文