// File: /workspace/github_cpp_makefile_star2_O3/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)
movq $0x0, 0x10(%rbx)
movq %rdi, -0x30(%rbp)
movl 0x58(%rdi), %eax
leaq -0x150(%rbp), %r12
movq %rax, -0x58(%rbp)
imulq $0x58, %rax, %rax
movq %rax, -0x38(%rbp)
xorl %r15d, %r15d
leaq -0x160(%rbp), %r14
movq 0x104e9(%rip), %rdi     # 0x15830
addq %r15, %rdi
callq 0x3762
cmpl $-0x1, %eax
jne 0x53e8
movl $0x0, -0xb0(%rbp)
movq 0x104c7(%rip), %r13     # 0x15830
movq %r12, -0x160(%rbp)
movq (%r13,%r15), %rsi
movq 0x8(%r13,%r15), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x37e4
movq 0x4e(%r13,%r15), %rax
movq %rax, 0x3e(%r12)
movups 0x20(%r13,%r15), %xmm0
movups 0x30(%r13,%r15), %xmm1
movups 0x40(%r13,%r15), %xmm2
movups %xmm2, 0x30(%r12)
movups %xmm1, 0x20(%r12)
movups %xmm0, 0x10(%r12)
movq %r14, %rsi
leaq -0xb0(%rbp), %rdx
callq 0x51d6
movq -0x160(%rbp), %rdi
cmpq %r12, %rdi
je 0x53d3
callq 0x2250
movq 0x10456(%rip), %rdi     # 0x15830
addq %r15, %rdi
movl -0xb0(%rbp), %esi
callq 0x3718
movq 0x10441(%rip), %rsi     # 0x15830
addq %r15, %rsi
movq %rbx, %rdi
callq 0xa5aa
addq $0x58, %r15
cmpq %r15, -0x38(%rbp)
jne 0x5340
movq -0x50(%rbp), %rbx
movq -0x48(%rbp), %r14
cmpq %r14, %rbx
je 0x5454
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 -0x50(%rbp), %rbx
movq %rbx, %rdi
callq 0x3758
movl %eax, %ebx
leaq -0xa0(%rbp), %r15
cmpl $0x0, -0x58(%rbp)
jle 0x54fe
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 0x37e4
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 0x3762
movl %eax, %r12d
leaq 0x10374(%rip), %rdi     # 0x15848
movq %r14, %rsi
callq 0xe7e0
movq -0xb0(%rbp), %rdi
cmpq %r15, %rdi
je 0x54ed
callq 0x2250
addl %r12d, %ebx
addq $0x58, %r13
cmpq %r13, -0x38(%rbp)
jne 0x5479
movq -0x30(%rbp), %rax
movl %ebx, 0x5c(%rax)
leaq 0x103dc(%rip), %rdi     # 0x158e8
leaq 0x10335(%rip), %rsi     # 0x15848
callq 0xa71e
movq 0xfaa9(%rip), %rdi      # 0x14fc8
movq (%rdi), %rax
addq -0x18(%rax), %rdi
movl $0xa, %esi
callq 0x2260
movsbl %al, %esi
movq 0xfa8e(%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 0xfa60(%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 0xfa32(%rip), %rdi      # 0x14fc8
callq 0x2040
movq %rax, %rdi
callq 0x2190
movq 0xfa1e(%rip), %rbx      # 0x14fc8
leaq 0xad8f(%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 0xf9ec(%rip), %rdi      # 0x14fc8
callq 0x2040
movq %rax, %rdi
callq 0x2190
movq 0xf9d8(%rip), %rbx      # 0x14fc8
leaq 0xacb5(%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 0xf9a6(%rip), %rdi      # 0x14fc8
callq 0x2040
movq %rax, %rdi
callq 0x2190
movq 0xf992(%rip), %rbx      # 0x14fc8
leaq 0xad03(%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 0xf960(%rip), %rbx      # 0x14fc8
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2190
xorl %r14d, %r14d
cmpb $0x0, 0x10326(%rip)     # 0x159a8
jne 0x58b5
xorl %r14d, %r14d
leaq -0xb0(%rbp), %r13
movl $0xa, %edx
movq %rbx, %rdi
leaq 0xac27(%rip), %rsi      # 0x102c8
callq 0x22a0
movq %rbx, %rdi
movl %r14d, %esi
callq 0x23d0
movq %rax, %r12
movl $0x2, %edx
movq %rax, %rdi
leaq 0xac10(%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 0x10161(%rip), %rdi     # 0x15858
cmpq %rdi, 0x1017a(%rip)     # 0x15878
je 0x589b
callq 0x3758
cmpl %eax, %r14d
jl 0x589b
movq 0x1013f(%rip), %r12     # 0x15858
movq %r15, -0xb0(%rbp)
movq (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
movq %r13, %rdi
callq 0x37e4
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 0x100e3(%rip), %rdi     # 0x15848
callq 0xcdea
leaq 0x101ff(%rip), %rdi     # 0x15970
leaq -0x84(%rbp), %rsi
callq 0xcd2a
cmpl $0x0, (%rax)
jne 0x57a8
callq 0x20a0
testl %eax, %eax
je 0x59ca
movl %eax, %r12d
leaq 0x101d7(%rip), %rdi     # 0x15970
leaq -0x84(%rbp), %rsi
callq 0xcd2a
movl %r12d, (%rax)
cmpb $0x0, 0x101fa(%rip)     # 0x159a9
je 0x581c
leaq 0x100e0(%rip), %rdi     # 0x15898
movq %r13, %rsi
callq 0xe7e0
leaq 0x101a9(%rip), %rdi     # 0x15970
leaq -0x84(%rbp), %rsi
callq 0xcd2a
movl (%rax), %edi
movl $0x14, %esi
callq 0x2310
movl $0x15, %edx
movq %rbx, %rdi
leaq 0xab07(%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 0x5881
movb $0x1, 0x10186(%rip)     # 0x159a9
leaq 0xffae(%rip), %rdi      # 0x157d8
movq %r13, %rsi
callq 0x2150
movq 0x3e(%r15), %rax
movq %rax, 0xffe9(%rip)      # 0x15826
movups 0x10(%r15), %xmm0
movups 0x20(%r15), %xmm1
movups 0x30(%r15), %xmm2
movups %xmm2, 0xffc5(%rip)     # 0x15818
movups %xmm1, 0xffae(%rip)     # 0x15808
movups %xmm0, 0xff97(%rip)     # 0x157f8
movl $0xe, %edi
leaq 0x2495(%rip), %rsi      # 0x7d02
callq 0x22b0
movq %r13, %rdi
callq 0x3762
movl %eax, %edi
callq 0x2410
movq -0xb0(%rbp), %rdi
cmpq %r15, %rdi
je 0x56f0
callq 0x2250
jmp 0x56f0
movl $0x1, %edi
callq 0x21f0
incl %r14d
cmpb $0x1, 0x100f9(%rip)     # 0x159a8
jne 0x5692
movq 0xf70c(%rip), %rdi      # 0x14fc8
leaq 0xaa05(%rip), %rsi      # 0x102c8
movl $0xa, %edx
callq 0x22a0
movq 0xf6f4(%rip), %rbx      # 0x14fc8
movq %rbx, %rdi
movl %r14d, %esi
callq 0x23d0
movq %rax, %r14
leaq 0xa9ea(%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 0xf683(%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 0xf655(%rip), %rdi      # 0x14fc8
callq 0x2040
movq %rax, %rdi
callq 0x2190
leaq 0xff61(%rip), %rsi      # 0x158e8
leaq -0x1b0(%rbp), %rdi
callq 0xcbaa
leaq -0x1b0(%rbp), %rsi
movq -0x30(%rbp), %rdi
callq 0x7e6e
leaq -0x1b0(%rbp), %rdi
callq 0x3378
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 -0xf8(%rbp), %r12
movq %r12, -0x10(%r12)
movq -0xb0(%rbp), %rsi
movq -0xa8(%rbp), %rdx
addq %rsi, %rdx
leaq -0x108(%rbp), %rdi
callq 0x37e4
movq 0x3e(%r15), %rax
leaq -0x108(%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 0x3a72
movq -0x108(%rbp), %rdi
cmpq %r12, %rdi
je 0x5a43
callq 0x2250
movq 0xf57e(%rip), %rdi      # 0x14fc8
leaq 0xa885(%rip), %rsi      # 0x102d6
movl $0x1e, %edx
callq 0x22a0
callq 0x2340
movq 0xf561(%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 0x3378
jmp 0x5aed
movq %rax, %rbx
movq -0x108(%rbp), %rdi
cmpq %r12, %rdi
je 0x5aba
callq 0x2250
jmp 0x5aba
movq %rax, %rbx
movq -0x160(%rbp), %rdi
cmpq %r12, %rdi
jne 0x5ae8
jmp 0x5aed
jmp 0x5ad4
movq %rax, %rbx
movq %r14, %rdi
callq 0x2250
jmp 0x5adc
jmp 0x5ad4
jmp 0x5ad4
jmp 0x5ad4
jmp 0x5ad9
jmp 0x5ad9
jmp 0x5ad4
jmp 0x5ad4
jmp 0x5ad4
movq %rax, %rbx
jmp 0x5aed
movq %rax, %rbx
movq -0xb0(%rbp), %rdi
cmpq %r15, %rdi
je 0x5aed
callq 0x2250
leaq -0x50(%rbp), %rdi
callq 0x32fe
movq %rbx, %rdi
callq 0x23e0