前言
去年學的作業系統,現在有學妹問調度演算法實作發現自己沒有復習竟然忘了很多,借此機會把原來的知識撿回來,本文是原來通過C實作作業調度演算法的集合,
一、行程入隊與出隊模擬

入隊代碼:
#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調度演算法

代碼:
#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.優先級調度演算法

這個好像沒有找到,回頭再補吧,
4.時間片輪轉調度演算法

#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");
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/266415.html
標籤:其他
上一篇:7年老Android收到阿里offer,跟領導提離職被懟:為年薪百萬不做兄弟?
下一篇:vue中的權限控制
