OS作业模拟进程分配与回收

本文介绍了一个简单的操作系统进程内存管理模拟程序。该程序通过输入选择来为进程分配或回收内存,并确保总内存不会超出限定范围。文章详细展示了如何初始化内存、添加及移除进程等操作。

OS的一个作业, 模拟进程的分配与管理


# initialize the memories and the process list(actually a dict)
total_memory = 110
os_memory = 10
process_memory = total_memory - os_memory
process = {}
sep_line_counter = 0

while 1:
    # For the neatness of the output of the program
    sep_line_counter += 1
    print('----------------------Seperation Line %d--------------------------' % sep_line_counter)

    # Input of choice, which is robustic here.
    choice = input('Assign memory or recycle memory or quit?\nPlease enter--assgnment: 1, recycle: 2, quit: q\n:')
    if not ( choice == 'q' or choice == '1' or choice == '2'):
        print('Please enter the right choice.')
        continue
    
    if choice == 'q':
        # Choose to quit
        break

    if int(choice) == 1:
        # Choose to add a process
        # Get name
        cur_proc_name = input('Name of the process?\n:')
        if cur_proc_name in process.keys():
            # if name of current process has existed in the process list
            print('The process you assigned has been created, no sharing of processes\' name.')
            continue
        # Get size
        cur_proc_size = int(input('OK, and what about its size?\n:'))
        if sum(process.values()) + int(cur_proc_size) > process_memory:
            # With current process entering the process list, the list would overload
            print('STOP ASSIGNMENT! Otherwise there\'s overload.')
            print( 'The remaining memory: %d KB' % ( process_memory - sum(process.values()) ) )
            print('The process list:', process)
            continue
        # No accidents, current process would be added into the process list
        process[cur_proc_name] = cur_proc_size
        print( 'The remaining memory: %d KB' % ( process_memory - sum(process.values()) ) )
        print('The process list:', process)
        continue

    if int(choice) == 2:
        # Choose to recycle a process
        cur_proc_name = input('Name of the process?\n:')
        if not cur_proc_name in process.keys():
            # If you want to recycle some process that doesn't exist
            print('No such process.')
            continue
        # No accidents, current process would be removed from the process list
        del process[cur_proc_name]
        print( 'The remaining memory: %d KB' % ( process_memory - sum(process.values() ) ) )
        print('The process list:', process)
        continue

本文版权归郑鹏(默盒)和博客园共有,原创文章,未经允许不得转载,否则保留追究法律责任的权利。

转载于:https://www.cnblogs.com/ZhengPeng7/p/7217843.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值