操作系统实验报告一 进程调度

操作系统实验报告一 进程调度实验一进程调度任务一一 实验名称进程调度 代码阅读并调试实验二 实验目的 1 阅读下面源代码 完善程序中填空处内容 2 阅读代码 写出调度算法 算法流程图和程序功能 3 解释数据结构 PCB 的定义和作用 4 为 main 写出每行的注释 5 调试并运行代码 写出结果 进程调度源程序如下 jingchendiao cpp include stdio h include stdlib h include conio h conio h stdlib h

实验一 进程调度

任务一

一、实验名称

 进程调度-代码阅读并调试实验 

二、实验目的

jingchendiaodu.cpp #include "stdio.h" #include <stdlib.h> #include <conio.h> #define getpch(type) (type*)malloc(sizeof(type)) #define NULL 0 struct pcb { 
    /* 定义进程控制块PCB */ char name[10]; char state; int super; int ntime; int rtime; struct pcb* link; }*ready=NULL,*p; typedef struct pcb PCB; sort() /* 建立对进程进行优先级排列函数*/ { 
    PCB *first, *second; int insert=0; if((ready==NULL)||((p->super)>(ready->super))) /*1 就绪队列为空或p的优先级大于就绪队列中第一个的优先级 */ { 
    p->link=ready; ready=p; } else /* 进程比较优先级,插入适当的位置中*/ { 
    first=ready; second=first->link; while(second!=NULL) { 
    if((p->super)>(second->super)) /*若插入进程比当前进程优先数大,*/ { 
    /*插入到当前进程前面*/ p->link=second; first->link=p; second=NULL; insert=1; } else /* 插入进程优先数最低,则插入到队尾*/ { 
    2 first=first->link ; second=second->link; } } if(insert==0) first->link=p; } } input() /* 建立进程控制块函数*/ { 
    int i,num; clrscr(); /*清屏*/ printf("\n 请输入进程号?"); scanf("%d",&num); for(i=0;i<num;i++) { 
    printf("\n 进程号No.%d:\n",i); p=getpch(PCB); printf("\n 输入进程名:"); scanf("%s",p->name); printf("\n 输入进程优先数:"); scanf("%d",&p->super); printf("\n 输入进程运行时间:"); scanf("%d",&p->ntime); printf("\n"); p->rtime=0;p->state='w'; p->link=NULL; sort(); /* 调用sort函数*/ } } int space() { 
    int l=0; PCB* pr=ready; while(pr!=NULL) { 
    l++; pr=pr->link; } return(l); } disp(PCB * pr) /*建立进程显示函数,用于显示当前进程*/ { 
    printf("\n qname \t state \t super \t ndtime \t runtime \n"); printf("|%s\t",pr->name); printf("|%c\t",pr->state); printf("|%d\t",pr->super); printf("|%d\t",pr->ntime); printf("|%d\t",pr->rtime); printf("\n"); } check() /* 建立进程查看函数 */ { 
    PCB* pr; printf("\n 当前正在运行的进程是:%s",p->name); /*显示当前运行进程*/ disp(p); pr=ready; printf("\n 当前就绪队列状态为:\n"); /*显示就绪队列状态*/ while(pr!=NULL) { 
    disp(pr); pr=pr->link; } } destroy() /*建立进程撤消函数(进程运行结束,撤消进程)*/ { 
    printf("\n 进程 [%s] 已完成.\n",p->name); free(p); } running() /* 建立进程就绪函数(进程运行时间到,置就绪状态*/ { 
    (p->rtime)++; if(p->rtime==p->ntime) destroy(); /* 调用destroy函数*/ else { 
    (p->super)--; p->state='w'; sort(); /*调用sort函数*/ } } main() /*主函数*/ { 
    int len,h=0; //定义两个int类型的变量 char ch; //定义一个字符型变量  input(); //调用进程控制块函数,录入进程信息并排序 len=space(); //用len表示进程块数 while((len!=0)&&(ready!=NULL)) //当进程没有全部运行完时 { 
    ch=getchar(); //读取按任意键继续的“任意键” h++; //执行次数加一 printf("\n The execute number:%d \n",h); //打印当前执行次数 p=ready; //p指向第一个就绪进程块 ready=p->link; //就绪指针指向p的下一个进程块 p->link=NULL; //将p的下一个进程块置为空 p->state='R'; //状态置为正在运行 check();//调用进程查看函数,打印进程调度信息  running(); //运行进程p printf("\n 按任一键继续......"); //提示用户按任意键继续 ch=getchar(); //存储“任意键”字符 } printf("\n\n 进程已经完成.\n"); //显示进程全部调度完成 ch=getchar();//存储“任意键”字符 } 

三、实验过程

在这里插入图片描述

 已在上文作业调度源程序呈现 

四、实验总结

1、先来先服务算法(FCFS)按照进程变为就绪状态的先后次序分派CPU,比较有利于长作业,而不利于短作业(因为长作业能够长时间占据处理机);有利于CPU繁忙的作业,而不利于I/O繁忙的作业。

任务二

一、实验名称

 进程调度-代码阅读并调试实验 

二、实验目的

三、实验过程

1、“最高优先数优先”调度算法

进程调度结果如下:

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

2、“轮转法”调度算法

输入进程信息如下:在这里插入图片描述

四、实验总结

五、代码

1、“最高优先数优先”调度算法
#include "stdio.h" #include <stdlib.h> #include <conio.h> #define getpch(type) (type*)malloc(sizeof(type)) #define NULL 0 #include<iostream> using namespace std; struct pcb /* 定义进程控制块PCB */ { 
    char name[10]; char state; int super; int ntime; int rtime; struct pcb* link; }*ready=NULL,*p; typedef struct pcb PCB; sort() /* 建立对进程进行优先级排列函数*/ { 
    PCB *first, *second; int insert=0; if((ready==NULL)||((p->super)>(ready->super))) /*1就绪队列为空或p的优先级大于就绪队列中第一个的优先级*/ { 
    p->link=ready; ready=p; } else /* 进程比较优先级,插入适当的位置中*/ { 
    first=ready; second=first->link; while(second!=NULL) { 
    if((p->super)>(second->super)) /*若插入进程比当前进程优先数大,*/ { 
    /*插入到当前进程前面*/ p->link=second; first->link=p; second=NULL; insert=1; } else /* 插入进程优先数最低,则插入到队尾*/ { 
    first=first->link; ; second=second->link; } } if(insert==0) first->link=p; } } input() /* 建立进程控制块函数 */ { 
    int i; for(i=0; i<5; i++) { 
    printf("\n 进程号No.%d:\n",i); p=getpch(PCB); printf("\n 输入进程名:"); scanf("%s",p->name); printf("\n 输入进程优先数:"); scanf("%d",&p->super); printf("\n 输入进程运行时间:"); scanf("%d",&p->ntime); printf("\n"); p->rtime=0; p->state='w'; p->link=NULL; sort(); /* 调用sort函数*/ } } int space() { 
    int l=0; PCB* pr=ready; while(pr!=NULL) { 
    l++; pr=pr->link; } return(l); } disp(PCB * pr) /*建立进程显示函数,用于显示当前进程*/ { 
    printf("\n qname \t state \t super \t ndtime \t runtime \n"); printf("|%s\t",pr->name); printf("|%c\t",pr->state); printf("|%d\t",pr->super); printf("|%d\t",pr->ntime); printf("|%d\t",pr->rtime); printf("\n"); } check() /* 建立进程查看函数 */ { 
    PCB* pr; printf("\n 当前正在运行的进程是:%s",p->name); /*显示当前运行进程*/ disp(p); pr=ready; printf("\n 当前就绪队列状态为:\n"); /*显示就绪队列状态*/ while(pr!=NULL) { 
    disp(pr); pr=pr->link; } } destroy() /*建立进程撤消函数(进程运行结束,撤消进程)*/ { 
    printf("\n 进程 [%s] 已完成.\n",p->name); free(p); } running() /* 建立进程就绪函数(进程运行时间到,置就绪状态*/ { 
    (p->rtime)++; if(p->rtime==p->ntime) destroy(); /* 调用destroy函数*/ else { 
    (p->super)--; p->state='w'; sort(); /*调用sort函数*/ } } main() /*主函数*/ { 
    int len,h=0; char ch; input(); len=space(); while((len!=0)&&(ready!=NULL)) { 
    ch=getchar(); h++; printf("\n The execute number:%d \n",h); p=ready; ready=p->link; p->link=NULL; p->state='R'; check(); running(); printf("\n 按任一键继续......"); ch=getchar(); } printf("\n\n 进程已经完成.\n"); ch=getchar(); } 
