// File: /workspace/github_cpp_makefile_star2_O1/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 0x3550
movq %rbx, -0x58(%rbp)
movslq 0x58(%rbx), %rax
movq %rax, -0x30(%rbp)
testq %rax, %rax
jle 0x5c6e
leaq -0x1a0(%rbp), %r13
imulq $0x58, -0x30(%rbp), %r15
xorl %r12d, %r12d
leaq -0x1b0(%rbp), %rbx
movq 0xfc6b(%rip), %rdi      # 0x15830
addq %r12, %rdi
callq 0x3746
cmpl $-0x1, %eax
jne 0x5c61
movl $0x0, -0xb0(%rbp)
movq 0xfc49(%rip), %r14      # 0x15830
movq %r13, -0x1b0(%rbp)
movq (%r14,%r12), %rsi
movq 0x8(%r14,%r12), %rdx
addq %rsi, %rdx
movq %rbx, %rdi
callq 0x37c8
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 0x521a
movq -0x1b0(%rbp), %rdi
cmpq %r13, %rdi
je 0x5c4c
callq 0x2250
movq 0xfbdd(%rip), %rdi      # 0x15830
addq %r12, %rdi
movl -0xb0(%rbp), %esi
callq 0x36fc
addq $0x58, %r12
cmpq %r12, %r15
jne 0x5bbe
movq 0xfbbb(%rip), %r14      # 0x15830
movq 0xfbbc(%rip), %rbx      # 0x15838
cmpq %rbx, %r14
movq -0x30(%rbp), %r12
je 0x5cf1
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 0x8728
cmpq $0x581, %r15            # imm = 0x581
jl 0x5ce6
leaq 0x580(%r14), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x9317
cmpq %rbx, %r15
je 0x5cf1
movq %r15, %rdi
callq 0x94d6
addq $0x58, %r15
jmp 0x5cd3
movq %r14, %rdi
movq %rbx, %rsi
callq 0x9317
testl %r12d, %r12d
jle 0x5d1f
imulq $0x58, %r12, %r14
xorl %r15d, %r15d
leaq -0x100(%rbp), %rbx
movq 0xfb25(%rip), %rsi      # 0x15830
addq %r15, %rsi
movq %rbx, %rdi
callq 0xe8c8
addq $0x58, %r15
cmpq %r15, %r14
jne 0x5d04
movq -0xf0(%rbp), %rdi
callq 0x373c
movl %eax, %r13d
leaq -0xa0(%rbp), %rbx
leaq -0xb0(%rbp), %rdi
leaq -0x100(%rbp), %r14
movq -0x50(%rbp), %r15
cmpq -0x48(%rbp), %r15
je 0x5dd2
movq %rbx, -0xb0(%rbp)
movq (%r15), %rsi
movq 0x8(%r15), %rdx
addq %rsi, %rdx
callq 0x37c8
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)
leaq -0x50(%rbp), %rdi
callq 0xa6d8
leaq -0xb0(%rbp), %rdi
callq 0x3746
movl %eax, %r15d
leaq 0xfa9f(%rip), %rdi      # 0x15848
leaq -0xb0(%rbp), %rsi
callq 0xe8c8
movq -0xb0(%rbp), %rdi
cmpq %rbx, %rdi
je 0x5dc6
callq 0x2250
addl %r13d, %r15d
leaq -0xb0(%rbp), %rdi
jmp 0x5dd5
movl %r13d, %r15d
movq -0xf0(%rbp), %r13
cmpq %r13, -0xd0(%rbp)
je 0x5ec8
movl %r15d, -0x30(%rbp)
movq %r14, %r15
movq %rbx, -0xb0(%rbp)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
addq %rsi, %rdx
movq %rdi, %r12
callq 0x37c8
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, %r14
movups %xmm0, 0x10(%rbx)
movq %r15, %rdi
callq 0xce58
movq %r12, %rdi
callq 0x3746
movl %eax, %r13d
leaq 0xf9fe(%rip), %rdi      # 0x15848
movq %r12, %rsi
callq 0xe8c8
addl -0x30(%rbp), %r13d
movq -0xf0(%rbp), %rdi
cmpq %rdi, -0xd0(%rbp)
movq %r14, %rbx
movq %r15, %r14
leaq -0x50(%rbp), %r15
je 0x5eae
callq 0x373c
cmpl %r13d, %eax
jg 0x5eae
movq -0xf0(%rbp), %rsi
movq %r15, %rdi
callq 0xa656
movq -0x50(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0xeef3
movq %r14, %rdi
callq 0xce58
movq -0xf0(%rbp), %rdi
cmpq %rdi, -0xd0(%rbp)
jne 0x5e70
movq -0xb0(%rbp), %rdi
cmpq %rbx, %rdi
je 0x5ebf
callq 0x2250
leaq -0xb0(%rbp), %rdi
jmp 0x5ecb
movl %r15d, %r13d
movq -0xd0(%rbp), %rax
cmpq -0xf0(%rbp), %rax
jne 0x5d43
movq -0x50(%rbp), %rax
cmpq -0x48(%rbp), %rax
jne 0x5d43
movq -0x58(%rbp), %rax
movl %r13d, 0x5c(%rax)
leaq 0xf9ec(%rip), %rdi      # 0x158e8
leaq 0xf945(%rip), %rsi      # 0x15848
callq 0xa7ca
movq 0xf0b9(%rip), %rdi      # 0x14fc8
movq (%rdi), %rax
addq -0x18(%rax), %rdi
movl $0xa, %esi
callq 0x2260
movsbl %al, %esi
movq 0xf09e(%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 0xf070(%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 0xf042(%rip), %rdi      # 0x14fc8
callq 0x2040
movq %rax, %rdi
callq 0x2190
movq 0xf02e(%rip), %r14      # 0x14fc8
leaq 0xa39f(%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 0xeffc(%rip), %rdi      # 0x14fc8
callq 0x2040
movq %rax, %rdi
callq 0x2190
movq 0xefe8(%rip), %r14      # 0x14fc8
leaq 0xa2c5(%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 0xefb6(%rip), %rdi      # 0x14fc8
callq 0x2040
movq %rax, %rdi
callq 0x2190
movq 0xefa2(%rip), %r14      # 0x14fc8
leaq 0xa313(%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 0xef70(%rip), %r15      # 0x14fc8
movq %r15, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2190
xorl %r14d, %r14d
cmpb $0x0, 0xf936(%rip)      # 0x159a8
jne 0x629f
xorl %r14d, %r14d
leaq -0xb0(%rbp), %r13
movl $0xa, %edx
movq %r15, %rdi
leaq 0xa237(%rip), %rsi      # 0x102c8
callq 0x22a0
movq %r15, %rdi
movl %r14d, %esi
callq 0x23d0
movq %rax, %r12
movl $0x2, %edx
movq %rax, %rdi
leaq 0xa220(%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 0xf771(%rip), %rdi      # 0x15858
cmpq %rdi, 0xf78a(%rip)      # 0x15878
je 0x6285
callq 0x373c
cmpl %eax, %r14d
jl 0x6285
movq 0xf74f(%rip), %r12      # 0x15858
movq %rbx, -0xb0(%rbp)
movq (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
movq %r13, %rdi
callq 0x37c8
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 0xf6f6(%rip), %rdi      # 0x15848
callq 0xce58
leaq 0xf812(%rip), %rdi      # 0x15970
leaq -0x84(%rbp), %rsi
callq 0xcdb0
cmpl $0x0, (%rax)
jne 0x6195
callq 0x20a0
testl %eax, %eax
je 0x63c0
movl %eax, %r12d
leaq 0xf7ea(%rip), %rdi      # 0x15970
leaq -0x84(%rbp), %rsi
callq 0xcdb0
movl %r12d, (%rax)
cmpb $0x0, 0xf80d(%rip)      # 0x159a9
je 0x6209
leaq 0xf6f3(%rip), %rdi      # 0x15898
movq %r13, %rsi
callq 0xe8c8
leaq 0xf7bc(%rip), %rdi      # 0x15970
leaq -0x84(%rbp), %rsi
callq 0xcdb0
movl (%rax), %edi
movl $0x14, %esi
callq 0x2310
movl $0x15, %edx
movq %r15, %rdi
leaq 0xa11a(%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 0x626b
movb $0x1, 0xf799(%rip)      # 0x159a9
leaq 0xf5c1(%rip), %rdi      # 0x157d8
movq %r13, %rsi
callq 0x2150
movq 0x3e(%rbx), %rax
movq %rax, 0xf5fc(%rip)      # 0x15826
movups 0x10(%rbx), %xmm0
movups 0x20(%rbx), %xmm1
movups 0x30(%rbx), %xmm2
movups %xmm2, 0xf5db(%rip)     # 0x15818
movups %xmm1, 0xf5c4(%rip)     # 0x15808
movups %xmm0, 0xf5ad(%rip)     # 0x157f8
movl $0xe, %edi
leaq 0x1b08(%rip), %rsi      # 0x7d5f
callq 0x22b0
movq %r13, %rdi
callq 0x3746
movl %eax, %edi
callq 0x2410
movq -0xb0(%rbp), %rdi
cmpq %rbx, %rdi
je 0x60e0
callq 0x2250
jmp 0x60e0
movl $0x1, %edi
callq 0x21f0
incl %r14d
cmpb $0x1, 0xf70f(%rip)      # 0x159a8
jne 0x6082
movq 0xed22(%rip), %rdi      # 0x14fc8
leaq 0xa01b(%rip), %rsi      # 0x102c8
movl $0xa, %edx
callq 0x22a0
movq 0xed0a(%rip), %rbx      # 0x14fc8
movq %rbx, %rdi
movl %r14d, %esi
callq 0x23d0
movq %rax, %r14
leaq 0xa000(%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 0xec99(%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 0xec6b(%rip), %rdi      # 0x14fc8
callq 0x2040
movq %rax, %rdi
callq 0x2190
leaq 0xf577(%rip), %rsi      # 0x158e8
leaq -0x200(%rbp), %rdi
callq 0xcc34
leaq -0x200(%rbp), %rsi
movq -0x58(%rbp), %rdi
callq 0x7eca
leaq -0x200(%rbp), %rdi
callq 0x336e
leaq -0x100(%rbp), %rdi
callq 0x336e
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), %r12
movq %r12, -0x10(%r12)
movq -0xb0(%rbp), %rsi
movq -0xa8(%rbp), %rdx
addq %rsi, %rdx
leaq -0x158(%rbp), %rdi
callq 0x37c8
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 0x3a52
movq -0x158(%rbp), %rdi
cmpq %r12, %rdi
je 0x6436
callq 0x2250
movq 0xeb8b(%rip), %rdi      # 0x14fc8
leaq 0x9e92(%rip), %rsi      # 0x102d6
movl $0x1e, %edx
callq 0x22a0
callq 0x2340
movq 0xeb6e(%rip), %rdi      # 0x14fc8
movl %eax, %esi
callq 0x23d0
movq %rax, %rdi
callq 0x2120
xorl %edi, %edi
callq 0x22c0
movq %rax, %r15
leaq -0x200(%rbp), %rdi
callq 0x336e
jmp 0x64f5
movq %rax, %r15
jmp 0x6501
jmp 0x64da
movq %rax, %r15
movq -0x158(%rbp), %rdi
cmpq %r12, %rdi
je 0x64b4
callq 0x2250
jmp 0x64b4
movq %rax, %r15
movq -0x1b0(%rbp), %rdi
cmpq %r13, %rdi
jne 0x64f0
jmp 0x64f5
jmp 0x64da
movq %rax, %r15
movq %r14, %rdi
callq 0x2250
jmp 0x64e4
jmp 0x64da
jmp 0x64da
jmp 0x64da
jmp 0x64da
jmp 0x64e1
jmp 0x64da
jmp 0x64da
movq %rax, %r15
movq %r14, %rbx
jmp 0x64e4
jmp 0x64da
jmp 0x64da
jmp 0x64e1
movq %rax, %r15
jmp 0x64f5
jmp 0x64e1
movq %rax, %r15
movq -0xb0(%rbp), %rdi
cmpq %rbx, %rdi
je 0x64f5
callq 0x2250
leaq -0x100(%rbp), %rdi
callq 0x336e
leaq -0x50(%rbp), %rdi
callq 0x32fe
movq %r15, %rdi
callq 0x23e0