// File: /workspace/github_cpp_makefile_star2_O2/MUST-Quick-Fry[P]Process-Scheduler/scheduler.cpp
// Function: Project::Scheduler::driveRR()
void Scheduler::driveRR(){
    
    // create a schedule
    queue<Job> pq_arr;
    queue<Job> wait_q; 
    int len = get_job_num();
    
    // clear -1
    for(int i = 0; i < len; ++i){
        if(job_queue[i].get_dur_time()==-1){ 
            int t = 0;
            find_dur_time(job_queue[i], t);
            //cout << t << endl;
            job_queue[i].set_dur_time(t);
        }
    }
     
    sort(job_queue.begin(), job_queue.end());
     
    for(int i = 0; i < len; ++i){
        pq_arr.emplace(job_queue[i]);  
    }
       
    int time = 0;
    int wait_for_empty = 0;
   
    while(true){
                                 
        while(!pq_arr.empty() && time == pq_arr.front().get_arr_time()){
            Job tmp = pq_arr.front();
            pq_arr.pop(); 
                
            if(tmp.get_dur_time() > QUANT){
                tmp.service_time_left = tmp.get_dur_time();
                scheduler.emplace(tmp);
                scheduler.back().set_dur_time(QUANT);
                tmp.set_dur_time(tmp.get_dur_time()-QUANT);
                wait_q.emplace(tmp);
                wait_for_empty += QUANT;
            }
            else{
                tmp.service_time_left = tmp.get_dur_time();
                scheduler.emplace(tmp);
                wait_for_empty += tmp.get_dur_time();
            }
                 
        }
        
        if(wait_for_empty == 0){
            if(wait_q.empty()){;}
            else{
                Job tmp2 = wait_q.front();
                wait_q.pop();
                tmp2.set_arr_time(time);
                if(tmp2.get_dur_time() > QUANT){
                    tmp2.service_time_left = tmp2.get_dur_time();
                    scheduler.emplace(tmp2);
                    scheduler.back().set_dur_time(QUANT);
                    tmp2.set_dur_time(tmp2.get_dur_time()-QUANT);
                    wait_q.emplace(tmp2);
                    wait_for_empty += QUANT;
                }
                else{
                    tmp2.service_time_left = tmp2.get_dur_time();
                    scheduler.emplace(tmp2);
                    wait_for_empty += tmp2.get_dur_time();
                }
            }
        }
        
        time++;
        if(wait_for_empty > 0){wait_for_empty--;}
        
        if(wait_q.empty() && pq_arr.empty()){break;}    
    }
    
        
    // copy
    set_total_time(time);
    real_job_queue = scheduler;
    cout << endl;
    cout << endl;
    cout << endl;
    
    // execute
    cout << "==================================================="<< endl;
    cout << "Now start the Scheduler !!!"<< endl;
    cout << "==================================================="<< endl;
    
    int realtime = 0;
    int pid = 1;
   
    while(!stop_flag){
     
        cout << "now time: " << realtime << " s" << endl;    
        
        while(!scheduler.empty() && realtime == scheduler.front().get_arr_time()){
            auto it = scheduler.front();
            scheduler.pop();
                        
            if(monitor_map[it.ID]==0){
                pid = fork();
                if(pid == 0){
                    Monitor* monitor = new Monitor(it);                       
                    cout << "Terminate the Parent Process: " << getpid() <<endl;     
                    
                    //kill(getpid(), SIGKILL);
                    exit(0);
  
                }
                else{ 
                    monitor_map[it.ID]=pid;
                }
            }
            
            
            if(pid !=0){     
                                           
                     if(allow_preem){                 
                         allow_preem = false;                                  
                         this_job = it;
                         
                         kill(monitor_map[it.ID], SIGCONT);
                         signal(SIGALRM, signal_RR);
                         alarm(it.get_dur_time());
 
                     }
                     else{
                         wait_queue.emplace(it);
                         kill(monitor_map[it.ID], SIGTSTP);
                         std::cout<<"Child Process Suspend" << std::endl;
                     }
                 
             }
        }
        
        
        if(pid!=0){       
             sleep(1); 
             realtime++;
                          
        }
                        
    }

    if(pid !=0){ 
        cout << "now time: " << realtime << " s" << endl;       
        sleep(2);
        cout << endl;
        cout << endl;
        
        // draw Gantt diagram
        Display(real_job_queue);

    }
        
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x208, %rsp            # imm = 0x208
movq %rdi, %rbx
leaq -0x128(%rbp), %rdi
callq 0x2ed2
leaq -0xd8(%rbp), %rdi
callq 0x2ed2
movq %rbx, -0x30(%rbp)
movl 0x58(%rbx), %eax
xorl %r12d, %r12d
testl %eax, %eax
cmovlel %r12d, %eax
imulq $0x58, %rax, %r15
leaq -0x228(%rbp), %rbx
leaq -0x88(%rbp), %r14
movq 0xca7c(%rip), %rdi      # 0x12810
cmpq %r12, %r15
je 0x5ded
addq %r12, %rdi
callq 0x34be
cmpl $-0x1, %eax
jne 0x5de7
andl $0x0, -0x88(%rbp)
movq 0xca5c(%rip), %rsi      # 0x12810
addq %r12, %rsi
movq %rbx, %rdi
callq 0x350a
movq %rbx, %rsi
movq %r14, %rdx
callq 0x4918
movq %rbx, %rdi
callq 0x2100
movq 0xca37(%rip), %rdi      # 0x12810
addq %r12, %rdi
movl -0x88(%rbp), %esi
callq 0x3488
addq $0x58, %r12
jmp 0x5d8d
movq 0xca24(%rip), %rsi      # 0x12818
callq 0xa951
xorl %r14d, %r14d
leaq -0x128(%rbp), %rbx
cmpq %r14, %r15
je 0x5e20
movq 0xca01(%rip), %rsi      # 0x12810
addq %r14, %rsi
movq %rbx, %rdi
callq 0xb0c6
addq $0x58, %r14
jmp 0x5e03
xorl %r14d, %r14d
leaq -0x88(%rbp), %r15
leaq 0xc9f7(%rip), %r13      # 0x12828
leaq -0x128(%rbp), %rbx
xorl %r12d, %r12d
movq -0x118(%rbp), %rdi
cmpq %rdi, -0xf8(%rbp)
je 0x5f1e
callq 0x34b4
cmpl %eax, %r14d
jne 0x5f1e
movq -0x118(%rbp), %rsi
movq %r15, %rdi
callq 0x350a
movq %rbx, %rdi
callq 0xa080
movq %r15, %rdi
callq 0x34be
cmpl $0x3, %eax
jl 0x5ef0
movq %r15, %rdi
callq 0x34be
movl %eax, -0x58(%rbp)
movq %r13, %rdi
movq %r15, %rsi
callq 0xb0c6
movq 0xc9ba(%rip), %rdi      # 0x12858
cmpq 0xc9bb(%rip), %rdi      # 0x12860
jne 0x5eba
movq 0xc9c2(%rip), %rax      # 0x12870
movq -0x8(%rax), %rdi
movl $0x1b8, %eax            # imm = 0x1B8
addq %rax, %rdi
addq $-0x58, %rdi
pushq $0x2
popq %rsi
callq 0x3488
movq %r15, %rdi
callq 0x34be
addl $-0x2, %eax
movq %r15, %rdi
movl %eax, %esi
callq 0x3488
leaq -0xd8(%rbp), %rdi
movq %r15, %rsi
callq 0xb0c6
addl $0x2, %r12d
jmp 0x5f11
movq %r15, %rdi
callq 0x34be
movl %eax, -0x58(%rbp)
movq %r13, %rdi
movq %r15, %rsi
callq 0xb0c6
movq %r15, %rdi
callq 0x34be
addl %eax, %r12d
movq %r15, %rdi
callq 0x2100
jmp 0x5e3b
movq -0xc8(%rbp), %rsi
movq -0xa8(%rbp), %rax
testl %r12d, %r12d
jne 0x6018
cmpq %rsi, %rax
je 0x5fd9
movq %r15, %rdi
callq 0x350a
leaq -0xd8(%rbp), %rdi
callq 0xa080
movq %r15, %rdi
movl %r14d, %esi
callq 0x347e
movq %r15, %rdi
callq 0x34be
cmpl $0x3, %eax
jl 0x5fe1
movq %r15, %rdi
callq 0x34be
movl %eax, -0x58(%rbp)
movq %r13, %rdi
movq %r15, %rsi
callq 0xb0c6
movq 0xc8d1(%rip), %rdi      # 0x12858
cmpq 0xc8d2(%rip), %rdi      # 0x12860
jne 0x5fa3
movq 0xc8d9(%rip), %rax      # 0x12870
movq -0x8(%rax), %rdi
movl $0x1b8, %eax            # imm = 0x1B8
addq %rax, %rdi
addq $-0x58, %rdi
pushq $0x2
popq %rsi
callq 0x3488
movq %r15, %rdi
callq 0x34be
addl $-0x2, %eax
movq %r15, %rdi
movl %eax, %esi
callq 0x3488
leaq -0xd8(%rbp), %rdi
movq %r15, %rsi
callq 0xb0c6
pushq $0x2
popq %r12
jmp 0x6002
incl %r14d
xorl %r12d, %r12d
jmp 0x602f
movq %r15, %rdi
callq 0x34be
movl %eax, -0x58(%rbp)
movq %r13, %rdi
movq %r15, %rsi
callq 0xb0c6
movq %r15, %rdi
callq 0x34be
movl %eax, %r12d
movq %r15, %rdi
callq 0x2100
movq -0xc8(%rbp), %rsi
movq -0xa8(%rbp), %rax
xorl %ecx, %ecx
testl %r12d, %r12d
setg %cl
subl %ecx, %r12d
incl %r14d
cmpq %rsi, %rax
jne 0x5e3b
movq -0xf8(%rbp), %rax
cmpq -0x118(%rbp), %rax
jne 0x5e3b
movq -0x30(%rbp), %rax
movl %r14d, 0x5c(%rax)
leaq 0xc876(%rip), %rdi      # 0x128c8
leaq 0xc7cf(%rip), %rsi      # 0x12828
callq 0x76d2
movq 0xbf5b(%rip), %rdi      # 0x11fc0
callq 0x2110
movq 0xbf4f(%rip), %rdi      # 0x11fc0
callq 0x2110
movq 0xbf43(%rip), %rdi      # 0x11fc0
callq 0x2110
movq 0xbf37(%rip), %rdi      # 0x11fc0
leaq 0x626d(%rip), %rsi      # 0xc2fd
callq 0x2230
movq %rax, %rdi
callq 0x2110
movq 0xbf1c(%rip), %rdi      # 0x11fc0
leaq 0x61be(%rip), %rsi      # 0xc269
callq 0x2230
movq %rax, %rdi
callq 0x2110
movq 0xbf01(%rip), %rdi      # 0x11fc0
leaq 0x6237(%rip), %rsi      # 0xc2fd
callq 0x2230
movq %rax, %rdi
callq 0x2110
xorl %ebx, %ebx
leaq -0x5c(%rbp), %r14
leaq -0x88(%rbp), %r13
leaq 0xc869(%rip), %r15      # 0x12950
cmpb $0x0, 0xc89a(%rip)      # 0x12988
jne 0x6241
movq 0xbec5(%rip), %rdi      # 0x11fc0
leaq 0x6183(%rip), %rsi      # 0xc285
callq 0x2230
movq %rax, %rdi
movl %ebx, %esi
callq 0x23a0
movq %rax, %rdi
leaq 0x6175(%rip), %rsi      # 0xc290
callq 0x2230
movq %rax, %rdi
callq 0x2110
movq 0xc709(%rip), %rdi      # 0x12838
cmpq %rdi, 0xc722(%rip)      # 0x12858
je 0x6232
callq 0x34b4
cmpl %eax, %ebx
jne 0x6232
movq 0xc6e8(%rip), %rsi      # 0x12838
movq %r13, %rdi
callq 0x350a
leaq 0xc6c9(%rip), %rdi      # 0x12828
callq 0xa080
movq %r15, %rdi
movq %r14, %rsi
callq 0x9faa
cmpl $0x0, (%rax)
jne 0x6192
callq 0x2090
testl %eax, %eax
je 0x62ee
movl %eax, %r12d
movq %r15, %rdi
movq %r14, %rsi
callq 0x9faa
movl %r12d, (%rax)
cmpb $0x0, 0xc7f0(%rip)      # 0x12989
je 0x61dc
leaq 0xc6d6(%rip), %rdi      # 0x12878
movq %r13, %rsi
callq 0xb0c6
movq %r15, %rdi
movq %r14, %rsi
callq 0x9faa
movl (%rax), %edi
pushq $0x14
popq %rsi
callq 0x22d0
movq 0xbdfa(%rip), %rdi      # 0x11fc0
leaq 0x60e5(%rip), %rsi      # 0xc2b2
callq 0x2230
movq %rax, %rdi
callq 0x2110
jmp 0x6225
movb $0x1, 0xc7a6(%rip)      # 0x12989
leaq 0xc5ce(%rip), %rdi      # 0x127b8
movq %r13, %rsi
callq 0x758c
movq %r15, %rdi
movq %r14, %rsi
callq 0x9faa
movl (%rax), %edi
pushq $0x12
popq %rsi
callq 0x22d0
pushq $0xe
popq %rdi
leaq 0x609(%rip), %rsi       # 0x681a
callq 0x2260
movq %r13, %rdi
callq 0x34be
movl %eax, %edi
callq 0x23d0
movq %r13, %rdi
callq 0x2100
jmp 0x6128
pushq $0x1
popq %rdi
callq 0x21c0
incl %ebx
jmp 0x60e7
movq 0xbd78(%rip), %rdi      # 0x11fc0
leaq 0x6036(%rip), %rsi      # 0xc285
callq 0x2230
movq %rax, %rdi
movl %ebx, %esi
callq 0x23a0
leaq 0x602b(%rip), %rsi      # 0xc290
movq %rax, %rdi
callq 0x2230
movq %rax, %rdi
callq 0x2110
pushq $0x2
popq %rdi
callq 0x21c0
movq 0xbd3c(%rip), %rdi      # 0x11fc0
callq 0x2110
movq 0xbd30(%rip), %rdi      # 0x11fc0
callq 0x2110
leaq 0xc62c(%rip), %rsi      # 0x128c8
leaq -0x178(%rbp), %rdi
callq 0x9e98
leaq -0x178(%rbp), %rsi
movq -0x30(%rbp), %rdi
callq 0x64d6
leaq -0x178(%rbp), %rdi
callq 0x2f36
leaq -0xd8(%rbp), %rdi
callq 0x2f36
leaq -0x128(%rbp), %rdi
callq 0x2f36
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xd0, %edi
callq 0x2240
movq %rax, %r14
leaq -0x1d0(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x350a
leaq -0x1d0(%rbp), %rsi
movq %r14, %rdi
callq 0x36ca
leaq -0x1d0(%rbp), %rdi
callq 0x2100
movq 0xbc90(%rip), %rdi      # 0x11fc0
leaq 0x5f5c(%rip), %rsi      # 0xc293
callq 0x2230
movq %rax, %rbx
callq 0x2300
movq %rbx, %rdi
movl %eax, %esi
callq 0x23a0
movq %rax, %rdi
callq 0x2110
xorl %edi, %edi
callq 0x2270
movq %rax, %rbx
leaq -0x178(%rbp), %rdi
callq 0x2f36
jmp 0x63cd
jmp 0x63b7
movq %rax, %rbx
jmp 0x63d9
jmp 0x63b7
jmp 0x63b7
movq %rax, %rbx
leaq -0x228(%rbp), %rdi
jmp 0x63c8
jmp 0x63b7
movq %rax, %rbx
leaq -0x1d0(%rbp), %rdi
callq 0x2100
jmp 0x63a1
jmp 0x63b7
jmp 0x63b7
jmp 0x63b7
movq %rax, %rbx
movq %r14, %rdi
callq 0x2220
jmp 0x63c1
jmp 0x63be
jmp 0x63be
jmp 0x63b7
jmp 0x63b7
jmp 0x63b7
jmp 0x63b7
movq %rax, %rbx
jmp 0x63cd
jmp 0x63be
movq %rax, %rbx
leaq -0x88(%rbp), %rdi
callq 0x2100
leaq -0xd8(%rbp), %rdi
callq 0x2f36
leaq -0x128(%rbp), %rdi
callq 0x2f36
movq %rbx, %rdi
callq 0x23b0