ref_safety_algorithm2.py 3.96 KB
#_*_ coding: utf-8 _*_

from string import ascii_uppercase


letters = list(ascii_uppercase)


def main() :

    available = []
    original_available = []
    process = []
    finished_process = []
    finished = False

    total_allocation = []

    # resource는 3종류이며, 각각 20, 30, 40을 가지고 있다.
    number_of_resources = 3
    total_resources = [20,30,40]

    # process는 4개가 있다.
    number_of_processes = 4

    # [ process:[[max],[current]], ...]
    process = [[[10,6,4],[0,1,1]],[[5,6,7],[2,2,2]],[[15,3,4],[1,2,2]],[[7,9,20],[2,8,11]]]

    # total_allocation, available, original_available 계산
    total_allocation = get_total_allocation(process, number_of_processes, number_of_resources)
    available = get_total_available(total_resources, total_allocation, number_of_resources)
    original_available = available

    # finish 설정
    for i in range(0,number_of_processes) :
        finished_process.append(False)

    print(" total_resources : " + str(total_resources))
    print("total_allocation : " + str(total_allocation))
    print("       available : " + str(available))
    print("          finish : " + str(finished_process))


    get_process_need(process, number_of_processes, number_of_resources)
    print("\n         process :     Max      Allocation    Need")

    for i in range(0, number_of_processes) :
        print("                   " + str(process[i]))

    
    temp = list(process)

    while(len(process) != 0) :
        queue, process = compare_avail_need(available, process, number_of_processes, number_of_resources)
        available = get_new_avail(available, queue, len(queue), number_of_resources)


    is_safe, available = check_safety(original_available, available, total_allocation, number_of_resources)

    process = list(temp)

    print("New available instances : " + str(available))
    print("              Processes : " + str(process))
    print("                             Safe" if is_safe==True else "                         Not Safe")





def get_total_allocation(process, number_of_processes, number_of_resources) :
    sum = 0
    temp = []
    # 각 process들의 현재 할당된 resource를 더해서 현재 전체 사용중인 resource 도출 
    for i in range(0, number_of_resources) :
        for j in range(0, number_of_processes) :
            sum += process[j][1][i]
        temp.append(sum)
        sum = 0

    return temp

def get_total_available(total_resources, total_allocation, number_of_resources) :
    temp = []

    # 현재 사용 가능한 resource를 계산
    for i in range(0,number_of_resources) :
        temp.append(total_resources[i] - total_allocation[i])

    return temp

def get_process_need(process, number_of_processes, number_of_resources) :
    need = []
    for i in range(0, number_of_processes) :
        for j in range(0, number_of_resources) :
            need.append(process[i][0][j] - process[i][1][j])
        process[i].append(need)
        need = []

def compare_avail_need(available, process, number_of_processes, number_of_resources) :
    remove_list = []
    queue = []

    for i in range(0, number_of_processes) :
        if(process[i][2] < available) :
            queue.append([process[i][2], process[i][0]])
            remove_list.append(process[i])
    queue.sort()
    for i in range(0, len(remove_list)) :
        process.remove(remove_list[i])
    return queue, process

def get_new_avail(available, queue, number_of_processes, number_of_resources) :
    for i in range(0, number_of_processes) :
        for j in range(0, number_of_resources) :
            available[j] = abs(available[j] - queue[i][0][j]) + queue[i][1][j]
    return available

def check_safety(original_available, available, total_allocation, number_of_resources) :
    available = list([abs(rn_available - rn_total_allocation) for rn_available, rn_total_allocation in zip(available, total_allocation)])
    return (True if (original_available == available) else False), available



if __name__ == '__main__' :
    main()