// File: /workspace/github_cpp_makefile_star2_O2/MUST-Quick-Fry[P]Process-Scheduler/scheduler.cpp
// Function: Project::Scheduler::driveFIFO()
void Scheduler::driveFIFO()
{
    // create a scheduler
    vector<Job> pq_arr;
    int len = get_job_num();
    int time = 0;

    // 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);
        }

        pq_arr.emplace_back(job_queue[i]);  
    }
    
    sort(pq_arr.begin(), pq_arr.end()); 
    
    time = pq_arr[0].get_arr_time();
    
    for(int i = 0; i < len; ++i){
        auto it = pq_arr[i];
        time += it.get_dur_time();
        scheduler.emplace(it);
    }
   
       
    // 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;
                        
                         signal(SIGALRM, signal_nonpreem);
                         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 $0x188, %rsp            # imm = 0x188
xorps %xmm0, %xmm0
leaq -0x50(%rbp), %rbx
movaps %xmm0, (%rbx)
andq $0x0, 0x10(%rbx)
movq %rdi, -0x30(%rbp)
movl 0x58(%rdi), %eax
xorl %r12d, %r12d
testl %eax, %eax
cmovlel %r12d, %eax
imulq $0x58, %rax, %r13
leaq -0x1a8(%rbp), %r14
leaq -0xa8(%rbp), %r15
cmpq %r12, %r13
je 0x4ab5
movq 0xddc1(%rip), %rdi      # 0x12810
addq %r12, %rdi
callq 0x34be
cmpl $-0x1, %eax
jne 0x4a9d
andl $0x0, -0xa8(%rbp)
movq 0xdda6(%rip), %rsi      # 0x12810
addq %r12, %rsi
movq %r14, %rdi
callq 0x350a
movq %r14, %rsi
movq %r15, %rdx
callq 0x4918
movq %r14, %rdi
callq 0x2100
movq 0xdd81(%rip), %rdi      # 0x12810
addq %r12, %rdi
movl -0xa8(%rbp), %esi
callq 0x3488
movq 0xdd6c(%rip), %rsi      # 0x12810
addq %r12, %rsi
movq %rbx, %rdi
callq 0x7550
addq $0x58, %r12
jmp 0x4a43
movq -0x50(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0xa951
movq -0x50(%rbp), %rdi
callq 0x34b4
movl %eax, %ebx
xorl %r15d, %r15d
leaq -0xa8(%rbp), %r14
cmpq %r15, %r13
je 0x4b16
movq -0x50(%rbp), %rsi
addq %r15, %rsi
movq %r14, %rdi
callq 0x350a
movq %r14, %rdi
callq 0x34be
movl %eax, %r12d
leaq 0xdd2b(%rip), %rdi      # 0x12828
movq %r14, %rsi
callq 0xb0c6
addl %r12d, %ebx
movq %r14, %rdi
callq 0x2100
addq $0x58, %r15
jmp 0x4ad7
movq -0x30(%rbp), %rax
movl %ebx, 0x5c(%rax)
leaq 0xdda4(%rip), %rdi      # 0x128c8
leaq 0xdcfd(%rip), %rsi      # 0x12828
callq 0x76d2
movq 0xd489(%rip), %rdi      # 0x11fc0
callq 0x2110
movq 0xd47d(%rip), %rdi      # 0x11fc0
callq 0x2110
movq 0xd471(%rip), %rdi      # 0x11fc0
callq 0x2110
movq 0xd465(%rip), %rdi      # 0x11fc0
leaq 0x779b(%rip), %rsi      # 0xc2fd
callq 0x2230
movq %rax, %rdi
callq 0x2110
movq 0xd44a(%rip), %rdi      # 0x11fc0
leaq 0x76ec(%rip), %rsi      # 0xc269
callq 0x2230
movq %rax, %rdi
callq 0x2110
movq 0xd42f(%rip), %rdi      # 0x11fc0
leaq 0x7765(%rip), %rsi      # 0xc2fd
callq 0x2230
movq %rax, %rdi
callq 0x2110
xorl %r14d, %r14d
leaq -0x7c(%rbp), %r15
leaq -0xa8(%rbp), %rbx
leaq 0xdd96(%rip), %r12      # 0x12950
cmpb $0x0, 0xddc7(%rip)      # 0x12988
jne 0x4d02
movq 0xd3f2(%rip), %rdi      # 0x11fc0
leaq 0x76b0(%rip), %rsi      # 0xc285
callq 0x2230
movq %rax, %rdi
movl %r14d, %esi
callq 0x23a0
movq %rax, %rdi
leaq 0x76a1(%rip), %rsi      # 0xc290
callq 0x2230
movq %rax, %rdi
callq 0x2110
movq 0xdc35(%rip), %rdi      # 0x12838
cmpq %rdi, 0xdc4e(%rip)      # 0x12858
je 0x4cf2
callq 0x34b4
cmpl %eax, %r14d
jl 0x4cf2
movq 0xdc13(%rip), %rsi      # 0x12838
movq %rbx, %rdi
callq 0x350a
leaq 0xdbf4(%rip), %rdi      # 0x12828
callq 0xa080
movq %r12, %rdi
movq %r15, %rsi
callq 0x9faa
cmpl $0x0, (%rax)
jne 0x4c67
callq 0x2090
testl %eax, %eax
je 0x4da1
movl %eax, %r13d
movq %r12, %rdi
movq %r15, %rsi
callq 0x9faa
movl %r13d, (%rax)
cmpb $0x0, 0xdd1b(%rip)      # 0x12989
je 0x4cb1
leaq 0xdc01(%rip), %rdi      # 0x12878
movq %rbx, %rsi
callq 0xb0c6
movq %r12, %rdi
movq %r15, %rsi
callq 0x9faa
movl (%rax), %edi
pushq $0x14
popq %rsi
callq 0x22d0
movq 0xd325(%rip), %rdi      # 0x11fc0
leaq 0x7610(%rip), %rsi      # 0xc2b2
callq 0x2230
movq %rax, %rdi
callq 0x2110
jmp 0x4ce5
movb $0x1, 0xdcd1(%rip)      # 0x12989
leaq 0xdaf9(%rip), %rdi      # 0x127b8
movq %rbx, %rsi
callq 0x758c
pushq $0xe
popq %rdi
leaq 0x171c(%rip), %rsi      # 0x63ed
callq 0x2260
movq %rbx, %rdi
callq 0x34be
movl %eax, %edi
callq 0x23d0
movq %rbx, %rdi
callq 0x2100
jmp 0x4bfc
pushq $0x1
popq %rdi
callq 0x21c0
incl %r14d
jmp 0x4bba
movq 0xd2b7(%rip), %rdi      # 0x11fc0
leaq 0x7575(%rip), %rsi      # 0xc285
callq 0x2230
movq %rax, %rdi
movl %r14d, %esi
callq 0x23a0
leaq 0x7569(%rip), %rsi      # 0xc290
movq %rax, %rdi
callq 0x2230
movq %rax, %rdi
callq 0x2110
pushq $0x2
popq %rdi
callq 0x21c0
movq 0xd27a(%rip), %rdi      # 0x11fc0
callq 0x2110
movq 0xd26e(%rip), %rdi      # 0x11fc0
callq 0x2110
leaq 0xdb6a(%rip), %rsi      # 0x128c8
leaq -0xf8(%rbp), %rdi
callq 0x9e98
leaq -0xf8(%rbp), %rsi
movq -0x30(%rbp), %rdi
callq 0x64d6
leaq -0xf8(%rbp), %rdi
callq 0x2f36
leaq -0x50(%rbp), %rdi
callq 0x2ea6
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xd0, %edi
callq 0x2240
movq %rax, %r14
leaq -0x150(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x350a
leaq -0x150(%rbp), %rsi
movq %r14, %rdi
callq 0x36ca
leaq -0x150(%rbp), %rdi
callq 0x2100
movq 0xd1dd(%rip), %rdi      # 0x11fc0
leaq 0x74a9(%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 -0xf8(%rbp), %rdi
callq 0x2f36
jmp 0x4e71
jmp 0x4e5d
movq %rax, %rbx
leaq -0x1a8(%rbp), %rdi
jmp 0x4e6c
jmp 0x4e5d
movq %rax, %rbx
leaq -0x150(%rbp), %rdi
callq 0x2100
jmp 0x4e49
jmp 0x4e5d
jmp 0x4e5d
movq %rax, %rbx
movq %r14, %rdi
callq 0x2220
jmp 0x4e65
jmp 0x4e62
jmp 0x4e5d
jmp 0x4e62
jmp 0x4e5d
jmp 0x4e5d
movq %rax, %rbx
jmp 0x4e71
movq %rax, %rbx
leaq -0xa8(%rbp), %rdi
callq 0x2100
leaq -0x50(%rbp), %rdi
callq 0x2ea6
movq %rbx, %rdi
callq 0x23b0