// File: /workspace/github_cpp_makefile_star2_O1/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
movaps %xmm0, -0x50(%rbp)
movq $0x0, -0x40(%rbp)
movq %rdi, -0x58(%rbp)
movslq 0x58(%rdi), %rax
movq %rax, -0x30(%rbp)
testq %rax, %rax
jle 0x544f
leaq -0xf8(%rbp), %r13
imulq $0x58, -0x30(%rbp), %r12
xorl %r14d, %r14d
leaq -0x50(%rbp), %r15
movq 0x104a4(%rip), %rdi     # 0x15830
addq %r14, %rdi
callq 0x3746
cmpl $-0x1, %eax
jne 0x5430
movl $0x0, -0xb0(%rbp)
movq 0x10482(%rip), %rbx     # 0x15830
movq %r13, -0x108(%rbp)
movq (%rbx,%r14), %rsi
movq 0x8(%rbx,%r14), %rdx
addq %rsi, %rdx
leaq -0x108(%rbp), %rdi
callq 0x37c8
movq 0x4e(%rbx,%r14), %rax
movq %rax, 0x3e(%r13)
movups 0x20(%rbx,%r14), %xmm0
movups 0x30(%rbx,%r14), %xmm1
movups 0x40(%rbx,%r14), %xmm2
movups %xmm2, 0x30(%r13)
movups %xmm1, 0x20(%r13)
movups %xmm0, 0x10(%r13)
leaq -0x108(%rbp), %rsi
leaq -0xb0(%rbp), %rdx
callq 0x521a
movq -0x108(%rbp), %rdi
cmpq %r13, %rdi
je 0x541b
callq 0x2250
movq 0x1040e(%rip), %rdi     # 0x15830
addq %r14, %rdi
movl -0xb0(%rbp), %esi
callq 0x36fc
movq 0x103f9(%rip), %rsi     # 0x15830
addq %r14, %rsi
movq %r15, %rdi
callq 0xa656
addq $0x58, %r14
cmpq %r14, %r12
jne 0x5385
movq -0x50(%rbp), %rbx
movq -0x48(%rbp), %r14
cmpq %r14, %rbx
je 0x5497
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 -0x50(%rbp), %rdi
callq 0x373c
movl %eax, %ebx
leaq -0xa0(%rbp), %r15
movq -0x30(%rbp), %rax
testl %eax, %eax
jle 0x554c
imulq $0x58, %rax, %rax
movq %rax, -0x30(%rbp)
xorl %r13d, %r13d
leaq -0xb0(%rbp), %r14
movq -0x50(%rbp), %r12
movq %r15, -0xb0(%rbp)
movq (%r12,%r13), %rsi
movq 0x8(%r12,%r13), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x37c8
movq 0x4e(%r12,%r13), %rax
movq %rax, 0x3e(%r15)
movups 0x20(%r12,%r13), %xmm0
movups 0x30(%r12,%r13), %xmm1
movups 0x40(%r12,%r13), %xmm2
movups %xmm2, 0x30(%r15)
movups %xmm1, 0x20(%r15)
movups %xmm0, 0x10(%r15)
movq %r14, %rdi
callq 0x3746
movl %eax, %r12d
leaq 0x10326(%rip), %rdi     # 0x15848
movq %r14, %rsi
callq 0xe8c8
movq -0xb0(%rbp), %rdi
cmpq %r15, %rdi
je 0x553b
callq 0x2250
addl %r12d, %ebx
addq $0x58, %r13
cmpq %r13, -0x30(%rbp)
jne 0x54c7
movq -0x58(%rbp), %rax
movl %ebx, 0x5c(%rax)
leaq 0x1038e(%rip), %rdi     # 0x158e8
leaq 0x102e7(%rip), %rsi     # 0x15848
callq 0xa7ca
movq 0xfa5b(%rip), %rdi      # 0x14fc8
movq (%rdi), %rax
addq -0x18(%rax), %rdi
movl $0xa, %esi
callq 0x2260
movsbl %al, %esi
movq 0xfa40(%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 0xfa12(%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 0xf9e4(%rip), %rdi      # 0x14fc8
callq 0x2040
movq %rax, %rdi
callq 0x2190
movq 0xf9d0(%rip), %rbx      # 0x14fc8
leaq 0xad41(%rip), %rsi      # 0x10340
movl $0x33, %edx
movq %rbx, %rdi
callq 0x22a0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2260
movsbl %al, %esi
movq 0xf99e(%rip), %rdi      # 0x14fc8
callq 0x2040
movq %rax, %rdi
callq 0x2190
movq 0xf98a(%rip), %rbx      # 0x14fc8
leaq 0xac67(%rip), %rsi      # 0x102ac
movl $0x1b, %edx
movq %rbx, %rdi
callq 0x22a0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2260
movsbl %al, %esi
movq 0xf958(%rip), %rdi      # 0x14fc8
callq 0x2040
movq %rax, %rdi
callq 0x2190
movq 0xf944(%rip), %rbx      # 0x14fc8
leaq 0xacb5(%rip), %rsi      # 0x10340
movl $0x33, %edx
movq %rbx, %rdi
callq 0x22a0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2260
movsbl %al, %esi
movq 0xf912(%rip), %rbx      # 0x14fc8
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2190
xorl %r14d, %r14d
cmpb $0x0, 0x102d8(%rip)     # 0x159a8
jne 0x5903
xorl %r14d, %r14d
leaq -0xb0(%rbp), %r13
movl $0xa, %edx
movq %rbx, %rdi
leaq 0xabd9(%rip), %rsi      # 0x102c8
callq 0x22a0
movq %rbx, %rdi
movl %r14d, %esi
callq 0x23d0
movq %rax, %r12
movl $0x2, %edx
movq %rax, %rdi
leaq 0xabc2(%rip), %rsi      # 0x102d3
callq 0x22a0
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x2260
movsbl %al, %esi
movq %r12, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2190
movq 0x10113(%rip), %rdi     # 0x15858
cmpq %rdi, 0x1012c(%rip)     # 0x15878
je 0x58e9
callq 0x373c
cmpl %eax, %r14d
jl 0x58e9
movq 0x100f1(%rip), %r12     # 0x15858
movq %r15, -0xb0(%rbp)
movq (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
movq %r13, %rdi
callq 0x37c8
movq 0x4e(%r12), %rax
movq %rax, 0x3e(%r15)
movups 0x20(%r12), %xmm0
movups 0x30(%r12), %xmm1
movups 0x40(%r12), %xmm2
movups %xmm2, 0x30(%r15)
movups %xmm1, 0x20(%r15)
movups %xmm0, 0x10(%r15)
leaq 0x10095(%rip), %rdi     # 0x15848
callq 0xce58
leaq 0x101b1(%rip), %rdi     # 0x15970
leaq -0x84(%rbp), %rsi
callq 0xcdb0
cmpl $0x0, (%rax)
jne 0x57f6
callq 0x20a0
testl %eax, %eax
je 0x5a18
movl %eax, %r12d
leaq 0x10189(%rip), %rdi     # 0x15970
leaq -0x84(%rbp), %rsi
callq 0xcdb0
movl %r12d, (%rax)
cmpb $0x0, 0x101ac(%rip)     # 0x159a9
je 0x586a
leaq 0x10092(%rip), %rdi     # 0x15898
movq %r13, %rsi
callq 0xe8c8
leaq 0x1015b(%rip), %rdi     # 0x15970
leaq -0x84(%rbp), %rsi
callq 0xcdb0
movl (%rax), %edi
movl $0x14, %esi
callq 0x2310
movl $0x15, %edx
movq %rbx, %rdi
leaq 0xaab9(%rip), %rsi      # 0x102f5
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
jmp 0x58cf
movb $0x1, 0x10138(%rip)     # 0x159a9
leaq 0xff60(%rip), %rdi      # 0x157d8
movq %r13, %rsi
callq 0x2150
movq 0x3e(%r15), %rax
movq %rax, 0xff9b(%rip)      # 0x15826
movups 0x10(%r15), %xmm0
movups 0x20(%r15), %xmm1
movups 0x30(%r15), %xmm2
movups %xmm2, 0xff77(%rip)     # 0x15818
movups %xmm1, 0xff60(%rip)     # 0x15808
movups %xmm0, 0xff49(%rip)     # 0x157f8
movl $0xe, %edi
leaq 0x24a4(%rip), %rsi      # 0x7d5f
callq 0x22b0
movq %r13, %rdi
callq 0x3746
movl %eax, %edi
callq 0x2410
movq -0xb0(%rbp), %rdi
cmpq %r15, %rdi
je 0x573e
callq 0x2250
jmp 0x573e
movl $0x1, %edi
callq 0x21f0
incl %r14d
cmpb $0x1, 0x100ab(%rip)     # 0x159a8
jne 0x56e0
movq 0xf6be(%rip), %rdi      # 0x14fc8
leaq 0xa9b7(%rip), %rsi      # 0x102c8
movl $0xa, %edx
callq 0x22a0
movq 0xf6a6(%rip), %rbx      # 0x14fc8
movq %rbx, %rdi
movl %r14d, %esi
callq 0x23d0
movq %rax, %r14
leaq 0xa99c(%rip), %rsi      # 0x102d3
movl $0x2, %edx
movq %rax, %rdi
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
movl $0x2, %edi
callq 0x21f0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2260
movsbl %al, %esi
movq 0xf635(%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 0xf607(%rip), %rdi      # 0x14fc8
callq 0x2040
movq %rax, %rdi
callq 0x2190
leaq 0xff13(%rip), %rsi      # 0x158e8
leaq -0x1b0(%rbp), %rdi
callq 0xcc34
leaq -0x1b0(%rbp), %rsi
movq -0x58(%rbp), %rdi
callq 0x7eca
leaq -0x1b0(%rbp), %rdi
callq 0x336e
leaq -0x50(%rbp), %rdi
callq 0x32fe
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xd0, %edi
callq 0x2280
movq %rax, %r14
leaq -0x150(%rbp), %r12
movq %r12, -0x10(%r12)
movq -0xb0(%rbp), %rsi
movq -0xa8(%rbp), %rdx
addq %rsi, %rdx
leaq -0x160(%rbp), %rdi
callq 0x37c8
movq 0x3e(%r15), %rax
leaq -0x160(%rbp), %rsi
movq %rax, 0x4e(%rsi)
movups 0x10(%r15), %xmm0
movups 0x20(%r15), %xmm1
movups 0x30(%r15), %xmm2
movups %xmm2, 0x40(%rsi)
movups %xmm1, 0x30(%rsi)
movups %xmm0, 0x20(%rsi)
movq %r14, %rdi
callq 0x3a52
movq -0x160(%rbp), %rdi
cmpq %r12, %rdi
je 0x5a91
callq 0x2250
movq 0xf530(%rip), %rdi      # 0x14fc8
leaq 0xa837(%rip), %rsi      # 0x102d6
movl $0x1e, %edx
callq 0x22a0
callq 0x2340
movq 0xf513(%rip), %rdi      # 0x14fc8
movl %eax, %esi
callq 0x23d0
movq %rax, %rdi
callq 0x2120
xorl %edi, %edi
callq 0x22c0
movq %rax, %rbx
leaq -0x1b0(%rbp), %rdi
callq 0x336e
jmp 0x5b3b
movq %rax, %rbx
movq -0x160(%rbp), %rdi
cmpq %r12, %rdi
je 0x5b08
callq 0x2250
jmp 0x5b08
movq %rax, %rbx
movq -0x108(%rbp), %rdi
cmpq %r13, %rdi
jne 0x5b36
jmp 0x5b3b
jmp 0x5b22
movq %rax, %rbx
movq %r14, %rdi
callq 0x2250
jmp 0x5b2a
jmp 0x5b22
jmp 0x5b22
jmp 0x5b22
jmp 0x5b27
jmp 0x5b27
jmp 0x5b22
jmp 0x5b22
jmp 0x5b22
movq %rax, %rbx
jmp 0x5b3b
movq %rax, %rbx
movq -0xb0(%rbp), %rdi
cmpq %r15, %rdi
je 0x5b3b
callq 0x2250
leaq -0x50(%rbp), %rdi
callq 0x32fe
movq %rbx, %rdi
callq 0x23e0