// File: /workspace/github_cpp_makefile_star2_O3/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 0x356c
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 0x356c
movq %rbx, -0x38(%rbp)
movslq 0x58(%rbx), %rax
movq %rax, -0x30(%rbp)
testq %rax, %rax
jle 0x7394
leaq -0x1d0(%rbp), %r12
imulq $0x58, -0x30(%rbp), %r13
xorl %r15d, %r15d
leaq -0x1e0(%rbp), %rbx
movq 0xe549(%rip), %rdi      # 0x15830
addq %r15, %rdi
callq 0x3762
cmpl $-0x1, %eax
jne 0x7387
movl $0x0, -0x90(%rbp)
movq 0xe527(%rip), %r14      # 0x15830
movq %r12, -0x1e0(%rbp)
movq (%r14,%r15), %rsi
movq 0x8(%r14,%r15), %rdx
addq %rsi, %rdx
movq %rbx, %rdi
callq 0x37e4
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 0x51d6
movq -0x1e0(%rbp), %rdi
cmpq %r12, %rdi
je 0x7372
callq 0x2250
movq 0xe4b7(%rip), %rdi      # 0x15830
addq %r15, %rdi
movl -0x90(%rbp), %esi
callq 0x3718
addq $0x58, %r15
cmpq %r15, %r13
jne 0x72e0
movq 0xe495(%rip), %rbx      # 0x15830
movq 0xe496(%rip), %r14      # 0x15838
cmpq %r14, %rbx
je 0x73e2
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 0xd8e9
movq %rbx, %rdi
movq %r14, %rsi
callq 0xd9c6
movq -0x30(%rbp), %rax
testl %eax, %eax
jle 0x7413
imulq $0x58, %rax, %r14
xorl %r15d, %r15d
leaq -0xe0(%rbp), %rbx
movq 0xe431(%rip), %rsi      # 0x15830
addq %r15, %rsi
movq %rbx, %rdi
callq 0xe7e0
addq $0x58, %r15
cmpq %r15, %r14
jne 0x73f8
leaq -0x80(%rbp), %rbx
xorl %r14d, %r14d
leaq -0x90(%rbp), %r15
xorl %r12d, %r12d
movq -0xd0(%rbp), %rdi
cmpq %rdi, -0xb0(%rbp)
je 0x7557
callq 0x3758
cmpl %eax, %r14d
jne 0x7557
movq -0xd0(%rbp), %r13
movq %rbx, -0x90(%rbp)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
addq %rsi, %rdx
movq %r15, %rdi
callq 0x37e4
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 0xcdea
movq %r15, %rdi
callq 0x3762
cmpl $0x3, %eax
jl 0x7518
movq %r15, %rdi
callq 0x3762
movl %eax, -0x60(%rbp)
leaq 0xe393(%rip), %rdi      # 0x15848
movq %r15, %rsi
callq 0xe7e0
movq 0xe3b4(%rip), %rdi      # 0x15878
cmpq 0xe3b5(%rip), %rdi      # 0x15880
jne 0x74e0
movq 0xe3bc(%rip), %rax      # 0x15890
movq -0x8(%rax), %rdi
movl $0x1b8, %eax            # imm = 0x1B8
addq %rax, %rdi
addq $-0x58, %rdi
movl $0x2, %esi
callq 0x3718
movq %r15, %rdi
callq 0x3762
addl $-0x2, %eax
movq %r15, %rdi
movl %eax, %esi
callq 0x3718
leaq -0x130(%rbp), %rdi
movq %r15, %rsi
callq 0xe7e0
addl $0x2, %r12d
jmp 0x753d
movq %r15, %rdi
callq 0x3762
movl %eax, -0x60(%rbp)
leaq 0xe31e(%rip), %rdi      # 0x15848
movq %r15, %rsi
callq 0xe7e0
movq %r15, %rdi
callq 0x3762
addl %eax, %r12d
movq -0x90(%rbp), %rdi
cmpq %rbx, %rdi
je 0x7424
callq 0x2250
jmp 0x7424
movq -0x120(%rbp), %r13
movq -0x100(%rbp), %rax
testl %r12d, %r12d
jne 0x769b
cmpq %r13, %rax
je 0x764f
movq %rbx, -0x90(%rbp)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
addq %rsi, %rdx
movq %r15, %rdi
callq 0x37e4
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 -0x130(%rbp), %rdi
callq 0xcdea
movq %r15, %rdi
movl %r14d, %esi
callq 0x370e
movq %r15, %rdi
callq 0x3762
cmpl $0x3, %eax
jl 0x7657
movq %r15, %rdi
callq 0x3762
movl %eax, -0x60(%rbp)
leaq 0xe25e(%rip), %rdi      # 0x15848
movq %r15, %rsi
callq 0xe7e0
movq 0xe27f(%rip), %rdi      # 0x15878
cmpq 0xe280(%rip), %rdi      # 0x15880
jne 0x7615
movq 0xe287(%rip), %rax      # 0x15890
movq -0x8(%rax), %rdi
movl $0x1b8, %eax            # imm = 0x1B8
addq %rax, %rdi
addq $-0x58, %rdi
movl $0x2, %esi
callq 0x3718
movq %r15, %rdi
callq 0x3762
addl $-0x2, %eax
movq %r15, %rdi
movl %eax, %esi
callq 0x3718
movl $0x2, %r12d
leaq -0x130(%rbp), %rdi
movq %r15, %rsi
callq 0xe7e0
jmp 0x767c
incl %r14d
xorl %r12d, %r12d
jmp 0x76b2
movq %r15, %rdi
callq 0x3762
movl %eax, -0x60(%rbp)
leaq 0xe1df(%rip), %rdi      # 0x15848
movq %r15, %rsi
callq 0xe7e0
movq %r15, %rdi
callq 0x3762
movl %eax, %r12d
movq -0x90(%rbp), %rdi
cmpq %rbx, %rdi
je 0x768d
callq 0x2250
movq -0x120(%rbp), %r13
movq -0x100(%rbp), %rax
xorl %ecx, %ecx
testl %r12d, %r12d
setg %cl
subl %ecx, %r12d
incl %r14d
cmpq %r13, %rax
jne 0x7424
movq -0xb0(%rbp), %rax
cmpq -0xd0(%rbp), %rax
jne 0x7424
movq -0x38(%rbp), %rax
movl %r14d, 0x5c(%rax)
leaq 0xe213(%rip), %rdi      # 0x158e8
leaq 0xe16c(%rip), %rsi      # 0x15848
callq 0xa71e
movq 0xd8e0(%rip), %rdi      # 0x14fc8
movq (%rdi), %rax
addq -0x18(%rax), %rdi
movl $0xa, %esi
callq 0x2260
movsbl %al, %esi
movq 0xd8c5(%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 0xd897(%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 0xd869(%rip), %rdi      # 0x14fc8
callq 0x2040
movq %rax, %rdi
callq 0x2190
movq 0xd855(%rip), %r14      # 0x14fc8
leaq 0x8bc6(%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 0xd823(%rip), %rdi      # 0x14fc8
callq 0x2040
movq %rax, %rdi
callq 0x2190
movq 0xd80f(%rip), %r14      # 0x14fc8
leaq 0x8aec(%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 0xd7dd(%rip), %rdi      # 0x14fc8
callq 0x2040
movq %rax, %rdi
callq 0x2190
movq 0xd7c9(%rip), %r14      # 0x14fc8
leaq 0x8b3a(%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 0xd797(%rip), %rdi      # 0x14fc8
callq 0x2040
movq %rax, %rdi
callq 0x2190
xorl %r15d, %r15d
cmpb $0x0, 0xe160(%rip)      # 0x159a8
jne 0x7a8f
xorl %r15d, %r15d
leaq -0x90(%rbp), %r12
leaq 0xe111(%rip), %r13      # 0x15970
movl $0xa, %edx
movq 0xd75d(%rip), %rdi      # 0x14fc8
leaq 0x8a56(%rip), %rsi      # 0x102c8
callq 0x22a0
movq 0xd74a(%rip), %rdi      # 0x14fc8
movl %r15d, %esi
callq 0x23d0
movq %rax, %r14
movl $0x2, %edx
movq %rax, %rdi
leaq 0x8a3b(%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 0xdf8d(%rip), %rdi      # 0x15858
cmpq %rdi, 0xdfa6(%rip)      # 0x15878
je 0x7a75
callq 0x3758
cmpl %eax, %r15d
jne 0x7a75
movq 0xdf6b(%rip), %r14      # 0x15858
movq %rbx, -0x90(%rbp)
movq (%r14), %rsi
movq 0x8(%r14), %rdx
addq %rsi, %rdx
movq %r12, %rdi
callq 0x37e4
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 0xdf18(%rip), %rdi      # 0x15848
callq 0xcdea
movq %r13, %rdi
leaq -0x64(%rbp), %rsi
callq 0xcd2a
cmpl $0x0, (%rax)
jne 0x7965
callq 0x20a0
testl %eax, %eax
je 0x7bb3
movl %eax, %r14d
movq %r13, %rdi
leaq -0x64(%rbp), %rsi
callq 0xcd2a
movl %r14d, (%rax)
cmpb $0x0, 0xe03d(%rip)      # 0x159a9
je 0x79e1
leaq 0xdf23(%rip), %rdi      # 0x15898
movq %r12, %rsi
callq 0xe7e0
movq %r13, %rdi
leaq -0x64(%rbp), %rsi
callq 0xcd2a
movl (%rax), %edi
movl $0x14, %esi
callq 0x2310
movl $0x15, %edx
movq 0xd627(%rip), %rdi      # 0x14fc8
leaq 0x894d(%rip), %rsi      # 0x102f5
callq 0x22a0
movq 0xd614(%rip), %rcx      # 0x14fc8
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x2260
movsbl %al, %esi
movq 0xd5f6(%rip), %rdi      # 0x14fc8
callq 0x2040
movq %rax, %rdi
callq 0x2190
jmp 0x7a5b
movb $0x1, 0xdfc1(%rip)      # 0x159a9
leaq 0xdde9(%rip), %rdi      # 0x157d8
movq %r12, %rsi
callq 0x2150
movq 0x3e(%rbx), %rax
movq %rax, 0xde24(%rip)      # 0x15826
movups 0x10(%rbx), %xmm0
movups 0x20(%rbx), %xmm1
movups 0x30(%rbx), %xmm2
movups %xmm2, 0xde03(%rip)     # 0x15818
movups %xmm1, 0xddec(%rip)     # 0x15808
movups %xmm0, 0xddd5(%rip)     # 0x157f8
movq %r13, %rdi
leaq -0x64(%rbp), %rsi
callq 0xcd2a
movl (%rax), %edi
movl $0x12, %esi
callq 0x2310
movl $0xe, %edi
leaq 0x98b(%rip), %rsi       # 0x83d2
callq 0x22b0
movq %r12, %rdi
callq 0x3762
movl %eax, %edi
callq 0x2410
movq -0x90(%rbp), %rdi
cmpq %rbx, %rdi
je 0x78c4
callq 0x2250
jmp 0x78c4
movl $0x1, %edi
callq 0x21f0
incl %r15d
cmpb $0x1, 0xdf1f(%rip)      # 0x159a8
jne 0x785f
movq 0xd532(%rip), %rdi      # 0x14fc8
leaq 0x882b(%rip), %rsi      # 0x102c8
movl $0xa, %edx
callq 0x22a0
movq 0xd51a(%rip), %r14      # 0x14fc8
movq %r14, %rdi
movl %r15d, %esi
callq 0x23d0
movq %rax, %rbx
leaq 0x8810(%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 0xd4a9(%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 0xd47b(%rip), %rdi      # 0x14fc8
callq 0x2040
movq %rax, %rdi
callq 0x2190
leaq 0xdd87(%rip), %rsi      # 0x158e8
leaq -0x230(%rbp), %rdi
callq 0xcbaa
leaq -0x230(%rbp), %rsi
movq -0x38(%rbp), %rdi
callq 0x7e6e
leaq -0x230(%rbp), %rdi
callq 0x3378
leaq -0x130(%rbp), %rdi
callq 0x3378
leaq -0xe0(%rbp), %rdi
callq 0x3378
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 0x37e4
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 0x3a72
movq -0x188(%rbp), %rdi
cmpq %r12, %rdi
je 0x7c29
callq 0x2250
movq 0xd398(%rip), %rdi      # 0x14fc8
leaq 0x869f(%rip), %rsi      # 0x102d6
movl $0x1e, %edx
callq 0x22a0
callq 0x2340
movq 0xd37b(%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 0x3378
jmp 0x7ce2
movq %rax, %r15
jmp 0x7cee
jmp 0x7cc7
movq %rax, %r15
movq -0x188(%rbp), %rdi
cmpq %r12, %rdi
je 0x7ca7
callq 0x2250
jmp 0x7ca7
movq %rax, %r15
movq -0x1e0(%rbp), %rdi
cmpq %r12, %rdi
jne 0x7cdd
jmp 0x7ce2
jmp 0x7cc7
movq %rax, %r15
movq %r14, %rdi
callq 0x2250
jmp 0x7cd1
jmp 0x7cc7
jmp 0x7cc7
jmp 0x7cc7
jmp 0x7cc7
jmp 0x7cc7
jmp 0x7cce
jmp 0x7cce
jmp 0x7cc7
jmp 0x7cc7
jmp 0x7cc7
jmp 0x7cc7
movq %rax, %r15
jmp 0x7ce2
jmp 0x7cce
movq %rax, %r15
movq -0x90(%rbp), %rdi
cmpq %rbx, %rdi
je 0x7ce2
callq 0x2250
leaq -0x130(%rbp), %rdi
callq 0x3378
leaq -0xe0(%rbp), %rdi
callq 0x3378
movq %r15, %rdi
callq 0x23e0