操作系统作业调度算法C代码实现

简介: 操作系统作业调度算法C代码实现

前言


去年学的操作系统,现在有学妹问调度算法实现发现自己没有复习竟然忘了很多,借此机会把原来的知识捡回来。本文是原来通过C实现作业调度算法的集合


一、进程入队与出队模拟

20210304175250582.png

入队代码:

#include <malloc.h>
    #include <stdio.h>
    #include <string.h>
    #define NULL 0
    typedef struct processpcb
    {
        int id;/*进程控制块编号*/
        struct processpcb *next;
    }node;
    int n;
    node *creat(void)   /*建立进程控制块队列表*/
    {
        node *head,*p1,*p2;
        n=0;
        printf("Input processpcb table:ID\n");
        p1=p2=(node *)malloc(sizeof(node));
        scanf("%d",&p1->id);
        head=NULL;
        while (p1->id>0)
        {
            n=n+1;
            if (n==1) head=p1;
            else p2->next=p1;
        p2=p1;
        p1=(node *) malloc (sizeof(node));
        scanf("%d",&p1->id);
        }
        p2->next=NULL;
        return(head);
    }
    node *append(node *head,node *q)  /*增加一个进程进入队列*/
    {
        node *p=head;
        if(!p)
            return q;
        else
        {
        while(p->next)
           p=p->next;
        p->next=q;
        q->next=NULL;
        return head;
        }
    }
    void print (node *head)  /*输出链表*/
    {
        node *p;
        p=head;
        if(!p)   printf("链表为空!");
        else
        {
            printf("元素为:");
            while(p)
            {
                printf("%5d",p->id);
                p=p->next;
            }
        }
    }
    int main()
    {
        node *p,*q;
        int pcbid;
        p=creat();
        printf("\nappend a processpcb\n");
        scanf("%d",&pcbid);
        q=(node *)malloc(sizeof(node));
        q->id=pcbid;
        q->next=NULL;
        printf("\ninit_processpcb queue is:\n");
        print(p);
        p=append(p,q);
        printf("\nappend_processpcb queue is:\n");
        print(p);
    }


出队代码:

#include <malloc.h>
    #include <stdio.h>
    #include <string.h>
    #define NULL 0
    typedef struct processpcb
    {
        int id;/*进程控制块编号*/
        struct processpcb *next;
    }node;
    int n;
    node *creat(void)   /*建立进程控制块队列表*/
    {
        node *head,*p1,*p2;
        n=0;
        printf("Input processpcb table:ID\n");
        p1=p2=(node *)malloc(sizeof(node));
        scanf("%d",&p1->id);
        head=NULL;
        while (p1->id>0)
        {
            n=n+1;
            if (n==1) head=p1;
            else p2->next=p1;
            p2=p1;
            p1=(node *) malloc (sizeof(node));
            scanf("%d",&p1->id);
        }
        p2->next=NULL;
        return(head);
    }
    node *find(node *head,int x)
    {
        node *p=head->next;
        while(p&&p->id!=x)
            p=p->next;
        if(!p)
            return 0;
        else
            return p;
    }
    node *del(node *head,int pcb)
    {
        node *p=head,*q;
        q=p->next;
        if(p->id==pcb)
            return q;
        while(q&&q->id!=pcb)
        {
            p=q;
            q=q->next;
        }
        if(p->id=pcb)
        p->next=q->next;
        return head;
    }
    void print (node *head)  /*输出链表*/
    {
        node *p;
        p=head;
        if(!p)   printf("链表为空!");
        else
        {
            printf("元素为:");
            while(p)
            {
                printf("%5d",p->id);
                p=p->next;
            }
        }
    }
    int main()
    {
        node *p,*q;
        int pcbid;
        p=creat();
        printf("\nappend a processpcb\n");
        scanf("%d",&pcbid);
        q=(node *)malloc(sizeof(node));
        q->id=pcbid;
        q->next=NULL;
        printf("\ninit_processpcb queue is:\n");
        print(p);
        p=del(p,pcbid);
        printf("\nappend_processpcb queue is:\n");
        print(p);
    }


二、FCFS调度算法

20210304180008807.png


代码:

#include <malloc.h>
#include <stdio.h>
#include <string.h>
typedef struct table
{ 
  int key;        /*进程ID号*/
  int sequence;     /*进程进入队列顺序号*/
  char message[10];   /*进程说明信息*/
  struct table *next;
}node;
/*
  定义函数,建立进程链表
*/
node *creat(void) 
{
  node *head;
  node *p1, *p2;
  int n = 0;
  p1 = p2 =(node *)malloc(sizeof(node));
  scanf("%d%d", &p1->key, &p1->sequence);
  gets(p1->message);
  head = NULL;
  while (p1->key != 0) {  //输入0表示结束
    ++n;
    if (n == 1)
      head = p1;
    else 
      p2->next = p1;
    p2 = p1;
    p1 = (node *)malloc(sizeof(node));
    scanf("%d%d", &p1->key, &p1->sequence);
    gets(p1->message);
  }
  p2->next = NULL;
  return head;
}
/*
  模拟当前就绪进程队列中最先进入进程出队并输出的调用过程
*/
node *fcfs(node *head)  
{
  node *p, *q;
  p = head;
  printf("key=%d,sequence=%d,message=%s\n",p->key,p->sequence,p->message);
  q = p;
  p = p->next;
  free(q);
  return p;
}
void print(node *head)  //输出链表
{
  node *p;
  printf("\n The table is : \n");
  p = head;
  while (p) {
    printf("%d, %d, %s\n", p->key, p->sequence, p->message);
    p = p->next;
  }
}
int main(void)
{
  int count = 0;
  node *p, *q;
  printf("新建的进程控制表为:\nkey sequence message\n");
  p = creat();    //输入进程控制表
  print(p);
  while (p) {
    ++count;
    printf("\n第%d次被调度的就绪进程为:\n",count);
    q = fcfs(p);
    p = q;
  }
  return 0;
}

3.优先级调度算法

20210304192212954.png


20210304194718430.png


这个好像没有找到,回头再补吧。


4.时间片轮转调度算法

20210304194802814.png


#include <malloc.h>
#include <stdio.h>
#include <string.h>
#define NULL 0
typedef struct table
 {
   int key;               /*进程ID号*/
   int run_time;         /*进程运行时间*/
   char message[10];     /*进程说明信息*/
   struct table *next;
 }node;
node *creat(void)      /*定义函数,输入ID号和顺序号,按照输入次序建立进程链表*/
{
   node *head,*p1,*p2;
   int n=0;
   p1=p2=(node *)malloc(sizeof(node));
   scanf("%d %d %s",&p1->key,&p1->run_time,&p1->message);
   head=NULL;
   while (p1->run_time>0)
    {
     n=n+1;
         if (n==1) head=p1;
     else p2->next=p1;
     p2=p1;
     p1=(node *) malloc (sizeof(node));
   scanf("%d %d %s",&p1->key,&p1->run_time,&p1->message);
    }
   p2->next=NULL;
   return(head);
}
void print (node *head)  /*输出链表*/
  {
    node *p;
    p=head;
   if(!p)
   {
       printf("该链表为空!");
   }
   else
   {
       while(p)
       {
           printf("%d , ",p->key);
           printf("%d , ",p->run_time);
           printf("%s",p->message);
           p=p->next;
           printf("\n");
       }
   }
 }
node *insert(node *head,node *news)   /*将进程news插入到队列尾部*/
  {
    node *t;
    t=head;
    while(t->next)
    {
        t=t->next;
    }
    t->next=news;
    news->next=NULL;
     return head;
 }
node *timeslice(node *head,int cpu_base_time)
          /*模拟时间片轮转调度过程:队列首进程使用一个时间片的CPU*/
{
    node *r,*q;
    r=head;
    q=(node *)malloc(sizeof(node));
    if(r->run_time>cpu_base_time)
    {
       q->run_time=r->run_time-cpu_base_time;
       q->key=r->key;
       strcpy(q->message,r->message);
       insert(r,q);
    }
    return head;
}
void main()
  {
       int count=0,cpu_base_time;
       node *p;
       printf("新建的进程控制表为:\nkey run_time message\n");
       p=creat();     /*输入进程控制表*/
       printf("所建的进程控制表为:\n");
       print(p);      /*输出原始进程控制表*/
       printf("CPU运行的单位时间片cpu_base_time为:\n");
       scanf("%d",&cpu_base_time);
       while(p)      /*模拟按单位时间片进程逐个被调度并进入CPU运行的过程*/
       {  
           p=timeslice(p,cpu_base_time);
           printf("第%d次被调度的就绪进程:\n",count+1);
           printf("key=%d,run_time=%d,message=%s\n",p->key,p->run_time,p->message);
           printf("recent table is:\n");
           print(p->next);
           printf("\n");
           count++;
           p=p->next;
       }
   printf("distribute is over!\n");
  }


