Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The total number of tickets in the system should be guaranteed not to exceed Integer.MAX_VALUE. #22

Closed
thinkhy opened this issue Jul 10, 2014 · 2 comments
Assignees
Labels
Milestone

Comments

@thinkhy
Copy link
Owner

thinkhy commented Jul 10, 2014

Run MyTester.LotterySchedulerVAR3, hit the error:

[ThreadState.getEffectivePriority] holder thread: main (#0) Size: 1
[LotteryQueue.pickNextThread] Thread: main (#0) Priority: 1

java.lang.IllegalArgumentException
at java.util.Random.nextInt(Random.java:236)
at nachos.threads.LotteryScheduler$LotteryQueue.pickNextThread(LotteryScheduler.java:92)
at nachos.threads.PriorityScheduler$PriorityQueue.nextThread(PriorityScheduler.java:196)
at nachos.threads.KThread.runNextThread(KThread.java:351)
at nachos.threads.KThread.sleep(KThread.java:264)
at nachos.threads.KThread.finish(KThread.java:211)
at nachos.threads.KThread.runThread(KThread.java:163)
at nachos.threads.KThread.access$000(KThread.java:30)
at nachos.threads.KThread$1.run(KThread.java:150)
at nachos.machine.TCB.threadroot(TCB.java:240)
at nachos.machine.TCB.access$100(TCB.java:25)
at nachos.machine.TCB$1.run(TCB.java:93)
at java.lang.Thread.run(Thread.java:738)

@thinkhy thinkhy added this to the Project 2 milestone Jul 10, 2014
@thinkhy thinkhy self-assigned this Jul 10, 2014
@thinkhy thinkhy added the bug label Jul 10, 2014
@thinkhy
Copy link
Owner Author

thinkhy commented Jul 10, 2014


Thread: Child thread 84 (#86)     Priority: 536870911
Thread: Child thread 85 (#87)     Priority: 536870911
Thread: Child thread 86 (#88)     Priority: 536870911
Thread: Child thread 87 (#89)     Priority: 536870911
Thread: Child thread 88 (#90)     Priority: 536870911
Thread: Child thread 89 (#91)     Priority: 536870911
Thread: Child thread 90 (#92)     Priority: 536870911
Thread: Child thread 91 (#93)     Priority: 536870911
Thread: Child thread 92 (#94)     Priority: 536870911
Thread: Child thread 93 (#95)     Priority: 536870911
Thread: Child thread 94 (#96)     Priority: 536870911
Thread: Child thread 95 (#97)     Priority: 536870911
Thread: Child thread 96 (#98)     Priority: 536870911
Thread: Child thread 97 (#99)     Priority: 536870911
Thread: Child thread 98 (#100)    Priority: 536870911
Thread: Child thread 99 (#101)    Priority: 536870911
*******************
[ThreadState.getEffectivePriority] holder thread: Child thread 0 (#2) Size: 1
[ThreadState.getEffectivePriority] holder thread: Child thread 0 (#2)
[ThreadState.getEffectivePriority] holder thread: main (#0) Size: 1
[ThreadState.getEffectivePriority] holder thread: main (#0)
[ThreadState.getEffectivePriority] waitQueue priority: 0
[LotteryQueue.getEffectivePriority] controlled thread: main (#0) priority: 1
[ThreadState.getEffectivePriority] waitQueue priority: 1
[LotteryQueue.pickNextThread] Thread: Child thread 0 (#2)   Priority: 536870912
[ThreadState.getEffectivePriority] holder thread: Child thread 1 (#3) Size: 0
[LotteryQueue.pickNextThread] Thread: Child thread 1 (#3)   Priority: 536870911
[ThreadState.getEffectivePriority] holder thread: Child thread 2 (#4) Size: 0
[LotteryQueue.pickNextThread] Thread: Child thread 2 (#4)   Priority: 536870911
[ThreadState.getEffectivePriority] holder thread: Child thread 3 (#5) Size: 0
[LotteryQueue.pickNextThread] Thread: Child thread 3 (#5)   Priority: 536870911
[ThreadState.getEffectivePriority] holder thread: Child thread 4 (#6) Size: 0
[LotteryQueue.pickNextThread] Thread: Child thread 4 (#6)   Priority: 536870911

java.lang.ArithmeticException: Integer overflow
        at nachos.threads.LotteryScheduler.safeAdd(LotteryScheduler.java:185)
        at nachos.threads.LotteryScheduler$LotteryQueue.pickNextThread(LotteryScheduler.java:89)
        at nachos.threads.PriorityScheduler$PriorityQueue.nextThread(PriorityScheduler.java:196)
        at nachos.threads.KThread.runNextThread(KThread.java:351)
        at nachos.threads.KThread.sleep(KThread.java:264)
        at nachos.threads.KThread.join(KThread.java:317)
        at nachos.threads.MyTester.LotterySchedulerVAR3(MyTester.java:375)
        at nachos.threads.MyTester.TestLotteryScheduler(MyTester.java:271)
        at nachos.threads.MyTester.selfTest(MyTester.java:24)
        at nachos.threads.ThreadedKernel.selfTest(ThreadedKernel.java:53)
        at nachos.userprog.UserKernel.selfTest(UserKernel.java:57)
        at nachos.ag.AutoGrader.run(AutoGrader.java:153)
        at nachos.ag.AutoGrader.start(AutoGrader.java:50)
        at nachos.machine.Machine$1.run(Machine.java:64)
        at nachos.machine.TCB.threadroot(TCB.java:240)
        at nachos.machine.TCB.start(TCB.java:123)
        at nachos.machine.Machine.main(Machine.java:63)

@thinkhy
Copy link
Owner Author

thinkhy commented Jul 10, 2014

Add a interface "safeAdd" in LotteryScheduler class:

    /**
     * @B22A
     * Detect and prevent integer overflow for plus of two integers
     *
     * @param   left        left value of integer
     * @param   right       right value of integer
     *
     * @return  a integer, sum of left and right value
     */
    static final int safeAdd(int left, int right)                       /* @B22A */
                 throws ArithmeticException {                           /* @B22A */

     if (right > 0 ? left > Integer.MAX_VALUE - right                   /* @B22A */
               : left < Integer.MIN_VALUE - right) {                    /* @B22A */
        /* throw new ArithmeticException("Integer overflow");                    */
         return Integer.MAX_VALUE;                                      /* @B22A */
     }                                                                  /* @B22A */

     return left + right;                                               /* @B22A */
    }

Then use safeAdd to perform add operation for calculation of tickets

thinkhy added a commit that referenced this issue Jul 10, 2014
@thinkhy thinkhy closed this as completed Jul 10, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant