C++进程调度·算法-先来先服务

本文深入探讨了先来先服务(FCFS)调度算法的工作原理,通过C++代码实现了一个简单的进程调度模拟,展示了如何计算进程的到达时间、服务时间、完成时间、周转时间和带权周转时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先来先服务调度算法(FCFS,first come first served) 
算法原理:进程按照它们请求CPU的顺序使用CPU.就像你买东西去排队,谁第一个排,谁就先被执行,在它执行的过程中,不会中断它。

#include<stdio.h>
#include<iostream>

using namespace std;

struct process{

char name;
int  arrive;//到达时间
int service;//服务时间
int finish;//完成时间
int zhouzhuan;//周转时间
int daiquan;//带权周转时间
};

struct process a[10];

void input(process a[],int n)
{
    for(int i=0;i<n;i++)
{
    cin>>a[i].name;
    cin>>a[i].arrive;
    cin>>a[i].service;
}
}

void print(process a[],int n)
{
for(int i=0;i<n;i++)
{
    cout<<" 进程:"<<a[i].name;
    cout<<" 到达时间:"<<a[i].arrive;
    cout<<" 服务时间:"<<a[i].service;
    cout<<" 周转时间:"<<a[i].zhouzhuan;
    cout<<" 带权周转时间:"<<a[i].daiquan;
    cout<<" 完成时间:"<<a[i].finish<<endl;
}

}

void paixv(process a[],int n)
{
    int flag =1;
    for(int i=0;i<n;i++)
    {
        flag=0;
        for(int j=0;j<n-i-1;j++)
        {
            if(a[j].arrive>a[j+1].arrive)
            {
                process temp = a[j];
                a[j] =a[j+1];
                a[j+1] = temp;
                flag=1;
            }
        }
        if(flag == 0)
            break;
    }
}

void fifs(process a[],int n)
{
    paixv(a,n);//排序
    a[0].finish = a[0].arrive+a[0].service;
    a[0].zhouzhuan = a[0].finish - a[0].arrive;
    a[0].daiquan = a[0].zhouzhuan/a[0].service;
    for(int i=1;i<n;i++)
    {
        if(a[i].arrive<a[i-1].finish)
        {
            a[i].finish = a[i-1].finish+a[i].service;
            a[i].zhouzhuan = a[i].finish - a[i].arrive;
            a[i].daiquan = a[i].zhouzhuan/a[i].service;
        }else
        {
             a[i].finish = a[i].arrive+a[i].service;
             a[i].zhouzhuan = a[i].finish - a[i].arrive;
             a[i].daiquan = a[i].zhouzhuan/a[i].service;
        }
    }

    print(a,n);
}


int main()
{
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    int n;
    cin>>n;
    input(a,n);
//    for(int i=0;i<n;i++)
//{
//    cout<<" 进程:"<<a[i].name;
//    cout<<" 到达时间:"<<a[i].arrive;
//    cout<<" 服务时间:"<<a[i].service;
//     cout<<endl;
//}
//paixv(a,n);
//  for(int i=0;i<n;i++)
//{
//    cout<<" 进程:"<<a[i].name;
//    cout<<" 到达时间:"<<a[i].arrive;
//    cout<<" 服务时间:"<<a[i].service;
//     cout<<endl;
//}
    fifs(a,n);
    fclose(stdin);
   // fclose(stdout);
}

 

先来先服务算法 #include "stdio.h" #include #define max 100 #define pfree 0 /*process end*/ #define running 1 /*process running status*/ #define aready 2 /*process aready status */ #define blocking 3 /*process aready blocking status*/ typedef struct node { char name; int status; int ax,bx,cx,dx; int pc; int psw; struct node *next; /*pcb define*/ }pcb; pcb *createprocess(pcb *head) { pcb *p,*q; int a,b,c,d,m,n; char ID; q=NULL; printf("input the first seven status pcb:"); scanf("\n%c%d%d%d%d%d%d",&ID,&a,&b,&c,&d,&m,&n); while(1) { p=(pcb*)malloc(sizeof(pcb)); p->name=ID; p->ax=a; p->bx=b; p->cx=c; p->dx=d; p->pc=m; p->psw=n; p->status=aready; if(head==NULL) head=p; else q->next=p; q=p; printf("input the next seven status pcb: "); scanf("\n%c",&ID); if (ID == '*') break; scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&m,&n); } if(q!=NULL) q->next=NULL; q=head; while(q) { printf("\n process name. status.ax. bx. cx. dx. pc. psw.\n "); printf("%10c%5d%5d%5d%5d%5d%5d%5d",q->name,q->status,q->ax,q->bx,q->cx,q->dx,q->pc,q->psw); q=q->next; } return head;/*createprocess end*/ } void processfcfs(pcb *head) /*use fifo */ { pcb *p; p=head; printf("\n the process use fcfs method.\n"); printf("running the frist process:\n"); while(p!=NULL) { p->status=running; printf("\nprocess name status. ax. bx. cx. dx. pc. psw."); printf("\n%10c%5d%8d%5d%5d%5d%5d%5d",p->name,p->status,p->ax,p->bx,p->cx,p->dx,p->pc,p->psw); /*check process running status */ p->status=0; p=p->next; } printf("\n检查进程是否结束:"); p=head; while(p) { printf("\n%3c%3d",p->name,p->status); p=p->next; } printf("\ngame is over!\n"); } main() { pcb *head; head=NULL; head=createprocess(head); processfcfs(head); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值