2、“轮转法”调度算法
#include<stdio.h> #include<malloc.h> enum process_status{ 
   READY , RUN , FINISH}; typedef struct pcb { 
    char process_tag[20] ; struct pcb *next ; int time_slice ; // 轮转时间片 int take_cpu_time ; //占用CPU时间片数 int process_time ; //进程所需时间片数 process_status status ; } PCB ; typedef struct { 
    PCB *run ; //当前运行的进程指针 PCB *ready ; //当前准备队列的头指针 PCB *tail ; //准备队列的队尾指针 PCB *finish ; //完成队列的指针 } PCBC ; void init_pcbc(PCBC *p) { 
    p->run = NULL ; p->ready = NULL ; p->tail = NULL ; p->finish = NULL ; } void input_process(PCBC *pcbc) { 
    PCB *pcb ; pcb = (PCB*)malloc(sizeof(PCB)) ; printf("请输入进程标识符:") ; scanf("%s" , &pcb->process_tag) ; printf("输入格式为: (优先级,占用CPU时间片数,进程所需时间片数) : ") ; scanf("%d%d%d" , &pcb->time_slice , &pcb->take_cpu_time , &pcb->process_time) ; pcb->status = READY ; //初始化就绪状态 //当就绪队列为空时 if(pcbc->ready == NULL && pcbc->tail == NULL) { 
    pcbc->ready = pcbc->tail = pcb ; pcb->next = NULL ; } else { 
    pcb->next = pcbc->tail->next ; pcbc->tail->next = pcb ; pcbc->tail = pcb ; } } void print_log(PCBC *pcbc) { 
    PCB *ready , *finish ; ready = pcbc->ready ; finish = pcbc->finish ; printf("-------------------------------------------------- \n") ; printf("Run: \n") ; if(pcbc->run != NULL) { 
    printf("%s %04d %04d %04d \n" , pcbc->run->process_tag ,pcbc->run->time_slice , pcbc->run->take_cpu_time , pcbc->run->process_time) ; } else { 
    printf("Run is empty! \n") ; } printf("Ready:\n") ; while(ready != NULL) { 
    printf("%s %04d %04d %04d \n" , ready->process_tag ,ready->time_slice , ready->take_cpu_time , ready->process_time) ; ready = ready->next ; } printf("Finish:\n") ; while(finish != NULL) { 
    printf("%s %04d %04d %04d \n" , finish->process_tag ,finish->time_slice , finish->take_cpu_time , finish->process_time) ; finish = finish->next ; } } void run_pcbc_timeSlice(PCBC *xpcbc) { 
    PCBC *pcbc = xpcbc ; PCB *temp , *pre , *tail ; while(pcbc->ready != NULL) { 
    pcbc->run = pcbc->ready ; pcbc->ready = pcbc->ready->next ; //改变队首元素 print_log(pcbc) ; pcbc->run->take_cpu_time += 1 ; pcbc->run->process_time -= 1 ; if(pcbc->run->process_time == 0) { 
    if(pcbc->finish == NULL) { 
    pcbc->finish = pcbc->run ; pcbc->finish->next = NULL ; tail = pcbc->finish ; } else { 
    tail->next = pcbc->run ; tail = tail->next ; tail->next = NULL ; } } else { 
    if(pcbc->run->time_slice == pcbc->run->take_cpu_time) //占用CPU时间片数到 { 
    pcbc->run->take_cpu_time = 0 ; pcbc->run->next = NULL ; pcbc->tail->next = pcbc->run ; pcbc->tail = pcbc->run ; if(pcbc->ready == NULL) { 
    pcbc->ready = pcbc->tail ; pcbc->ready->next = NULL ; } } else { 
    pcbc->run->next = pcbc->ready ; pcbc->ready = pcbc->run ; } } } pcbc->run = NULL ; print_log(pcbc) ; } int main() { 
    PCBC *pcbc ; //创建进程控制块链 ; pcbc = (PCBC*)malloc(sizeof(PCBC)) ; init_pcbc(pcbc) ; //初始化进程控制块链 for(int i = 0 ; i < 5 ; i++) { 
    input_process(pcbc) ; } printf("------------------------\n") ; run_pcbc_timeSlice(pcbc) ; return 0 ; } 
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/221529.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月17日 下午5:53
下一篇 2026年3月17日 下午5:54


