// File: /workspace/github_cpp_makefile_star2_O1/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
xorps %xmm0, %xmm0
leaq -0xe0(%rbp), %rdi
movaps %xmm0, 0x40(%rdi)
movaps %xmm0, 0x30(%rdi)
movaps %xmm0, 0x20(%rdi)
movaps %xmm0, 0x10(%rdi)
movaps %xmm0, (%rdi)
xorl %esi, %esi
callq 0x3550
leaq -0x130(%rbp), %rdi
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rdi)
movaps %xmm0, 0x30(%rdi)
movaps %xmm0, 0x20(%rdi)
movaps %xmm0, 0x10(%rdi)
movaps %xmm0, (%rdi)
xorl %esi, %esi
callq 0x3550
movq %rbx, -0x38(%rbp)
movslq 0x58(%rbx), %rax
movq %rax, -0x30(%rbp)
testq %rax, %rax
jle 0x73e8
leaq -0x1d0(%rbp), %r12
imulq $0x58, -0x30(%rbp), %r13
xorl %r15d, %r15d
leaq -0x1e0(%rbp), %rbx
movq 0xe4f5(%rip), %rdi      # 0x15830
addq %r15, %rdi
callq 0x3746
cmpl $-0x1, %eax
jne 0x73db
movl $0x0, -0x90(%rbp)
movq 0xe4d3(%rip), %r14      # 0x15830
movq %r12, -0x1e0(%rbp)
movq (%r14,%r15), %rsi
movq 0x8(%r14,%r15), %rdx
addq %rsi, %rdx
movq %rbx, %rdi
callq 0x37c8
movq 0x4e(%r14,%r15), %rax
movq %rax, 0x3e(%r12)
movups 0x20(%r14,%r15), %xmm0
movups 0x30(%r14,%r15), %xmm1
movups 0x40(%r14,%r15), %xmm2
movups %xmm2, 0x30(%r12)
movups %xmm1, 0x20(%r12)
movups %xmm0, 0x10(%r12)
movq %rbx, %rsi
leaq -0x90(%rbp), %rdx
callq 0x521a
movq -0x1e0(%rbp), %rdi
cmpq %r12, %rdi
je 0x73c6
callq 0x2250
movq 0xe463(%rip), %rdi      # 0x15830
addq %r15, %rdi
movl -0x90(%rbp), %esi
callq 0x36fc
addq $0x58, %r15
cmpq %r15, %r13
jne 0x7334
movq 0xe441(%rip), %rbx      # 0x15830
movq 0xe442(%rip), %r14      # 0x15838
cmpq %r14, %rbx
je 0x7436
movq %r14, %rax
subq %rbx, %rax
sarq $0x3, %rax
movabsq $0x2e8ba2e8ba2e8ba3, %rcx # imm = 0x2E8BA2E8BA2E8BA3
imulq %rax, %rcx
bsrq %rcx, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0xd9ad
movq %rbx, %rdi
movq %r14, %rsi
callq 0xda8a
movq -0x30(%rbp), %rax
testl %eax, %eax
jle 0x7467
imulq $0x58, %rax, %r14
xorl %r15d, %r15d
leaq -0xe0(%rbp), %rbx
movq 0xe3dd(%rip), %rsi      # 0x15830
addq %r15, %rsi
movq %rbx, %rdi
callq 0xe8c8
addq $0x58, %r15
cmpq %r15, %r14
jne 0x744c
leaq -0x80(%rbp), %rbx
xorl %r14d, %r14d
leaq -0x90(%rbp), %r15
xorl %r12d, %r12d
movq -0xd0(%rbp), %rdi
cmpq %rdi, -0xb0(%rbp)
je 0x75ab
callq 0x373c
cmpl %eax, %r14d
jne 0x75ab
movq -0xd0(%rbp), %r13
movq %rbx, -0x90(%rbp)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
addq %rsi, %rdx
movq %r15, %rdi
callq 0x37c8
movq 0x4e(%r13), %rax
movq %rax, 0x3e(%rbx)
movups 0x20(%r13), %xmm0
movups 0x30(%r13), %xmm1
movups 0x40(%r13), %xmm2
movups %xmm2, 0x30(%rbx)
movups %xmm1, 0x20(%rbx)
movups %xmm0, 0x10(%rbx)
leaq -0xe0(%rbp), %rdi
callq 0xce58
movq %r15, %rdi
callq 0x3746
cmpl $0x3, %eax
jl 0x756c
movq %r15, %rdi
callq 0x3746
movl %eax, -0x60(%rbp)
leaq 0xe33f(%rip), %rdi      # 0x15848
movq %r15, %rsi
callq 0xe8c8
movq 0xe360(%rip), %rdi      # 0x15878
cmpq 0xe361(%rip), %rdi      # 0x15880
jne 0x7534
movq 0xe368(%rip), %rax      # 0x15890
movq -0x8(%rax), %rdi
movl $0x1b8, %eax            # imm = 0x1B8
addq %rax, %rdi
addq $-0x58, %rdi
movl $0x2, %esi
callq 0x36fc
movq %r15, %rdi
callq 0x3746
addl $-0x2, %eax
movq %r15, %rdi
movl %eax, %esi
callq 0x36fc
leaq -0x130(%rbp), %rdi
movq %r15, %rsi
callq 0xe8c8
addl $0x2, %r12d
jmp 0x7591
movq %r15, %rdi
callq 0x3746
movl %eax, -0x60(%rbp)
leaq 0xe2ca(%rip), %rdi      # 0x15848
movq %r15, %rsi
callq 0xe8c8
movq %r15, %rdi
callq 0x3746
addl %eax, %r12d
movq -0x90(%rbp), %rdi
cmpq %rbx, %rdi
je 0x7478
callq 0x2250
jmp 0x7478
testl %r12d, %r12d
je 0x75eb
incl %r14d
xorl %eax, %eax
testl %r12d, %r12d
setg %al
subl %eax, %r12d
movq -0x100(%rbp), %rax
cmpq -0x120(%rbp), %rax
jne 0x7478
movq -0xb0(%rbp), %rax
cmpq -0xd0(%rbp), %rax
jne 0x7478
jmp 0x7723
movq -0x120(%rbp), %r12
cmpq %r12, -0x100(%rbp)
je 0x76dc
movq %rbx, -0x90(%rbp)
movq (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
movq %r15, %rdi
callq 0x37c8
movq 0x4e(%r12), %rax
movq %rax, 0x3e(%rbx)
movups 0x20(%r12), %xmm0
movups 0x30(%r12), %xmm1
movups 0x40(%r12), %xmm2
movups %xmm2, 0x30(%rbx)
movups %xmm1, 0x20(%rbx)
movups %xmm0, 0x10(%rbx)
leaq -0x130(%rbp), %rdi
callq 0xce58
movq %r15, %rdi
movl %r14d, %esi
callq 0x36f2
movq %r15, %rdi
callq 0x3746
cmpl $0x3, %eax
jl 0x76e4
movq %r15, %rdi
callq 0x3746
movl %eax, -0x60(%rbp)
leaq 0xe1d1(%rip), %rdi      # 0x15848
movq %r15, %rsi
callq 0xe8c8
movq 0xe1f2(%rip), %rdi      # 0x15878
cmpq 0xe1f3(%rip), %rdi      # 0x15880
jne 0x76a2
movq 0xe1fa(%rip), %rax      # 0x15890
movq -0x8(%rax), %rdi
movl $0x1b8, %eax            # imm = 0x1B8
addq %rax, %rdi
addq $-0x58, %rdi
movl $0x2, %esi
callq 0x36fc
movq %r15, %rdi
callq 0x3746
addl $-0x2, %eax
movq %r15, %rdi
movl %eax, %esi
callq 0x36fc
movl $0x2, %r12d
leaq -0x130(%rbp), %rdi
movq %r15, %rsi
callq 0xe8c8
jmp 0x7709
xorl %r12d, %r12d
jmp 0x75b0
movq %r15, %rdi
callq 0x3746
movl %eax, -0x60(%rbp)
leaq 0xe152(%rip), %rdi      # 0x15848
movq %r15, %rsi
callq 0xe8c8
movq %r15, %rdi
callq 0x3746
movl %eax, %r12d
movq -0x90(%rbp), %rdi
cmpq %rbx, %rdi
je 0x75b0
callq 0x2250
jmp 0x75b0
movq -0x38(%rbp), %rax
movl %r14d, 0x5c(%rax)
leaq 0xe1b6(%rip), %rdi      # 0x158e8
leaq 0xe10f(%rip), %rsi      # 0x15848
callq 0xa7ca
movq 0xd883(%rip), %rdi      # 0x14fc8
movq (%rdi), %rax
addq -0x18(%rax), %rdi
movl $0xa, %esi
callq 0x2260
movsbl %al, %esi
movq 0xd868(%rip), %r14      # 0x14fc8
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2190
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x2260
movsbl %al, %esi
movq 0xd83a(%rip), %r14      # 0x14fc8
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2190
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x2260
movsbl %al, %esi
movq 0xd80c(%rip), %rdi      # 0x14fc8
callq 0x2040
movq %rax, %rdi
callq 0x2190
movq 0xd7f8(%rip), %r14      # 0x14fc8
leaq 0x8b69(%rip), %rsi      # 0x10340
movl $0x33, %edx
movq %r14, %rdi
callq 0x22a0
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x2260
movsbl %al, %esi
movq 0xd7c6(%rip), %rdi      # 0x14fc8
callq 0x2040
movq %rax, %rdi
callq 0x2190
movq 0xd7b2(%rip), %r14      # 0x14fc8
leaq 0x8a8f(%rip), %rsi      # 0x102ac
movl $0x1b, %edx
movq %r14, %rdi
callq 0x22a0
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x2260
movsbl %al, %esi
movq 0xd780(%rip), %rdi      # 0x14fc8
callq 0x2040
movq %rax, %rdi
callq 0x2190
movq 0xd76c(%rip), %r14      # 0x14fc8
leaq 0x8add(%rip), %rsi      # 0x10340
movl $0x33, %edx
movq %r14, %rdi
callq 0x22a0
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x2260
movsbl %al, %esi
movq 0xd73a(%rip), %rdi      # 0x14fc8
callq 0x2040
movq %rax, %rdi
callq 0x2190
xorl %r15d, %r15d
cmpb $0x0, 0xe103(%rip)      # 0x159a8
jne 0x7aec
xorl %r15d, %r15d
leaq -0x90(%rbp), %r12
leaq 0xe0b4(%rip), %r13      # 0x15970
movl $0xa, %edx
movq 0xd700(%rip), %rdi      # 0x14fc8
leaq 0x89f9(%rip), %rsi      # 0x102c8
callq 0x22a0
movq 0xd6ed(%rip), %rdi      # 0x14fc8
movl %r15d, %esi
callq 0x23d0
movq %rax, %r14
movl $0x2, %edx
movq %rax, %rdi
leaq 0x89de(%rip), %rsi      # 0x102d3
callq 0x22a0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2260
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2190
movq 0xdf30(%rip), %rdi      # 0x15858
cmpq %rdi, 0xdf49(%rip)      # 0x15878
je 0x7ad2
callq 0x373c
cmpl %eax, %r15d
jne 0x7ad2
movq 0xdf0e(%rip), %r14      # 0x15858
movq %rbx, -0x90(%rbp)
movq (%r14), %rsi
movq 0x8(%r14), %rdx
addq %rsi, %rdx
movq %r12, %rdi
callq 0x37c8
movq 0x4e(%r14), %rax
movq %rax, 0x3e(%rbx)
movups 0x20(%r14), %xmm0
movups 0x30(%r14), %xmm1
movups 0x40(%r14), %xmm2
movups %xmm2, 0x30(%rbx)
movups %xmm1, 0x20(%rbx)
movups %xmm0, 0x10(%rbx)
leaq 0xdebb(%rip), %rdi      # 0x15848
callq 0xce58
movq %r13, %rdi
leaq -0x64(%rbp), %rsi
callq 0xcdb0
cmpl $0x0, (%rax)
jne 0x79c2
callq 0x20a0
testl %eax, %eax
je 0x7c10
movl %eax, %r14d
movq %r13, %rdi
leaq -0x64(%rbp), %rsi
callq 0xcdb0
movl %r14d, (%rax)
cmpb $0x0, 0xdfe0(%rip)      # 0x159a9
je 0x7a3e
leaq 0xdec6(%rip), %rdi      # 0x15898
movq %r12, %rsi
callq 0xe8c8
movq %r13, %rdi
leaq -0x64(%rbp), %rsi
callq 0xcdb0
movl (%rax), %edi
movl $0x14, %esi
callq 0x2310
movl $0x15, %edx
movq 0xd5ca(%rip), %rdi      # 0x14fc8
leaq 0x88f0(%rip), %rsi      # 0x102f5
callq 0x22a0
movq 0xd5b7(%rip), %rcx      # 0x14fc8
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x2260
movsbl %al, %esi
movq 0xd599(%rip), %rdi      # 0x14fc8
callq 0x2040
movq %rax, %rdi
callq 0x2190
jmp 0x7ab8
movb $0x1, 0xdf64(%rip)      # 0x159a9
leaq 0xdd8c(%rip), %rdi      # 0x157d8
movq %r12, %rsi
callq 0x2150
movq 0x3e(%rbx), %rax
movq %rax, 0xddc7(%rip)      # 0x15826
movups 0x10(%rbx), %xmm0
movups 0x20(%rbx), %xmm1
movups 0x30(%rbx), %xmm2
movups %xmm2, 0xdda6(%rip)     # 0x15818
movups %xmm1, 0xdd8f(%rip)     # 0x15808
movups %xmm0, 0xdd78(%rip)     # 0x157f8
movq %r13, %rdi
leaq -0x64(%rbp), %rsi
callq 0xcdb0
movl (%rax), %edi
movl $0x12, %esi
callq 0x2310
movl $0xe, %edi
leaq 0x990(%rip), %rsi       # 0x8434
callq 0x22b0
movq %r12, %rdi
callq 0x3746
movl %eax, %edi
callq 0x2410
movq -0x90(%rbp), %rdi
cmpq %rbx, %rdi
je 0x7921
callq 0x2250
jmp 0x7921
movl $0x1, %edi
callq 0x21f0
incl %r15d
cmpb $0x1, 0xdec2(%rip)      # 0x159a8
jne 0x78bc
movq 0xd4d5(%rip), %rdi      # 0x14fc8
leaq 0x87ce(%rip), %rsi      # 0x102c8
movl $0xa, %edx
callq 0x22a0
movq 0xd4bd(%rip), %r14      # 0x14fc8
movq %r14, %rdi
movl %r15d, %esi
callq 0x23d0
movq %rax, %rbx
leaq 0x87b3(%rip), %rsi      # 0x102d3
movl $0x2, %edx
movq %rax, %rdi
callq 0x22a0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2260
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2190
movl $0x2, %edi
callq 0x21f0
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x2260
movsbl %al, %esi
movq 0xd44c(%rip), %rbx      # 0x14fc8
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2190
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2260
movsbl %al, %esi
movq 0xd41e(%rip), %rdi      # 0x14fc8
callq 0x2040
movq %rax, %rdi
callq 0x2190
leaq 0xdd2a(%rip), %rsi      # 0x158e8
leaq -0x230(%rbp), %rdi
callq 0xcc34
leaq -0x230(%rbp), %rsi
movq -0x38(%rbp), %rdi
callq 0x7eca
leaq -0x230(%rbp), %rdi
callq 0x336e
leaq -0x130(%rbp), %rdi
callq 0x336e
leaq -0xe0(%rbp), %rdi
callq 0x336e
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xd0, %edi
callq 0x2280
movq %rax, %r14
leaq -0x178(%rbp), %r12
movq %r12, -0x10(%r12)
movq -0x90(%rbp), %rsi
movq -0x88(%rbp), %rdx
addq %rsi, %rdx
leaq -0x188(%rbp), %rdi
callq 0x37c8
movq 0x3e(%rbx), %rax
leaq -0x188(%rbp), %rsi
movq %rax, 0x4e(%rsi)
movups 0x10(%rbx), %xmm0
movups 0x20(%rbx), %xmm1
movups 0x30(%rbx), %xmm2
movups %xmm2, 0x40(%rsi)
movups %xmm1, 0x30(%rsi)
movups %xmm0, 0x20(%rsi)
movq %r14, %rdi
callq 0x3a52
movq -0x188(%rbp), %rdi
cmpq %r12, %rdi
je 0x7c86
callq 0x2250
movq 0xd33b(%rip), %rdi      # 0x14fc8
leaq 0x8642(%rip), %rsi      # 0x102d6
movl $0x1e, %edx
callq 0x22a0
callq 0x2340
movq 0xd31e(%rip), %rdi      # 0x14fc8
movl %eax, %esi
callq 0x23d0
movq %rax, %rdi
callq 0x2120
xorl %edi, %edi
callq 0x22c0
movq %rax, %r15
leaq -0x230(%rbp), %rdi
callq 0x336e
jmp 0x7d3f
movq %rax, %r15
jmp 0x7d4b
jmp 0x7d24
movq %rax, %r15
movq -0x188(%rbp), %rdi
cmpq %r12, %rdi
je 0x7d04
callq 0x2250
jmp 0x7d04
movq %rax, %r15
movq -0x1e0(%rbp), %rdi
cmpq %r12, %rdi
jne 0x7d3a
jmp 0x7d3f
jmp 0x7d24
movq %rax, %r15
movq %r14, %rdi
callq 0x2250
jmp 0x7d2e
jmp 0x7d24
jmp 0x7d24
jmp 0x7d24
jmp 0x7d24
jmp 0x7d24
jmp 0x7d2b
jmp 0x7d24
jmp 0x7d2b
jmp 0x7d24
jmp 0x7d24
jmp 0x7d24
movq %rax, %r15
jmp 0x7d3f
jmp 0x7d2b
movq %rax, %r15
movq -0x90(%rbp), %rdi
cmpq %rbx, %rdi
je 0x7d3f
callq 0x2250
leaq -0x130(%rbp), %rdi
callq 0x336e
leaq -0xe0(%rbp), %rdi
callq 0x336e
movq %r15, %rdi
callq 0x23e0