目录
相关文章
|
2天前
|
负载均衡 算法 调度
深入理解操作系统:进程管理与调度策略
在现代操作系统中,进程管理是核心功能之一,它关系到系统资源的合理分配、任务的有效执行以及用户体验的流畅度。本文将探讨操作系统中进程的概念、进程的状态转换、调度算法及其对系统性能的影响。通过分析不同的进程调度策略,我们旨在揭示它们如何适应多样化的使用场景并平衡效率与公平性。文章还将讨论实时系统中的调度问题以及多核处理器环境下的进程管理挑战。
|
2天前
|
数据采集 人工智能 自然语言处理
综述170篇自监督学习推荐算法,港大发布SSL4Rec:代码、资料库全面开源!
【5月更文挑战第20天】港大团队发布SSL4Rec,一个全面开源的自监督学习推荐算法框架,基于170篇相关文献的深入分析。SSL4Rec利用未标记数据提升推荐系统性能,解决了传统方法依赖大量标记数据的问题。开源代码与资料库促进研究复现与交流,为推荐系统领域带来新思路和工具。尽管面临数据需求大和依赖数据质量的挑战,但SSL4Rec展现出巨大的发展潜力和跨领域应用前景。[链接:https://arxiv.org/abs/2404.03354]
16 1
|
2天前
|
算法 Linux 调度
深入理解操作系统:进程管理与调度策略
【5月更文挑战第21天】 在现代计算机系统中,操作系统的核心职能之一是确保系统资源的有效分配与利用。其中,进程管理作为操作系统的重要组成部分,负责协调和管理运行中的程序。本文将深入探讨进程的概念、状态转换以及进程调度的关键策略,并分析它们如何影响系统性能和用户体验。通过比较不同的进程调度算法,本文旨在提供一个全面的视角,帮助读者更好地理解操作系统内部的运作机制。
|
3天前
|
存储 算法 搜索推荐
数据结构与算法⑰(第五章_八大排序)(完整代码+动图+详解+对比)(下)
数据结构与算法⑰(第五章_八大排序)(完整代码+动图+详解+对比)
21 1
|
3天前
|
算法 编译器
数据结构与算法⑰(第五章_八大排序)(完整代码+动图+详解+对比)(中)
数据结构与算法⑰(第五章_八大排序)(完整代码+动图+详解+对比)
20 4
|
3天前
|
存储 算法 搜索推荐
数据结构与算法⑰(第五章_八大排序)(完整代码+动图+详解+对比)(上)
数据结构与算法⑰(第五章_八大排序)(完整代码+动图+详解+对比)
22 6
|
3天前
|
机器学习/深度学习 算法 分布式数据库
数据结构与算法⑭(第四章_下)二叉树的模拟实现和遍历代码(下)
数据结构与算法⑭(第四章_下)二叉树的模拟实现和遍历代码
10 1
|
3天前
|
算法
数据结构与算法⑭(第四章_下)二叉树的模拟实现和遍历代码(上)
数据结构与算法⑭(第四章_下)二叉树的模拟实现和遍历代码
11 1
|
8天前
|
算法 数据安全/隐私保护 计算机视觉
基于二维CS-SCHT变换和LABS方法的水印嵌入和提取算法matlab仿真
该内容包括一个算法的运行展示和详细步骤,使用了MATLAB2022a。算法涉及水印嵌入和提取,利用LAB色彩空间可能用于隐藏水印。水印通过二维CS-SCHT变换、低频系数处理和特定解码策略来提取。代码段展示了水印置乱、图像处理(如噪声、旋转、剪切等攻击)以及水印的逆置乱和提取过程。最后,计算并保存了比特率,用于评估水印的稳健性。
|
1天前
|
机器学习/深度学习 算法
基于BP神经网络的QPSK解调算法matlab性能仿真
该文介绍了使用MATLAB2022a实现的QPSK信号BP神经网络解调算法。QPSK调制信号在复杂信道环境下受到干扰,BP网络能适应性地补偿失真,降低误码率。核心程序涉及数据分割、网络训练及性能评估,最终通过星座图和误码率曲线展示结果。