相关推荐

  • Iterative Shrinkage Thresholding Algorithm

    Iterative Shrinkage Thresholding AlgorithmIterativeShrinkageThresholdingAlgorithm(ISTA)

    2022年6月11日
    44
  • Logback日志工具使用详解

    Logback日志工具使用详解由于Logback比log4j和SLF4J拥有众多优点,如性能(据说有时达到10倍以上),并且支持自动加载配置文件,自动删除旧的日志文件,以及同一个logback配置文件同时适应开发,测试,生产等。因此Logback官方强烈建议开发人员从log4j转到使用Logback。

    2022年6月17日
    35
  • 指尖玩转非遗绝活!文心一言首创“非遗智能体”

    指尖玩转非遗绝活!文心一言首创“非遗智能体”

    2026年3月12日
    3
  • android studio usb连接手机_android studio怎么用真机调试

    android studio usb连接手机_android studio怎么用真机调试    Android开发者第一步学习的应该就是真机调试了。但是很多初次接触androidstudio的同学还是不知道如何用真机调试,今天我就给大家写一个教程,希望可以帮到需要的人。   我使用的是一款国家电网定制机型。Android版本为:5.0.2。     1.先用usb线把你的测试手机连接到你的电脑上,并且安装驱动(由于机子型号不同,安装方式有差异,可以根据你的机子百度安装…

    2025年11月10日
    8
  • chmod用法介绍「建议收藏」

    chmod用法介绍「建议收藏」chmod—修改文件、目录权限Usage:chmod[OPTION]…MODE[,MODE]…FILE… or: chmod[OPTION]…OCTAL-MODEFILE… or: chmod[OPTION]…–reference=RFILEFILE…ChangethemodeofeachFILEtoMODE….

    2022年10月20日
    5
  • xshell安装步骤_Xshell怎么使用

    xshell安装步骤_Xshell怎么使用XShell可以在Windows界面下来访问远端不同系统下的服务器,从而比较好的达到远程控制终端的目的。它支持RLOGIN、SFTP、SERIAL、TELNET、SSH2和SSH1,可以非常方便的对Linux主机进行远程管理。除此之外,其还有丰富的外观配色方案以及样式选择。Xshell免费版官网下载地址https//www.xshell.com/zh/free-for-home-school/链接https//pan.baidu.com/s/1NJGWZHkByakOkQpKfkc7Yg。…

    2025年9月5日
    56

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号