// File: /workspace/github_cpp_makefile_star2_O2/MUST-Quick-Fry[P]Process-Scheduler/scheduler.cpp
// Function: Project::Scheduler::driveSJF2()
void Scheduler::driveSJF2(){

    // create a schedule
    queue<Job> pq_arr; 
    priority_queue<Job,vector<Job>,greater<Job> > wait_q; 
    int len = get_job_num();

    // 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]);  
    }
       
      
    int time = 0;
    Job con;
    con.ID = 0;
    unordered_map<int, int> jobmap;
    Job lastjob = con;
    
    while(true){                                          
        while(!pq_arr.empty() && time == pq_arr.front().get_arr_time()){
            Job tmp = pq_arr.front();
            pq_arr.pop(); 
            
            if(!wait_q.empty() && tmp.get_dur_time() < wait_q.top().get_dur_time() && wait_q.top().ID == lastjob.ID)
                wait_q.pop();
            wait_q.emplace(tmp);         
        }
        
   
        if(!wait_q.empty()){
            
            Job it = wait_q.top();
            wait_q.pop();
            
            if(lastjob.ID == it.ID || lastjob.ID == 0){
            
                if(jobmap[it.ID] == 0){
                    scheduler.emplace(it);
                    jobmap[it.ID] = 1;
                }
            
                it.has_exp_time++;
                it.set_dur_time(it.get_dur_time()-1);
                           
                if(it.get_dur_time() == 0){
                    scheduler.back().set_dur_time(it.has_exp_time);
                    scheduler.back().service_time_left = 2;
                    //cout << scheduler.back().ID << " arr " << scheduler.back().get_arr_time() << " dur "<< scheduler.back().get_dur_time()<< endl;
                    jobmap[it.ID] = 0;
                    lastjob = con;
                }
                else{ 
                    if(wait_q.empty() || it.get_dur_time() < wait_q.top().get_dur_time())
                        wait_q.emplace(it); 
                        
                    lastjob = it;
                }
                
            }
            else{
                scheduler.back().set_dur_time(lastjob.has_exp_time);
                //cout << scheduler.back().ID << " arr " << scheduler.back().get_arr_time() << " dur "<< scheduler.back().get_dur_time()<< endl;
                jobmap[lastjob.ID] = 0;
                lastjob.has_exp_time = 0;
                lastjob.set_arr_time(time);
                wait_q.emplace(lastjob);
                
             
                if(jobmap[it.ID] == 0){
                    scheduler.emplace(it);
                    jobmap[it.ID] = 1;
                }
            
                it.has_exp_time++;
                it.set_dur_time(it.get_dur_time()-1);
                
                
                if(it.get_dur_time() == 0){
                    scheduler.back().set_dur_time(it.has_exp_time);
                    scheduler.back().service_time_left = 2;
                    //cout << scheduler.back().ID << " arr " << scheduler.back().get_arr_time() << " dur "<< scheduler.back().get_dur_time()<< endl;
                    jobmap[it.ID] = 0;
                    lastjob = con;
                }
                else{ 
                    if(wait_q.empty() || it.get_dur_time() < wait_q.top().get_dur_time())
                        wait_q.emplace(it); 
                        
                    lastjob = it;
                }            
                
            }
                
                
        }
        
        time++;
        //cout << "now is " << time << " " << wait_q.size() << endl;
        if(wait_q.empty() && pq_arr.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;
                         
                         kill(monitor_map[it.ID], SIGCONT);
                         signal(SIGALRM, signal_preemSJF);
                         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 $0x2c8, %rsp            # imm = 0x2C8
movq %rdi, %rbx
leaq -0x190(%rbp), %rdi
callq 0x2ed2
xorps %xmm0, %xmm0
movaps %xmm0, -0x50(%rbp)
andq $0x0, -0x40(%rbp)
movq %rbx, -0x58(%rbp)
movl 0x58(%rbx), %eax
xorl %r13d, %r13d
testl %eax, %eax
cmovlel %r13d, %eax
imulq $0x58, %rax, %r12
leaq -0x2e8(%rbp), %rbx
leaq -0x1e8(%rbp), %r14
movq 0xd35a(%rip), %r15      # 0x12810
cmpq %r13, %r12
je 0x5512
addq %r13, %r15
movq %r15, %rdi
callq 0x34be
cmpl $-0x1, %eax
jne 0x550c
andl $0x0, -0x1e8(%rbp)
movq 0xd337(%rip), %rsi      # 0x12810
addq %r13, %rsi
movq %rbx, %rdi
callq 0x350a
movq %rbx, %rsi
movq %r14, %rdx
callq 0x4918
movq %rbx, %rdi
callq 0x2100
movq 0xd312(%rip), %rdi      # 0x12810
addq %r13, %rdi
movl -0x1e8(%rbp), %esi
callq 0x3488
addq $0x58, %r13
jmp 0x54af
movq 0xd2ff(%rip), %rbx      # 0x12818
cmpq %rbx, %r15
je 0x5580
movq %rbx, %r14
subq %r15, %r14
pushq $0x58
popq %rcx
movq %r14, %rax
cqto
idivq %rcx
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x6f4f
cmpq $0x581, %r14            # imm = 0x581
jl 0x5575
leaq 0x580(%r15), %r14
movq %r15, %rdi
movq %r14, %rsi
callq 0x737d
cmpq %rbx, %r14
je 0x5580
movq %r14, %rdi
callq 0x741b
addq $0x58, %r14
jmp 0x5562
movq %r15, %rdi
movq %rbx, %rsi
callq 0x737d
xorl %r14d, %r14d
leaq -0x190(%rbp), %rbx
cmpq %r14, %r12
je 0x55a7
movq 0xd27a(%rip), %rsi      # 0x12810
addq %r14, %rsi
movq %rbx, %rdi
callq 0xb0c6
addq $0x58, %r14
jmp 0x558a
leaq -0x1d8(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
andq $0x0, 0x18(%rax)
leaq -0xb8(%rbp), %rax
movq %rax, -0x30(%rax)
movq $0x1, -0x28(%rax)
xorps %xmm0, %xmm0
movups %xmm0, -0x20(%rax)
movl $0x3f800000, -0x10(%rax) # imm = 0x3F800000
movups %xmm0, -0x8(%rax)
leaq -0x140(%rbp), %rdi
leaq -0x1e8(%rbp), %rsi
callq 0x350a
xorl %r15d, %r15d
leaq -0xb0(%rbp), %rbx
leaq -0x50(%rbp), %r14
leaq -0x190(%rbp), %r13
movq -0x180(%rbp), %rdi
cmpq %rdi, -0x160(%rbp)
je 0x568c
callq 0x34b4
cmpl %eax, %r15d
jne 0x568c
movq -0x180(%rbp), %rsi
movq %rbx, %rdi
callq 0x350a
movq %r13, %rdi
callq 0xa080
movq -0x50(%rbp), %rax
cmpq -0x48(%rbp), %rax
je 0x5677
movq %rbx, %rdi
callq 0x34be
movl %eax, %r12d
movq -0x50(%rbp), %rdi
callq 0x34be
cmpl %eax, %r12d
jge 0x5677
movq -0x50(%rbp), %rax
movl 0x2c(%rax), %eax
cmpl -0x114(%rbp), %eax
jne 0x5677
movq %r14, %rdi
callq 0x75ca
movq %r14, %rdi
movq %rbx, %rsi
callq 0x75ee
movq %rbx, %rdi
callq 0x2100
jmp 0x560c
movq -0x50(%rbp), %rsi
cmpq -0x48(%rbp), %rsi
je 0x57bf
movq %rbx, %rdi
callq 0x350a
movq %r14, %rdi
callq 0x75ca
movl -0x114(%rbp), %eax
testl %eax, %eax
je 0x57c7
cmpl -0x84(%rbp), %eax
je 0x57c7
movq 0xd18d(%rip), %rdi      # 0x12858
cmpq 0xd18e(%rip), %rdi      # 0x12860
jne 0x56e7
movq 0xd195(%rip), %rax      # 0x12870
movq -0x8(%rax), %rdi
movl $0x1b8, %eax            # imm = 0x1B8
addq %rax, %rdi
addq $-0x58, %rdi
movl -0x10c(%rbp), %esi
callq 0x3488
leaq -0xe8(%rbp), %rdi
leaq -0x114(%rbp), %rsi
callq 0x9faa
andl $0x0, (%rax)
andl $0x0, -0x10c(%rbp)
leaq -0x140(%rbp), %rdi
movl %r15d, %esi
callq 0x347e
movq %r14, %rdi
leaq -0x140(%rbp), %rsi
callq 0x75ee
leaq -0xe8(%rbp), %rdi
leaq -0x84(%rbp), %rsi
callq 0x9faa
cmpl $0x0, (%rax)
jne 0x5771
leaq 0xd0d8(%rip), %rdi      # 0x12828
movq %rbx, %rsi
callq 0xb0c6
leaq -0xe8(%rbp), %rdi
leaq -0x84(%rbp), %rsi
callq 0x9faa
movl $0x1, (%rax)
incl -0x7c(%rbp)
movq %rbx, %rdi
callq 0x34be
decl %eax
movq %rbx, %rdi
movl %eax, %esi
callq 0x3488
movq %rbx, %rdi
callq 0x34be
testl %eax, %eax
je 0x5898
movq -0x50(%rbp), %rax
cmpq -0x48(%rbp), %rax
je 0x5854
movq %rbx, %rdi
callq 0x34be
movl %eax, %r12d
movq -0x50(%rbp), %rdi
callq 0x34be
jmp 0x5848
incl %r15d
jmp 0x5933
leaq -0xe8(%rbp), %rdi
leaq -0x84(%rbp), %rsi
callq 0x9faa
cmpl $0x0, (%rax)
jne 0x5807
leaq 0xd042(%rip), %rdi      # 0x12828
movq %rbx, %rsi
callq 0xb0c6
leaq -0xe8(%rbp), %rdi
leaq -0x84(%rbp), %rsi
callq 0x9faa
movl $0x1, (%rax)
incl -0x7c(%rbp)
movq %rbx, %rdi
callq 0x34be
decl %eax
movq %rbx, %rdi
movl %eax, %esi
callq 0x3488
movq %rbx, %rdi
callq 0x34be
testl %eax, %eax
je 0x5867
movq -0x50(%rbp), %rax
cmpq -0x48(%rbp), %rax
je 0x5854
movq %rbx, %rdi
callq 0x34be
movl %eax, %r12d
movq -0x50(%rbp), %rdi
callq 0x34be
movq %rbx, %rsi
cmpl %eax, %r12d
jge 0x590e
movq %r14, %rdi
movq %rbx, %rsi
callq 0x75ee
movq %rbx, %rsi
jmp 0x590e
movq 0xcfea(%rip), %rdi      # 0x12858
cmpq 0xcfeb(%rip), %rdi      # 0x12860
jne 0x588a
movq 0xcff2(%rip), %rax      # 0x12870
movq -0x8(%rax), %rdi
movl $0x1b8, %eax            # imm = 0x1B8
addq %rax, %rdi
addq $-0x58, %rdi
movl -0x7c(%rbp), %esi
callq 0x3488
jmp 0x58c7
movq 0xcfb9(%rip), %rdi      # 0x12858
cmpq 0xcfba(%rip), %rdi      # 0x12860
jne 0x58bb
movq 0xcfc1(%rip), %rax      # 0x12870
movq -0x8(%rax), %rdi
movl $0x1b8, %eax            # imm = 0x1B8
addq %rax, %rdi
addq $-0x58, %rdi
movl -0x7c(%rbp), %esi
callq 0x3488
movq 0xcf8a(%rip), %rax      # 0x12858
cmpq 0xcf8b(%rip), %rax      # 0x12860
jne 0x58ea
movq 0xcf92(%rip), %rax      # 0x12870
movq -0x8(%rax), %rax
movl $0x1b8, %ecx            # imm = 0x1B8
addq %rcx, %rax
movl $0x2, -0x28(%rax)
leaq -0xe8(%rbp), %rdi
leaq -0x84(%rbp), %rsi
callq 0x9faa
andl $0x0, (%rax)
leaq -0x1e8(%rbp), %rsi
leaq -0x140(%rbp), %rdi
callq 0x758c
movq %rbx, %rdi
callq 0x2100
movq -0x50(%rbp), %rax
incl %r15d
cmpq -0x48(%rbp), %rax
jne 0x560c
movq -0x160(%rbp), %rax
cmpq -0x180(%rbp), %rax
jne 0x560c
movq -0x58(%rbp), %rax
movl %r15d, 0x5c(%rax)
leaq 0xcf72(%rip), %rdi      # 0x128c8
leaq 0xcecb(%rip), %rsi      # 0x12828
callq 0x76d2
movq 0xc657(%rip), %rdi      # 0x11fc0
callq 0x2110
movq 0xc64b(%rip), %rdi      # 0x11fc0
callq 0x2110
movq 0xc63f(%rip), %rdi      # 0x11fc0
callq 0x2110
movq 0xc633(%rip), %rdi      # 0x11fc0
leaq 0x6969(%rip), %rsi      # 0xc2fd
callq 0x2230
movq %rax, %rdi
callq 0x2110
movq 0xc618(%rip), %rdi      # 0x11fc0
leaq 0x68ba(%rip), %rsi      # 0xc269
callq 0x2230
movq %rax, %rdi
callq 0x2110
movq 0xc5fd(%rip), %rdi      # 0x11fc0
leaq 0x6933(%rip), %rsi      # 0xc2fd
callq 0x2230
movq %rax, %rdi
callq 0x2110
xorl %ebx, %ebx
leaq -0x84(%rbp), %r14
leaq -0xb0(%rbp), %r13
leaq 0xcf62(%rip), %r15      # 0x12950
cmpb $0x0, 0xcf93(%rip)      # 0x12988
jne 0x5b48
movq 0xc5be(%rip), %rdi      # 0x11fc0
leaq 0x687c(%rip), %rsi      # 0xc285
callq 0x2230
movq %rax, %rdi
movl %ebx, %esi
callq 0x23a0
movq %rax, %rdi
leaq 0x686e(%rip), %rsi      # 0xc290
callq 0x2230
movq %rax, %rdi
callq 0x2110
movq 0xce02(%rip), %rdi      # 0x12838
cmpq %rdi, 0xce1b(%rip)      # 0x12858
je 0x5b39
callq 0x34b4
cmpl %eax, %ebx
jl 0x5b39
movq 0xcde1(%rip), %rsi      # 0x12838
movq %r13, %rdi
callq 0x350a
leaq 0xcdc2(%rip), %rdi      # 0x12828
callq 0xa080
movq %r15, %rdi
movq %r14, %rsi
callq 0x9faa
cmpl $0x0, (%rax)
jne 0x5a99
callq 0x2090
testl %eax, %eax
je 0x5c16
movl %eax, %r12d
movq %r15, %rdi
movq %r14, %rsi
callq 0x9faa
movl %r12d, (%rax)
cmpb $0x0, 0xcee9(%rip)      # 0x12989
je 0x5ae3
leaq 0xcdcf(%rip), %rdi      # 0x12878
movq %r13, %rsi
callq 0xb0c6
movq %r15, %rdi
movq %r14, %rsi
callq 0x9faa
movl (%rax), %edi
pushq $0x14
popq %rsi
callq 0x22d0
movq 0xc4f3(%rip), %rdi      # 0x11fc0
leaq 0x67de(%rip), %rsi      # 0xc2b2
callq 0x2230
movq %rax, %rdi
callq 0x2110
jmp 0x5b2c
movb $0x1, 0xce9f(%rip)      # 0x12989
leaq 0xccc7(%rip), %rdi      # 0x127b8
movq %r13, %rsi
callq 0x758c
movq %r15, %rdi
movq %r14, %rsi
callq 0x9faa
movl (%rax), %edi
pushq $0x12
popq %rsi
callq 0x22d0
pushq $0xe
popq %rdi
leaq 0xdf9(%rip), %rsi       # 0x6911
callq 0x2260
movq %r13, %rdi
callq 0x34be
movl %eax, %edi
callq 0x23d0
movq %r13, %rdi
callq 0x2100
jmp 0x5a2f
pushq $0x1
popq %rdi
callq 0x21c0
incl %ebx
jmp 0x59ee
movq 0xc471(%rip), %rdi      # 0x11fc0
leaq 0x672f(%rip), %rsi      # 0xc285
callq 0x2230
movq %rax, %rdi
movl %ebx, %esi
callq 0x23a0
leaq 0x6724(%rip), %rsi      # 0xc290
movq %rax, %rdi
callq 0x2230
movq %rax, %rdi
callq 0x2110
pushq $0x2
popq %rdi
callq 0x21c0
movq 0xc435(%rip), %rdi      # 0x11fc0
callq 0x2110
movq 0xc429(%rip), %rdi      # 0x11fc0
callq 0x2110
leaq 0xcd25(%rip), %rsi      # 0x128c8
leaq -0x238(%rbp), %rdi
callq 0x9e98
leaq -0x238(%rbp), %rsi
movq -0x58(%rbp), %rdi
callq 0x64d6
leaq -0x238(%rbp), %rdi
callq 0x2f36
leaq -0x140(%rbp), %rdi
callq 0x2100
leaq -0xe8(%rbp), %rdi
callq 0x30dc
leaq -0x1e8(%rbp), %rdi
callq 0x2100
leaq -0x50(%rbp), %rdi
callq 0x2ea6
leaq -0x190(%rbp), %rdi
callq 0x2f36
addq $0x2c8, %rsp            # imm = 0x2C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xd0, %edi
callq 0x2240
movq %rax, %r14
leaq -0x290(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x350a
leaq -0x290(%rbp), %rsi
movq %r14, %rdi
callq 0x36ca
leaq -0x290(%rbp), %rdi
callq 0x2100
movq 0xc368(%rip), %rdi      # 0x11fc0
leaq 0x6634(%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 -0x238(%rbp), %rdi
callq 0x2f36
jmp 0x5cfa
movq %rax, %rbx
jmp 0x5d06
jmp 0x5cc4
jmp 0x5ce4
jmp 0x5cc4
jmp 0x5ce4
movq %rax, %rbx
leaq -0x2e8(%rbp), %rdi
jmp 0x5d19
movq %rax, %rbx
leaq -0x290(%rbp), %rdi
callq 0x2100
jmp 0x5cce
jmp 0x5cc4
jmp 0x5cc4
movq %rax, %rbx
jmp 0x5d1e
jmp 0x5ce4
movq %rax, %rbx
movq %r14, %rdi
callq 0x2220
jmp 0x5cee
jmp 0x5ceb
jmp 0x5ce4
jmp 0x5ceb
jmp 0x5ce4
jmp 0x5ce4
jmp 0x5ce4
movq %rax, %rbx
jmp 0x5cfa
jmp 0x5ceb
movq %rax, %rbx
leaq -0xb0(%rbp), %rdi
callq 0x2100
leaq -0x140(%rbp), %rdi
callq 0x2100
leaq -0xe8(%rbp), %rdi
callq 0x30dc
leaq -0x1e8(%rbp), %rdi
callq 0x2100
leaq -0x50(%rbp), %rdi
callq 0x2ea6
leaq -0x190(%rbp), %rdi
callq 0x2f36
movq %rbx, %rdi
callq 0x23b0
nop