// File: /workspace/github_cpp_makefile_star2_O3/MUST-Quick-Fry[P]Process-Scheduler/scheduler.cpp
// Function: Project::Scheduler::driveSJF1()
void Scheduler::driveSJF1()
{
    // create a scheduler
    priority_queue<Job,vector<Job>,greater<Job> > wait_q; 
    queue<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);
        }
    }
        
    sort(job_queue.begin(), job_queue.end(), [](const Job &a, const Job &b)->bool
    {   if(a.get_arr_time() == b.get_arr_time()){return a.get_dur_time() < b.get_dur_time();}
        else{return a.get_arr_time() < b.get_arr_time();}
    });
    
    for(int i = 0; i < len; ++i){
        pq_arr.emplace(job_queue[i]);  
    }
    
    time = pq_arr.front().get_arr_time();
    
    while(true){
    
        if(!wait_q.empty()){
            auto it = wait_q.top();
            wait_q.pop();
            time += it.get_dur_time();
            scheduler.emplace(it);    
        }
    
        if(!pq_arr.empty()){
            auto it = pq_arr.front();
            pq_arr.pop();
            time += it.get_dur_time();
            scheduler.emplace(it);
        
            while(!pq_arr.empty() && pq_arr.front().get_arr_time()<=time){
                wait_q.emplace(pq_arr.front());
                pq_arr.pop();
            }
        }
        if(pq_arr.empty() && wait_q.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;
                        
                         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 $0x1d8, %rsp            # imm = 0x1D8
movq %rdi, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, -0x50(%rbp)
movq $0x0, -0x40(%rbp)
leaq -0x100(%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
movq %rbx, -0x58(%rbp)
movslq 0x58(%rbx), %rax
movq %rax, -0x30(%rbp)
testq %rax, %rax
jle 0x5c20
leaq -0x1a0(%rbp), %r13
imulq $0x58, -0x30(%rbp), %r15
xorl %r12d, %r12d
leaq -0x1b0(%rbp), %rbx
movq 0xfcb9(%rip), %rdi      # 0x15830
addq %r12, %rdi
callq 0x3762
cmpl $-0x1, %eax
jne 0x5c13
movl $0x0, -0xb0(%rbp)
movq 0xfc97(%rip), %r14      # 0x15830
movq %r13, -0x1b0(%rbp)
movq (%r14,%r12), %rsi
movq 0x8(%r14,%r12), %rdx
addq %rsi, %rdx
movq %rbx, %rdi
callq 0x37e4
movq 0x4e(%r14,%r12), %rax
movq %rax, 0x3e(%r13)
movups 0x20(%r14,%r12), %xmm0
movups 0x30(%r14,%r12), %xmm1
movups 0x40(%r14,%r12), %xmm2
movups %xmm2, 0x30(%r13)
movups %xmm1, 0x20(%r13)
movups %xmm0, 0x10(%r13)
movq %rbx, %rsi
leaq -0xb0(%rbp), %rdx
callq 0x51d6
movq -0x1b0(%rbp), %rdi
cmpq %r13, %rdi
je 0x5bfe
callq 0x2250
movq 0xfc2b(%rip), %rdi      # 0x15830
addq %r12, %rdi
movl -0xb0(%rbp), %esi
callq 0x3718
addq $0x58, %r12
cmpq %r12, %r15
jne 0x5b70
movq 0xfc09(%rip), %r14      # 0x15830
movq 0xfc0a(%rip), %rbx      # 0x15838
cmpq %rbx, %r14
movq -0x30(%rbp), %r12
je 0x5ca3
movq %rbx, %r15
subq %r14, %r15
movq %r15, %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 %r14, %rdi
movq %rbx, %rsi
callq 0x86c6
cmpq $0x581, %r15            # imm = 0x581
jl 0x5c98
leaq 0x580(%r14), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x92a0
cmpq %rbx, %r15
je 0x5ca3
movq %r15, %rdi
callq 0x945f
addq $0x58, %r15
jmp 0x5c85
movq %r14, %rdi
movq %rbx, %rsi
callq 0x92a0
testl %r12d, %r12d
jle 0x5cd1
imulq $0x58, %r12, %r14
xorl %r15d, %r15d
leaq -0x100(%rbp), %rbx
movq 0xfb73(%rip), %rsi      # 0x15830
addq %r15, %rsi
movq %rbx, %rdi
callq 0xe7e0
addq $0x58, %r15
cmpq %r15, %r14
jne 0x5cb6
movq -0xf0(%rbp), %rdi
callq 0x3758
movl %eax, %r13d
leaq -0xa0(%rbp), %rbx
leaq -0xb0(%rbp), %rdi
leaq -0x100(%rbp), %r14
leaq -0x50(%rbp), %r12
movq -0x50(%rbp), %r15
cmpq -0x48(%rbp), %r15
je 0x5d87
movq %rbx, -0xb0(%rbp)
movq (%r15), %rsi
movq 0x8(%r15), %rdx
addq %rsi, %rdx
callq 0x37e4
movq 0x4e(%r15), %rax
movq %rax, 0x3e(%rbx)
movups 0x20(%r15), %xmm0
movups 0x30(%r15), %xmm1
movups 0x40(%r15), %xmm2
movups %xmm2, 0x30(%rbx)
movups %xmm1, 0x20(%rbx)
movups %xmm0, 0x10(%rbx)
movq %r12, %rdi
callq 0xa62c
leaq -0xb0(%rbp), %rdi
callq 0x3762
movl %eax, %r15d
leaq 0xfaea(%rip), %rdi      # 0x15848
leaq -0xb0(%rbp), %rsi
callq 0xe7e0
movq -0xb0(%rbp), %rdi
cmpq %rbx, %rdi
je 0x5d7b
callq 0x2250
addl %r13d, %r15d
leaq -0xb0(%rbp), %rdi
jmp 0x5d8a
movl %r13d, %r15d
movq -0xf0(%rbp), %r13
cmpq %r13, -0xd0(%rbp)
je 0x5e91
movl %r15d, -0x30(%rbp)
movq %r14, %r12
movq %rbx, -0xb0(%rbp)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
addq %rsi, %rdx
movq %rdi, %r14
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)
movq %rbx, %r15
movups %xmm0, 0x10(%rbx)
movq %r12, %rdi
callq 0xcdea
movq %r14, %rdi
callq 0x3762
movl %eax, %r13d
leaq 0xfa49(%rip), %rdi      # 0x15848
movq %r14, %rsi
callq 0xe7e0
addl -0x30(%rbp), %r13d
movq -0xf0(%rbp), %rdi
cmpq %rdi, -0xd0(%rbp)
movq %r12, %r14
leaq -0x50(%rbp), %r12
movq %r15, %rbx
je 0x5e63
callq 0x3758
cmpl %r13d, %eax
jg 0x5e63
movq -0xf0(%rbp), %rsi
movq %r12, %rdi
callq 0xa5aa
movq -0x50(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0xee0b
movq %r14, %rdi
callq 0xcdea
movq -0xf0(%rbp), %rdi
cmpq %rdi, -0xd0(%rbp)
jne 0x5e25
movq -0xb0(%rbp), %rdi
cmpq %rbx, %rdi
je 0x5e74
callq 0x2250
movq -0xd0(%rbp), %rax
cmpq -0xf0(%rbp), %rax
leaq -0xb0(%rbp), %rdi
jne 0x5cf9
jmp 0x5e94
movl %r15d, %r13d
movq -0x50(%rbp), %rax
cmpq -0x48(%rbp), %rax
jne 0x5cf9
movq -0x58(%rbp), %rax
movl %r13d, 0x5c(%rax)
leaq 0xfa37(%rip), %rdi      # 0x158e8
leaq 0xf990(%rip), %rsi      # 0x15848
callq 0xa71e
movq 0xf104(%rip), %rdi      # 0x14fc8
movq (%rdi), %rax
addq -0x18(%rax), %rdi
movl $0xa, %esi
callq 0x2260
movsbl %al, %esi
movq 0xf0e9(%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 0xf0bb(%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 0xf08d(%rip), %rdi      # 0x14fc8
callq 0x2040
movq %rax, %rdi
callq 0x2190
movq 0xf079(%rip), %r14      # 0x14fc8
leaq 0xa3ea(%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 0xf047(%rip), %rdi      # 0x14fc8
callq 0x2040
movq %rax, %rdi
callq 0x2190
movq 0xf033(%rip), %r14      # 0x14fc8
leaq 0xa310(%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 0xf001(%rip), %rdi      # 0x14fc8
callq 0x2040
movq %rax, %rdi
callq 0x2190
movq 0xefed(%rip), %r14      # 0x14fc8
leaq 0xa35e(%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 0xefbb(%rip), %r15      # 0x14fc8
movq %r15, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2190
xorl %r14d, %r14d
cmpb $0x0, 0xf981(%rip)      # 0x159a8
jne 0x6254
xorl %r14d, %r14d
leaq -0xb0(%rbp), %r13
movl $0xa, %edx
movq %r15, %rdi
leaq 0xa282(%rip), %rsi      # 0x102c8
callq 0x22a0
movq %r15, %rdi
movl %r14d, %esi
callq 0x23d0
movq %rax, %r12
movl $0x2, %edx
movq %rax, %rdi
leaq 0xa26b(%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 0xf7bc(%rip), %rdi      # 0x15858
cmpq %rdi, 0xf7d5(%rip)      # 0x15878
je 0x623a
callq 0x3758
cmpl %eax, %r14d
jl 0x623a
movq 0xf79a(%rip), %r12      # 0x15858
movq %rbx, -0xb0(%rbp)
movq (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
movq %r13, %rdi
callq 0x37e4
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 0xf741(%rip), %rdi      # 0x15848
callq 0xcdea
leaq 0xf85d(%rip), %rdi      # 0x15970
leaq -0x84(%rbp), %rsi
callq 0xcd2a
cmpl $0x0, (%rax)
jne 0x614a
callq 0x20a0
testl %eax, %eax
je 0x6375
movl %eax, %r12d
leaq 0xf835(%rip), %rdi      # 0x15970
leaq -0x84(%rbp), %rsi
callq 0xcd2a
movl %r12d, (%rax)
cmpb $0x0, 0xf858(%rip)      # 0x159a9
je 0x61be
leaq 0xf73e(%rip), %rdi      # 0x15898
movq %r13, %rsi
callq 0xe7e0
leaq 0xf807(%rip), %rdi      # 0x15970
leaq -0x84(%rbp), %rsi
callq 0xcd2a
movl (%rax), %edi
movl $0x14, %esi
callq 0x2310
movl $0x15, %edx
movq %r15, %rdi
leaq 0xa165(%rip), %rsi      # 0x102f5
callq 0x22a0
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x2260
movsbl %al, %esi
movq %r15, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2190
jmp 0x6220
movb $0x1, 0xf7e4(%rip)      # 0x159a9
leaq 0xf60c(%rip), %rdi      # 0x157d8
movq %r13, %rsi
callq 0x2150
movq 0x3e(%rbx), %rax
movq %rax, 0xf647(%rip)      # 0x15826
movups 0x10(%rbx), %xmm0
movups 0x20(%rbx), %xmm1
movups 0x30(%rbx), %xmm2
movups %xmm2, 0xf626(%rip)     # 0x15818
movups %xmm1, 0xf60f(%rip)     # 0x15808
movups %xmm0, 0xf5f8(%rip)     # 0x157f8
movl $0xe, %edi
leaq 0x1af6(%rip), %rsi      # 0x7d02
callq 0x22b0
movq %r13, %rdi
callq 0x3762
movl %eax, %edi
callq 0x2410
movq -0xb0(%rbp), %rdi
cmpq %rbx, %rdi
je 0x6095
callq 0x2250
jmp 0x6095
movl $0x1, %edi
callq 0x21f0
incl %r14d
cmpb $0x1, 0xf75a(%rip)      # 0x159a8
jne 0x6037
movq 0xed6d(%rip), %rdi      # 0x14fc8
leaq 0xa066(%rip), %rsi      # 0x102c8
movl $0xa, %edx
callq 0x22a0
movq 0xed55(%rip), %rbx      # 0x14fc8
movq %rbx, %rdi
movl %r14d, %esi
callq 0x23d0
movq %rax, %r14
leaq 0xa04b(%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 0xece4(%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 0xecb6(%rip), %rdi      # 0x14fc8
callq 0x2040
movq %rax, %rdi
callq 0x2190
leaq 0xf5c2(%rip), %rsi      # 0x158e8
leaq -0x200(%rbp), %rdi
callq 0xcbaa
leaq -0x200(%rbp), %rsi
movq -0x58(%rbp), %rdi
callq 0x7e6e
leaq -0x200(%rbp), %rdi
callq 0x3378
leaq -0x100(%rbp), %rdi
callq 0x3378
leaq -0x50(%rbp), %rdi
callq 0x32fe
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xd0, %edi
callq 0x2280
movq %rax, %r14
leaq -0x148(%rbp), %r15
movq %r15, -0x10(%r15)
movq -0xb0(%rbp), %rsi
movq -0xa8(%rbp), %rdx
addq %rsi, %rdx
leaq -0x158(%rbp), %rdi
callq 0x37e4
movq 0x3e(%rbx), %rax
leaq -0x158(%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 -0x158(%rbp), %rdi
cmpq %r15, %rdi
je 0x63ea
callq 0x2250
movq 0xebd7(%rip), %rdi      # 0x14fc8
leaq 0x9ede(%rip), %rsi      # 0x102d6
movl $0x1e, %edx
callq 0x22a0
callq 0x2340
movq 0xebba(%rip), %rdi      # 0x14fc8
movl %eax, %esi
callq 0x23d0
movq %rax, %rdi
callq 0x2120
xorl %edi, %edi
callq 0x22c0
movq %rax, %r12
leaq -0x200(%rbp), %rdi
callq 0x3378
jmp 0x64a9
movq %rax, %r12
jmp 0x64b5
jmp 0x648e
movq %rax, %r12
movq -0x158(%rbp), %rdi
cmpq %r15, %rdi
je 0x6468
callq 0x2250
jmp 0x6468
movq %rax, %r12
movq -0x1b0(%rbp), %rdi
cmpq %r13, %rdi
jne 0x64a4
jmp 0x64a9
jmp 0x648e
movq %rax, %r12
movq %r14, %rdi
callq 0x2250
jmp 0x6498
jmp 0x648e
jmp 0x648e
jmp 0x648e
jmp 0x648e
jmp 0x648e
jmp 0x648e
jmp 0x6495
movq %rax, %r12
movq %r15, %rbx
jmp 0x6498
jmp 0x6495
jmp 0x648e
jmp 0x648e
movq %rax, %r12
jmp 0x64a9
jmp 0x6495
movq %rax, %r12
movq -0xb0(%rbp), %rdi
cmpq %rbx, %rdi
je 0x64a9
callq 0x2250
leaq -0x100(%rbp), %rdi
callq 0x3378
leaq -0x50(%rbp), %rdi
callq 0x32fe
movq %r12, %rdi
callq 0x23e0