内存读写死锁

内存条

http://www.nowcoder.com/questionTerminal/3441be0bbf0a4dfd8527198a08a71fb6

分R==W、R<W、R>W三种情况

import java.util.*;
public class Main {
    public static void main(String[] args) throws Exception{
        Scanner sc = new Scanner(System.in);
        int L = sc.nextInt(), R = sc.nextInt(), W = sc.nextInt();
        if(R > L || W > L){    //读写超过内存总量的,直接死锁
            System.out.println("DEADLOCK");
            return;
        }
        if(R == W) System.out.println("OK");    //读写等大的,交替即可OK
        if(R < W){
            int sum = 0;
            while(L - sum >= W) sum += W - R;    //写大于读的,交替增量直到不能再写
            if(sum < R) System.out.println("DEADLOCK");    //增量总量不够读一次,死锁
            else System.out.println("OK");
        }
        if(R > W){
            int sum = 0;
            while(sum <= L) sum += W;    //读大于写的,先尽量写满
            sum -= W;
            if(sum < R) System.out.println("DEADLOCK");    //不够读一次,死锁
            else System.out.println("OK");
        }
    }
}
全部评论

相关推荐

06-07 17:17
嘉兴学院 教师
心爱的idea:你孩
点赞 评论 收藏
分享
06-26 15:33
青岛工学院 Java
积极的秋田犬要冲国企:他现在邀请我明天面试
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务