操作系统课程设计 —— 银行家算法

talk is easy, show the code

import Process.MaxResource.aMax
import Process.MaxResource.bMax
import Process.MaxResource.cMax
import Process.MaxResource.dMax

/**
 * 进程对象
 * @property name String 进程名
 * @property max Resource 该进程声明的最大所需资源
 * @property allocation Resource 为了创建进程所分配的资源
 * @property need Resource 为了执行进程所还需要的资源
 * @constructor 私有构造器, 防止被外部类直接实例化
 */
class Process private constructor(
    private val name: String,
    private val max: Resource,
    private val allocation: Resource
) {
    private lateinit var need: Resource

    /**
     * 使用静态变量存储系统最大资源值
     */
    private object MaxResource {
        var aMax = 3
        var bMax = 12
        var cMax = 14
        var dMax = 14

        /**
         * 以人能看懂的方式显示数据
         * @return String
         */
        override fun toString(): String {
            return "A -> $aMax B -> $bMax C -> $cMax D -> $dMax"
        }

        /**
         * 重载 比较 运算符, 方便检测系统安全性
         * @param other Resource 要比较的对象
         * @return Int 1 大于 0 等于 -1 小于
         */
        operator fun compareTo(other: Resource): Int {
            return if (other.A > aMax || other.B > bMax || other.C > cMax || other.D > dMax)
                1
            else if (other.A == aMax || other.B == bMax || other.C == cMax || other.D == dMax)
                0
            else -1
        }

    }

    /**
     * 资源对象
     * @property A Int
     * @property B Int
     * @property C Int
     * @property D Int
     * @constructor
     */
    private data class Resource(val A: Int, val B: Int, val C: Int, val D: Int) {
        /**
         * 申请资源
         */
        fun alloc() {
            aMax -= A
            bMax -= B
            cMax -= C
            dMax -= D
        }

        /**
         * 释放资源
         */
        fun free() {
            aMax += A
            bMax += B
            cMax += C
            dMax += D
        }

        /**
         * 以人能看懂的方式显示数据
         * @return String
         */
        override fun toString(): String {
            return "A -> $A B -> $B C -> $C D -> $D"
        }

        /**
         * 重载 - 运算符, 方便求还需要的资源
         * @param other Resource
         * @return Resource
         */
        operator fun minus(other: Resource): Resource {
            return Resource(
                A - other.A,
                B - other.B,
                C - other.C,
                D - other.D
            )
        }
    }

    /**
     * 使用 Builder 创建对象
     */
    object Builder {

        private lateinit var name: String
        private lateinit var maxResource: Resource
        private lateinit var allocResource: Resource

        /**
         * 进程名
         * @param value String
         * @return Builder
         */
        fun setName(value: String): Builder {
            this.name = value
            return this
        }

        /**
         * 已分配资源量
         * @param A Int
         * @param B Int
         * @param C Int
         * @param D Int
         * @return Builder
         */
        fun allocation(A: Int = 0, B: Int = 0, C: Int = 0, D: Int = 0): Builder {
            if (A > aMax || B > bMax || C > cMax || D > dMax)
                throw IllegalArgumentException("资源分配失败, 系统资源不够!")
            // 向系统申请资源创建进程
            allocResource = Resource(A, B, C, D).apply {
                alloc()
            }
            return this
        }

        /**
         * 资源最大需求量
         * @param A Int
         * @param B Int
         * @param C Int
         * @param D Int
         * @return Builder
         */
        fun max(A: Int = 0, B: Int = 0, C: Int = 0, D: Int = 0): Builder {
            maxResource = Resource(A, B, C, D)
            return this
        }

        /**
         * 构造 Process 对象
         * @return Process
         */
        fun build(): Process {
            // 如果存在没有初始化的对象, 那就使用默认参数初始化
            if (!this::name.isInitialized) setName("奥里给")
            if (!this::maxResource.isInitialized) max()
            if (!this::allocResource.isInitialized) allocation()
            return Process(name, maxResource, allocResource)
        }
    }

    /**
     * 检测系统安全性
     */
    fun checkSafe() {
        need = max - allocation
        println("进程名\t$name\n最大资源需求\t$max\n已分配资源\t$allocation\n还需要资源\t$need")
        println("当前剩余资源为 $MaxResource")
        if (MaxResource < need) {
            throw IllegalArgumentException("资源不够, 系统不安全!")
        } else {
            need.alloc()
            println(
                "资源正常分配, 系统安全\n当前剩余资源量为 $MaxResource\n" +
                        "$name 资源分配完毕, 进程结束释放资源"
            )
            need.free()
            allocation.free()
            println("资源释放完毕, 当前系统剩余资源量为 $MaxResource\n")
        }
    }
}

object Main {
    @JvmStatic
    fun main(args: Array<String>) {
        
        val p = listOf(
            Process.Builder.build(),
            Process.Builder
                .setName("P1")
                .max(C = 4, D = 4)
                .allocation(C = 3, D = 2)
                .build(),
            Process.Builder
                .setName("P2")
                .max(A = 2, B = 7, C = 5)
                .allocation(A = 1)
                .build(),
            Process.Builder
                .setName("P3")
                .max(A = 3, B = 6, C = 10, D = 10)
                .allocation(A = 1, B = 3, C = 5, D = 4)
                .build(),
            Process.Builder
                .setName("P4")
                .max(B = 9, C = 8, D = 4)
                .allocation(B = 3, C = 3, D = 2)
                .build(),
            Process.Builder
                .setName("P5")
                .max(B = 6, C = 6, D = 10)
                .allocation(C = 1, D = 4)
                .build()
        )
        p[1].checkSafe()
        p[4].checkSafe()
        p[5].checkSafe()
        p[2].checkSafe()
        p[3].checkSafe()
    }
}
posted @ 2021-08-30 08:38  博麗靈夢  阅读(149)  评论(0编辑  收藏  举报