战点 2024-05-10 10:31 采纳率: 17.4%
浏览 4

补全insert和rr()函数

实现 insert()和rr()函数,前者函数只需要简单的将新创建的进程插入就绪队列,而后者需要你在每次执行running()前将需要执行的正确的进程放在队首,时间片用完的进程放在队尾。现在请根据轮转调度算法的原理补全insert和rr()函数。
//创建就绪队列


void insert()
 {
     PCB *q=NOTHING;
    if(!ready) {
         //待补全
         ready=p;
         ready->state = 'R';
         ready->oncerun=1;
         pfend=p;
         pfend->link = NOTHING;
    }else{
        q=ready; 
        while(q->link){
            q=q->link;
        }
        q->link=p;
        p->state = 'w';
        pfend = p;
        pfend->link = NOTHING;
    }
}
void rr()//调整进程队列
{
    PCB *q;
    if(rrtime==0){
        printf("时间片长度设置:");
        rrtime = geti(); 
    }else{
        if(ready->link)//就绪队列中不只一个进程 
            if(ready->rtime!=0 && ready->rtime%rrtime==0 && ready->rtime!=ready->ntime){//重新进入就绪队列 
                
       }
       if(ready->rtime==ready->ntime && ready->link){ //时间片已经执行结束 
               //待补全 
               
       }
    }
}
  • 写回答

4条回答 默认 最新

  • 笑面虎 2024-05-10 10:31
    关注

    以下内容由CHATGPT及笑面虎共同生成、有用望采纳:


    【您想要解决的问题】:您提问的目的是要求补全C语言中的两个函数 insert()rr(),这两个函数是用于实现轮转调度算法中的进程管理。insert() 函数用于将新创建的进程插入到就绪队列中,而 rr() 函数用于在每次执行前调整进程队列,确保时间片用完的进程被放在队列的末尾,而需要执行的进程放在队首。

    【问题出现原因】:问题出现的原因是您提供的代码片段中有几个关键部分没有完成,这些部分涉及到对链表的操作以及对进程状态的管理。在轮转调度算法中,正确地管理进程的时间片和它们在就绪队列中的位置是至关重要的。

    【问题解决方案】:为了解决这个问题,我们需要补全 insert()rr() 函数中的缺失部分。以下是补全后的代码示例:

    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct PCB {
        char state; // 'R' for ready, 'W' for waiting
        int oncerun;
        int rtime; // remaining time for the current time slice
        int ntime; // total time needed for the process to run to completion
        struct PCB *link;
    } PCB;
    
    PCB *p, *ready, *pfend = NULL;
    
    void insert(PCB *newProcess) {
        PCB *q = NULL;
        p = newProcess;
        if (!ready) {
            // If the ready queue is empty, add the process to the queue
            ready = p;
            ready->state = 'R';
            ready->oncerun = 1;
            pfend = p;
            pfend->link = NULL;
        } else {
            // If the ready queue is not empty, add the process to the end of the queue
            q = ready;
            while (q->link) {
                q = q->link;
            }
            q->link = p;
            p->state = 'W';
            pfend = p;
            pfend->link = NULL;
        }
    }
    
    void rr() {
        PCB *q;
        if (rrtime == 0) {
            printf("Time slice length setting: ");
            rrtime = geti(); // Assume geti() is a function that gets an integer input
        } else {
            if (ready->link) { // There is more than one process in the ready queue
                q = ready;
                while (q) {
                    if (q->rtime % rrtime == 0 && q->rtime != q->ntime) {
                        // If the remaining time is a multiple of the time slice, move the process to the end
                        if (q->link) {
                            PCB *temp = q->link;
                            q->link = temp->link;
                            if (q->link) {
                                q->link->link = NULL;
                            }
                            temp->link = ready;
                            ready = temp;
                        } else {
                            // This is the last process in the queue
                            pfend->link = q;
                            pfend = q;
                            q->link = NULL;
                        }
                    }
                    q = q->link;
                }
            }
            if (ready->rtime == ready->ntime && ready->link) { // The time slice has been used up
                // Move the process to the end of the ready queue
                PCB *temp = ready;
                ready = ready->link;
                pfend->link = temp;
                temp->link = NULL;
                pfend = temp;
                temp->state = 'W'; // Set the state to waiting
            }
        }
    }
    
    // Assume geti() is defined elsewhere in the code
    int geti() {
        int input;
        scanf("%d", &input);
        return input;
    }
    
    int main() {
        // Example usage of insert() and rr()
        // ...
        return 0;
    }
    

    【提供代码】:上面的代码提供了补全后的 insert()rr() 函数。请注意,这里假设 geti() 是一个已经定义好的函数,用于获取用户输入的整数。

    【代码运行方式】:这段代码需要在一个支持C语言的编译环境中运行,比如GCC。您可以将代码复制到一个文本文件中,保存为 .c 扩展名,然后使用编译器编译并运行。

    【代码预期运行结果】:如果代码正确地插入了进程,并且 rr() 函数能够正确地管理进程的时间片和它们在就绪队列中的位置,那么轮转调度算法应该能够按照预期工作。

    【推荐相关链接】:

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 5月10日

悬赏问题

  • ¥15 CCF-CSP 2023 第三题 解压缩(50%)
  • ¥30 comfyui openpose报错
  • ¥20 Wpf Datarid单元格闪烁效果的实现
  • ¥15 图像分割、图像边缘提取
  • ¥15 sqlserver执行存储过程报错
  • ¥100 nuxt、uniapp、ruoyi-vue 相关发布问题
  • ¥15 浮窗和全屏应用同时存在,全屏应用输入法无法弹出
  • ¥100 matlab2009 32位一直初始化
  • ¥15 Expected type 'str | PathLike[str]…… bytes' instead
  • ¥15 三极管电路求解,已知电阻电压和三级关放大倍数