操作系统.

1.先来先服务算法

#include<stdio.h>
#include<stdlib.h>
typedef struct process_FCFS
{
    float arrivetime;//到达时间
    float servetime;//服务时间
    float finishtime;//完成时间
    float roundtime;//周转时间
    float daiquantime;//带权周转时间
    struct process_FCFS *link;//结构体指针
} FCFS;
FCFS *p,*q,*head=NULL;
struct process_FCFS a[100];
//按到达时间进行冒泡排序

struct process_FCFS *sortarrivetime(struct process_FCFS a[],int n)
{
    int i,j;
    struct process_FCFS t;
    int flag;
    for(i=1; i<n; i++)
    {
        flag=0;
        for(j=0; j<n-i; j++)
        {
            if(a[j].arrivetime>a[j+1].arrivetime)
            {
                t=a[j];
                a[j]=a[j+1];
                a[j+1]=t;
                flag=1;//交换
            }
        }
        if(flag==0)//如果一趟排序中没发生任何交换,则排序结束
            break;
    }
    return a;
}
//先来先服务算法
void print(struct process_FCFS a[],int n)
{
    int i;
    for(i=0; i<n; i++)
    {
        printf("到达时间:%f",a[i].arrivetime);
        printf("服务时间:%f",a[i].servetime);
        printf("完成时间:%f",a[i].finishtime);
        printf("周转时间:%f",a[i].roundtime);
        printf("带权周转时间:%f",a[i].daiquantime);
        printf("\n");
    }
}
void Fcfs(struct process_FCFS a[],int n)
{
    int i;
    a[0].finishtime=a[0].arrivetime+a[0].servetime;
    a[0].roundtime=a[0].finishtime+a[0].arrivetime;
    a[0].daiquantime=a[0].roundtime/a[0].servetime;
    for(i=0; i<n; i++)
    {
        if(a[i].arrivetime<a[i-1].finishtime)
        {
            a[i].finishtime=a[i-1].finishtime+a[i].servetime;
            a[i].roundtime=a[i].finishtime-a[i].arrivetime;
            a[i].daiquantime=a[i].roundtime/a[i].servetime;
        }
        else
        {
            a[i].finishtime=a[i].arrivetime+a[i].servetime;
            a[i].roundtime=a[i].finishtime-a[i].arrivetime;
            a[i].daiquantime=a[i].roundtime/a[i].servetime;
        }
    }
    printf("先来先服务\n");
    print(a,n);
}
//主函数
int main()
{
    int n,i;
    printf("请输入有几个进程\n");
    scanf("%d",&n);
    for(i=0; i<n; i++)
    {
        printf("arrivetime");
        scanf("%f",&a[i].arrivetime);
        printf("servetime");
        scanf("%f",&a[i].servetime);
    }
    Fcfs(a,n);
}

2.短进程优先算法

#include<iostream>
#include<stdio.h>
using namespace std;

struct pcb
{
    char pno;
    int come_time;   //到达时间
    int run_time;    //服务时间
};
float fcfs(pcb pro[],int n)
{
    struct pcb temp;
    int i,j,k;       //time为当前时间
    float weight_time=0,time=0;  //记录周转时间的和
    //temp=(pcb)malloc(sizeof(pcb));
    cout<<"进程调度情况如下:"<<endl;
    cout<<"进程号  到达时间    服务时间    周转时间:"<<endl;
    //选择排序过程,按到达时间升序排列
    for(i=0; i<n-1; i++)
    {
        k=i;
        for(j=i+1; j<n; j++)
            if(pro[k].come_time>pro[j].come_time)
                k=j;
        if(k!=i)
        {
            temp=pro[i];
            pro[i]=pro[k];
            pro[k]=temp;
        }
    }
    for(i=0; i<n; i++)
    {
        time+=pro[i].run_time;
        weight_time+=(time-pro[i].come_time)/pro[i].run_time;  //(time-pro[i].come_time)/pro[i].run_time为排序后第i个进程的周转时间
        cout<<pro[i].pno<<"     "<<pro[i].come_time<<"     "<<pro[i].run_time<<"     "<<(time-pro[i].come_time)/pro[i].run_time<<endl;
    }
    return weight_time/=n;    //返回平均带权周转时间
}
void insert(pcb pro[],pcb pro1,int start,int end)//将一pcb类型的元素插入到有序数组中,最后还保持有序
{
    int i=end;
    while((i--)>start)
        if(pro[i].run_time>pro1.run_time)pro[i+1]=pro[i];
    pro[i]=pro1;
}
float sjp(pcb pro[],int n)
{
    int i,first=0,count,flag[20],k,min;
    float time=0,weight_time=0;//调度第一个到达内存的进程
    for(i=1; i<n; i++)
    {
        if(pro[first].come_time>pro[i].come_time) first=i;
        flag[i]=0;
    }
    flag[first]=1;
    time=(float)pro[first].run_time;
    weight_time=1;
    cout<<pro[first].pno<<"     "<<pro[first].come_time<<"    "<<pro[first].run_time<<"     "<<weight_time<<endl;
    //pro_temp[0]=pro[first];
    count=n-1;
    while(count)
    {
        k=0;
        min=32767;           //设置一个较大的阈值,
        for(i=0; i<n; i++)     //找到一个未被访问的,作业较短的且已经到达内存的作业调度
            if((i!=first)&&(flag[i]==0)&&(time>=pro[i].come_time)&&(min>pro[i].run_time))
            {
                k=i;
                min=pro[i].run_time;
            }
        flag[k]=1;           //访问后置标记为访问
        time+=pro[k].run_time;
        weight_time+=(time-pro[k].come_time)/pro[k].run_time;
        cout<<pro[k].pno<<"     "<<pro[k].come_time<<"    "<<pro[k].run_time<<"    "<<(time-pro[k].come_time)/pro[k].run_time<<endl;
        count--;            //每调度一个作业,count减1
    }
    return weight_time/=n;
}
int main()
{
    pcb pro[5]= {{'C',2,5},{'A',0,4},{'B',1,3},{'D',3,2},{'E',4,4}};
    cout<<fcfs(pro,5)<<endl;
    cout<<sjp(pro,5)<<endl;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

勇敢nn

心动不如行动,感谢您的支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值