操作系统 专题九 基础算法(BA)代码

74 篇文章 12 订阅

前言

  • 最终解释权归 彡倾灬染| 所属
  • 使用时注意检查结果
  • 如果有错误请及时反馈(Thanks)
  • 可以计算的题型仅限于我刷到过的,要是有新题型请联系我添加(蟹蟹)
  • 笔芯❤

代码

/*
*备注:
 *最终解释权归 彡倾灬染| 所属
 *使用时注意检查结果
 *如果有错误请及时反馈(Thanks)
 *可以计算的题型仅限于我刷到过的,要是有新题型请联系我添加(蟹蟹)
 *笔芯❤
 */

#include <iostream>
#include <algorithm>
#include<bits/stdc++.h>
#define ll long long
#define mem(a,b) memset(a,b,sizeof a)
#define ull unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define x first
#define y second
const double eps = 1e-6;
const int mod = 998244353;
const int MOD = 1e9 + 7;
const int N = 2e5 + 10;
const int M = 111;
int dx[]={-1, 0, 1, 0};
int dy[]={0, 1, 0, -1};
using namespace std;

void solve(){
    cout<<"BA算法目录"<<endl;
    cout << "BA-1.请求资源(死锁)" << endl;
    cout << "BA-2. 分区分配(BF/WF)" << endl;
    cout << "BA-3.分页地址变换" << endl;
    cout << "BA-4.有效访问时间" << endl;
    cout << "BA-5.二级页表" << endl;
    cout << "BA-6.磁盘传输速率" << endl;
    cout << "BA-7.中断频率和响应时间计算" << endl;
    cout << "BA-8.缓冲区结构" << endl;
    cout << "BA-9.位示图" << endl;
    cout << "请输入题目编号:" << endl;
    int id;
    cin>>id;
    if(id==1){
        cout<<"假设:"<<endl;
        cout << "A—–系统中并发进程的数目"<<endl;
        cout << "B—–每个进程运行所需要的资源数目;"<<endl;
        cout << "C—–多个进程并发执行共同竞争的该类资源总数"<<endl;
        cout<<"请输入问题编号:"<<endl;
        cout << "1、已知A、B,问可能发生死锁的最大C?" << endl;
        cout << "2、已知A、B,问一定不发生死锁的最小C?" << endl;
        cout << "3、已知A、C,问可能发生死锁的最小B? " << endl;
        cout << "4、已知A、C,问一定不发生死锁的最大B?" << endl;
        cout << "5、已知B、C,问有可能发生死锁的最小A?" << endl;
        cout << "6、已知B、C,问一定不会死锁的最大A?" << endl;
        int ope;
        cin>>ope;
        if(ope==1){
            cout<<"请输入A、B"<<endl;
            int a,b;
            cin>>a>>b;
            cout<<"C="<<a*(b-1)<<endl;
        }
        if (ope ==2) {
            cout << "请输入A、B" << endl;
            int a, b;
            cin >> a >> b;
            cout << "C=" << a * (b - 1)+1 << endl;
        }
        if (ope ==3) {
            cout << "请输入A、C" << endl;
            int a, c;
            cin >> a >> c;
            cout << "B=" << ceil(c*1.0/a)+1 << endl;
        }
        if (ope ==4) {
            cout << "请输入A、C" << endl;
            int a, c;
            cin >> a >> c;
            cout << "B=" << ceil(c * 1.0 / a) << endl;
        }
        if (ope ==5) {
            cout << "请输入B、C" << endl;
            int b, c;
            cin >> b >> c;
            cout << "A=" << ceil(c * 1.0 / (b-1)) << endl;
        }
        if (ope ==6) {
            cout << "请输入B、C" << endl;
            int b, c;
            cin >> b >> c;
            cout << "A=" << ceil(c * 1.0 / b) << endl;
        }
    }
    if (id == 2) {
        cout<<"说明:本代码仅实现如下格式:\"分配n,分配m,释放n,分配a,分配b\"格式"<<endl;
        cout<<"请选择分配方法:"<<endl;
        cout<<"1-BF"<<endl;
        cout<<"2-WF"<<endl;
        cout<<"请输入序号:"<<endl;
        int ope;
        cin>>ope;
        if(ope==1){
            int sum;
            cout<<"请输入初始空间:"<<endl;
            cin>>sum;
            cout << "请输入n,m,a,b" << endl;
            int n,m,a,b;
            cin>>n>>m>>a>>b;
            sum=sum-n-m;
            int maxn=max(sum,n);
            int minn=min(sum,n);
            if(minn>=a) minn-=a;
            else maxn-=a;
            maxn=max(maxn,minn);
            minn=min(maxn,minn);
            if(minn>=b) minn-=b;
            else maxn-=b;
            maxn=max(maxn,minn);
            //minn=min(maxn,minn);
            cout << "最大空闲分区=" <<maxn<< endl;
        }
        if(ope==2){
            int sum;
            cout<<"请输入初始空间:"<<endl;
            cin>>sum;
            cout << "请输入n,m,a,b" << endl;
            int n,m,a,b;
            cin>>n>>m>>a>>b;
            sum=sum-n-m;
            int maxn=max(sum,n);
            int minn=min(sum,n);
            maxn-=a;
            maxn=max(maxn,minn);
            minn=min(maxn,minn);
            maxn-=b;
            maxn=max(maxn,minn);
            //minn=min(maxn,minn);
            cout << "最大空闲分区=" <<maxn<< endl;
        }
    }
    if (id == 3) {
        cout<<"假设:"<<endl;
        cout << "A-逻辑地址" << endl;
        cout << "B-页号" << endl;
        cout << "C-页面大小" << endl;
        cout << "D-页内地址" << endl;
        cout << "注意:页面大小是否以k为单位!" << endl;
        cout << "问题目录:" << endl;
        cout << "1-求逻辑地址" << endl;
        cout << "2-求页号" << endl;
        cout << "3-求页面大小" << endl;
        cout << "4-求页内地址" << endl;
        cout << "请输入问题编号:" << endl;
        int ope;
        cin>>ope;
        if(ope==1){
            cout << "请按顺序输入页面大小(KB)、页号、页内地址:" << endl;
            int c,b,d;
            cin>>c>>b>>d;
            cout<<"逻辑地址="<<c*1024*b+d<<endl;
        }
        if(ope==2){
            cout << "请按顺序输入逻辑地址(B)、页面大小(KB)、页内地址:" << endl;
            int a,c,d;
            cin>>a>>c>>d;
            cout<<"页号="<<(a-d)/(c*1024)<<endl;
        }
        if(ope==3){
            cout << "请按顺序输入逻辑地址(B)、页号、页内地址:" << endl;
            int a,b,d;
            cin>>a>>b>>d;
            cout<<"页面大小="<<(a-d)/(1024*b)<<"KB"<<endl;
        }
        if(ope==4){
            cout << "请按顺序输入逻辑地址(B)、页面大小(KB)" << endl;
            int a,c;
            cin>>a>>c;
            cout<<"页内地址="<<a%(c*1024)<<"B"<<endl;
        }
    }
    if (id == 4) {
        cout << "EAT - 一次存取有效访问时间"<<endl;
        cout << "t - 一次内存访问时间"<<endl;;
        cout << "b - 快表访问用时"<<endl;
        cout << "a - 快表命中率"<<endl;
        cout<<"问题列表:"<<endl;
        cout << "1 - 求一次存取有效访问时间" << endl;
        cout << "2 - 求一次内存访问时间" << endl;
        cout << "3 - 求快表访问用时" << endl;
        cout << "4 - 求快表命中率" << endl;
        cout << "请输入问题编号:" << endl;
        int ope;
        cin>>ope;
        if(ope==1){
            cout<<"请按顺序输入t、b、a(百分号前整数):"<<endl;
            double t,b,a;
            cin>>t>>b>>a;
            double eat=(2-a/100)*t+b;
            cout<<"EAT="<<fixed<<setprecision(2)<<eat<<endl;
        }
        if(ope==2){
            cout<<"请按顺序输入EAT、b、a(百分号前整数):"<<endl;
            double eat,b,a;
            cin>>eat>>b>>a;
            double t=(eat-b)/(2-a/100);
            cout<<"t="<<fixed<<setprecision(2)<<t<<endl;
        }
        if(ope==3){
            cout<<"请按顺序输入EAT、t、a(百分号前整数):"<<endl;
            double eat,t,a;
            cin>>eat>>t>>a;
            double b=eat-(2-a/100)*t;
            cout<<"b="<<fixed<<setprecision(2)<<b<<endl;
        }
        if(ope==4){
            cout<<"请按顺序输入EAT、t、b:"<<endl;
            double eat,t,b;
            cin>>eat>>t>>b;
            double a=2-(eat-b)/t;
            cout<<"a="<<fixed<<setprecision(2)<<a*100<<"%"<<endl;
        }
    }
    if (id == 5) {
        cout << "A - 页面大小" << endl;
        cout << "B - 页表项大小"<<endl;
        cout << "C - 一级页表项个数"<<endl;
        cout << "D - 逻辑地址空间大小(页数)"<<endl;
        cout << "问题列表:" << endl;
        cout << "1 - 求页面大小" << endl;
        cout << "2 - 求页表项大小" << endl;
        cout << "3 - 求一级页表项个数" << endl;
        cout << "4 - 求逻辑地址空间大小(页数)" << endl;
        cout << "请输入问题编号:" << endl;
        int ope;
        cin >> ope;
        if(ope==1){
            cout<<"请按顺序输入B、C、D(K页):"<<endl;
            int b,c,d;
            cin>>b>>c>>d;
            int a=d*b/c;
            cout<<"A="<<a<<"KB"<<endl;
        }
        if(ope==2){
            cout<<"请按顺序输入A(KB)、C、D(K页):"<<endl;
            int a,c,d;
            cin>>a>>c>>d;
            int b=a*c/d;
            cout<<"B="<<b<<"Byte"<<endl;
        }
        if(ope==3){
            cout<<"请按顺序输入A(KB)、B、D(K页):"<<endl;
            int a,b,d;
            cin>>a>>b>>d;
            int c=d*b/a;
            cout<<"C="<<c<<endl;
        }
        if(ope==4){
            cout<<"请按顺序输入A(KB)、B、C:"<<endl;
            int a,b,c;
            cin>>a>>b>>c;
            int d=a*c/b;
            cout<<"D="<<d<<"K页"<<endl;
        }
    }
    if (id == 6) {
        cout << "n - 磁盘容量(G)" << endl;
        cout << "m - 磁道数" << endl;
        cout << "b - 磁盘传输速率(Mbps)" << endl;
        cout << "r - 磁盘转速(rpm)" << endl;
        cout << "问题目录:" << endl;
        cout << "1 - 求磁盘容量(G)" << endl;
        cout << "2 - 磁道数" << endl;
        cout << "3 - 磁盘传输速率(Mbps)" << endl;
        cout << "4 - 磁盘转速(rpm)" << endl;
        cout << "请输入问题序号:" << endl;
        int ope;
        cin>>ope;
        if(ope==1){
            cout<<"请按照顺序输入m、b(Mbps)、r(rpm)"<<endl;
            double m,b,r;
            cin>>m>>b>>r;
            double n=(60*m*b)/(1024*r);
            cout<<"磁盘容量="<<fixed<<setprecision(0)<<n<<"GB"<<endl;
        }
        if(ope==2){
            cout<<"请按照顺序输入n、b(Mbps)、r(rpm)"<<endl;
            double n,b,r;
            cin>>n>>b>>r;
            double m=(n*1024*r)/(60*b);
            cout<<"磁道数="<<fixed<<setprecision(0)<<m<<endl;
        }
        if(ope==3){
            cout<<"请按照顺序输入n、m、r(rpm)"<<endl;
            double n,m,r;
            cin>>n>>m>>r;
            double b=(n*1024*r)/(60*m);
            cout << "磁盘传输速率=" << fixed << setprecision(0) << b << "MBps" << endl;
        }
        if(ope==4){
            cout<<"请按照顺序输入n、m、b(Mbps)"<<endl;
            double n,m,b;
            cin>>n>>m>>b;
            double r=(60*m*b)/(1024*n);
            cout << "磁盘转速=" << fixed << setprecision(0) << r << "rpm" << endl;
        }
    }
    if (id == 7) {
        cout<<"速率:A kb/s"<<endl;
        cout<<"m位缓冲"<<endl;
        cout<<"问题目录:"<<endl;
        cout<<"1 - 求CPU中断频率、CPU响应时间" <<endl;
        cout<<"2 - 已知CPU中断频率,求多少位"<<endl;
        cout<<"请输入序号:"<<endl;
        int pos;
        cin>>pos;
        if(pos==1){
            cout<<"请选择单、双缓冲:"<<endl;
            cout<<"1 - 单缓冲"<<endl;
            cout<<"2 - 双缓冲"<<endl;
            cout<<"请输入编号:"<<endl;
            int ope;
            cin>>ope;
            if(ope==1){
                cout<<"请按照顺序输入速率、缓冲位数:"<<endl;
                double a,m;
                cin>>a>>m;
                double p=(a*1024)/m;
                cout<<"CPU中断频率="<<fixed<<setprecision(2)<<p<<endl;
                double q=1e6*1.0/(a*1024);
                cout<<"CPU响应时间="<<fixed<<setprecision(0)<<q<<"us"<<endl;
            }
            if(ope==2){
                cout<<"请按照顺序输入速率、缓冲位数:"<<endl;
                double a,m;
                cin>>a>>m;
                double p=(a*1024)/m;
                cout<<"CPU中断频率="<<fixed<<setprecision(2)<<p<<endl;
                double q=1e6*m*1.0/(a*1024);
                cout<<"CPU响应时间="<<fixed<<setprecision(0)<<q<<"us"<<endl;
            }
        }
        if(pos==2){
            cout << "请按照顺序输入速率、CPU中断频率:" << endl;
            double a,p;
            cin>>a>>p;
            double m=(a*1024)/p;
            cout<<"缓冲位数="<<fixed<<setprecision(0)<<m<<endl;
        }
    }
    if (id == 8) {
        cout << "n - 要处理的磁盘数据所占磁盘块数目"<<endl;
        cout << "T - 单个磁盘块数据输入到缓冲区的用时"<<endl;
        cout << "M - 缓冲区送往用户区用时"<<endl;
        cout << "C - CPU对用户区单块数据的计算处理用时"<<endl;
        cout << "S - 磁盘数据处理的总用时"<<endl;
        cout<<"问题目录:"<<endl;
        cout << "1 - 求单个磁盘块数据输入到缓冲区的用时" << endl;
        cout << "2 - 求缓冲区送往用户区用时" << endl;
        cout << "3 - 求CPU对用户区单块数据的计算处理用时" << endl;
        cout << "4 - 磁盘数据处理的总用时" << endl;
        cout << "请输入编号:" << endl;
        int ope;
        cin>>ope;
        if(ope==1){
            cout<<"1 - 单缓冲"<<endl;
            cout<<"2 - 双缓冲"<<endl;
            cout<<"请输入序号:"<<endl;
            int pos;
            cin>>pos;
            if(pos==1){
                cout<<"请按照顺序输入n、M、C、S"<<endl;
                int n,m,c,s;
                cin>>n>>m>>c>>s;
                cout<<"请根据选项判断T、C大小,若T>C请输入1;否则请输入2:"<<endl;
                int fla;
                cin>>fla;
                if(fla==1){ //T>C
                    int t=(s-c)/n-m;
                    cout << "单个磁盘块数据输入到缓冲区的用时="<<t<<endl;
                }
                else{
                    int t=s-n*(m+c);
                    cout << "单个磁盘块数据输入到缓冲区的用时=" << t << endl;
                }
            }
            if(pos==2){
                cout<<"请按照顺序输入n、M、C、S"<<endl;
                int n,m,c,s;
                cin>>n>>m>>c>>s;
                cout<<"请根据选项判断T、M+C大小,若T>M+C请输入1;否则请输入2:"<<endl;
                int fla;
                cin>>fla;
                if(fla==1){ //T>C
                    int t=(s-m-c)/n;
                    cout << "单个磁盘块数据输入到缓冲区的用时="<<t<<endl;
                }
                else{
                    int t=s-n*(m+c);
                    cout << "单个磁盘块数据输入到缓冲区的用时=" << t << endl;
                }
            }
        }
        if(ope==2){
            cout<<"1 - 单缓冲"<<endl;
            cout<<"2 - 双缓冲"<<endl;
            cout<<"请输入序号:"<<endl;
            int pos;
            cin>>pos;
            if(pos==1){
                cout<<"请按照顺序输入n、T、C、S"<<endl;
                int n,t,c,s;
                cin>>n>>t>>c>>s;
                int m=(s-t-c-(n-1)*max(c,t));
                cout << "求缓冲区送往用户区用时=" << m/n << endl;
            }
            if(pos==2){
                cout<<"请按照顺序输入n、T、C、S"<<endl;
                int n,t,c,s;
                cin>>n>>t>>c>>s;
                cout<<"请根据选项判断T、M+C大小,若T>M+C请输入1;否则请输入2:"<<endl;
                int fla;
                cin>>fla;
                if(fla==1){ //T>M+C
                    int m=s-n*t-c;
                    cout << "求缓冲区送往用户区用时=" << m << endl;
                }
                else{
                    int m=(s-t-n*c)/n;
                    cout << "求缓冲区送往用户区用时=" << m << endl;
                }
            }
        }
        if(ope==3){
            cout<<"1 - 单缓冲"<<endl;
            cout<<"2 - 双缓冲"<<endl;
            cout<<"请输入序号:"<<endl;
            int pos;
            cin>>pos;
            if(pos==1){
                cout<<"请按照顺序输入n、T、M、S"<<endl;
                int n,t,m,s;
                cin>>n>>t>>m>>s;
                cout<<"请根据选项判断T、C大小,若T>C请输入1;否则请输入2:"<<endl;
                int fla;
                cin>>fla;
                if(fla==1){ //T>C
                    int c = (s - n * (t + m));
                    cout << "CPU对用户区单块数据的计算处理用时="<<c<<endl;
                }
                else{
                    int c = (s - t - n * m) / n;
                    cout << "CPU对用户区单块数据的计算处理用时=" << c << endl;
                }
            }
            if(pos==2){
                cout<<"请按照顺序输入n、T、M、S"<<endl;
                int n,t,m,s;
                cin>>n>>t>>m>>s;
                cout<<"请根据选项判断T、M+C大小,若T>M+C请输入1;否则请输入2:"<<endl;
                int fla;
                cin>>fla;
                if(fla==1){ //T>C
                    int c=s-n*t-m;
                    cout << "CPU对用户区单块数据的计算处理用时="<<c<<endl;
                }
                else{
                    int c=(s-t)/n-m;
                    cout << "CPU对用户区单块数据的计算处理用时=" << c << endl;
                }
            }
        }
        if(ope==4){
            cout<<"1 - 单缓冲"<<endl;
            cout<<"2 - 双缓冲"<<endl;
            cout<<"请输入序号:"<<endl;
            int pos;
            cin>>pos;
            if(pos==1){
                cout<<"请按照顺序输入n、T、M、C"<<endl;
                int n,t,m,c;
                cin>>n>>t>>m>>c;
                int s=t+m+c+(n-1)*(max(c,t)+m);
                cout << "磁盘数据处理的总用时="<<s<<endl;
            }
            if(pos==2){
                cout<<"请按照顺序输入n、T、M、C"<<endl;
                int n,t,m,c;
                cin>>n>>t>>m>>c;
                int s=t+m+c+(n-1)*max(c+m,t);
                cout << "磁盘数据处理的总用时="<<s<<endl;
            }
        }
    }
    if (id == 9) {
        cout<<"n - 磁盘分区容量(G)"<<endl;
        cout<<"m - 存放位图需要的簇个数"<<endl;
        cout<<"a - 分区簇的大小(K)"<<endl;
        cout<<"问题列表:"<<endl;
        cout << "1 - 求磁盘分区容量(G)"<<endl;
        cout << "2 - 存放位图需要的簇个数"<<endl;
        cout << "3 - 分区簇的大小(K)"<<endl;
        cout<<"请输入问题编号:"<<endl;
        int ope;
        cin>>ope;
        if(ope==1){
            cout<<"请按照顺序输入m、a(KB)"<<endl;
            int m,a;
            cin>>m>>a;
            int n=8*a*a*m;
            cout << "磁盘分区容量="<<n/1024<<"G"<<endl;
        }
        if(ope==2){
            cout<<"请按照顺序输入n(G)、a(KB)"<<endl;
            int n,a;
            cin>>n>>a;
            int m=(n*1024)/(8*a*a);
            cout << "存放位图需要的簇个数=" << m << endl;
        }
        if(ope==3){
            cout<<"请按照顺序输入n(G)、m"<<endl;
            int n,m;
            cin>>n>>m;
            int a=(n*1024)/(8*m);
            cout << "分区簇的大小=" << sqrt(a)<<"KB" << endl;
        }
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    solve();
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值