// File: 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
subq $0x3e0, %rsp            # imm = 0x3E0
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x2d8(%rbp)
leaq -0x28(%rbp), %rdi
callq 0xf9d0
leaq -0x78(%rbp), %rdi
callq 0x4230
jmp 0x9c6e
movq -0x2d8(%rbp), %rdi
callq 0x86a0
movl %eax, -0x88(%rbp)
movl $0x0, -0x8c(%rbp)
movl $0x0, -0x90(%rbp)
movl -0x90(%rbp), %eax
cmpl -0x88(%rbp), %eax
jge 0x9db1
movslq -0x90(%rbp), %rsi
leaq 0x19bcc(%rip), %rdi     # 0x23880
callq 0xf730
movq %rax, %rdi
callq 0x5b30
movl %eax, -0x2dc(%rbp)
jmp 0x9cc9
movl -0x2dc(%rbp), %eax
cmpl $-0x1, %eax
jne 0x9d9b
movl $0x0, -0x94(%rbp)
movslq -0x90(%rbp), %rsi
leaq 0x19b90(%rip), %rdi     # 0x23880
callq 0xf730
movq %rax, %rsi
leaq -0xf0(%rbp), %rdi
callq 0x5c00
jmp 0x9d06
movq -0x2d8(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
leaq -0x94(%rbp), %rdx
callq 0x9240
jmp 0x9d22
leaq -0xf0(%rbp), %rdi
callq 0x4190
movslq -0x90(%rbp), %rsi
leaq 0x19b44(%rip), %rdi     # 0x23880
callq 0xf730
movq %rax, %rdi
movl -0x94(%rbp), %esi
callq 0x5aa0
jmp 0x9d51
jmp 0x9d9b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0xa6f3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0xa6ea
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x4190
jmp 0xa6ea
jmp 0x9d9d
movl -0x90(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x90(%rbp)
jmp 0x9c94
leaq 0x19ac8(%rip), %rdi     # 0x23880
movq %rdi, -0x2e8(%rbp)
callq 0xf800
movq -0x2e8(%rbp), %rdi
movq %rax, -0xf8(%rbp)
callq 0xf830
movq %rax, -0x100(%rbp)
movq -0xf8(%rbp), %rdi
movq -0x100(%rbp), %rsi
callq 0xcb40
jmp 0x9df3
movl $0x0, -0x108(%rbp)
movl -0x108(%rbp), %eax
cmpl -0x88(%rbp), %eax
jge 0x9e3f
movslq -0x108(%rbp), %rsi
leaq 0x19a67(%rip), %rdi     # 0x23880
callq 0xf730
movq %rax, %rsi
leaq -0x78(%rbp), %rdi
callq 0xf860
jmp 0x9e2c
jmp 0x9e2e
movl -0x108(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x108(%rbp)
jmp 0x9dfd
leaq -0x78(%rbp), %rdi
callq 0xf8e0
movq %rax, -0x2f0(%rbp)
jmp 0x9e51
movq -0x2f0(%rbp), %rdi
callq 0x5b10
movl %eax, -0x2f4(%rbp)
jmp 0x9e65
movl -0x2f4(%rbp), %eax
movl %eax, -0x8c(%rbp)
jmp 0x9e73
leaq -0x28(%rbp), %rdi
callq 0xfa00
movb %al, -0x2f5(%rbp)
jmp 0x9e84
movb -0x2f5(%rbp), %al
testb $0x1, %al
jne 0x9f31
jmp 0x9e94
leaq -0x28(%rbp), %rdi
callq 0xfa20
movq %rax, -0x300(%rbp)
jmp 0x9ea6
movq -0x300(%rbp), %rsi
leaq -0x160(%rbp), %rdi
callq 0x5c00
jmp 0x9ebb
leaq -0x28(%rbp), %rdi
callq 0xfa40
jmp 0x9ec6
leaq -0x160(%rbp), %rdi
callq 0x5b30
movl %eax, -0x304(%rbp)
jmp 0x9eda
movl -0x304(%rbp), %ecx
movl -0x8c(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x8c(%rbp)
leaq 0x199a3(%rip), %rdi     # 0x23898
leaq -0x160(%rbp), %rsi
callq 0xf860
jmp 0x9f03
leaq -0x160(%rbp), %rdi
callq 0x4190
jmp 0x9f31
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x160(%rbp), %rdi
callq 0x4190
jmp 0xa6ea
leaq -0x78(%rbp), %rdi
callq 0xf8c0
movb %al, -0x305(%rbp)
jmp 0x9f42
movb -0x305(%rbp), %al
testb $0x1, %al
jne 0xa090
jmp 0x9f52
leaq -0x78(%rbp), %rdi
callq 0xf8e0
movq %rax, -0x310(%rbp)
jmp 0x9f64
movq -0x310(%rbp), %rsi
leaq -0x1b8(%rbp), %rdi
callq 0x5c00
jmp 0x9f79
leaq -0x78(%rbp), %rdi
callq 0xf900
jmp 0x9f84
leaq -0x1b8(%rbp), %rdi
callq 0x5b30
movl %eax, -0x314(%rbp)
jmp 0x9f98
movl -0x314(%rbp), %ecx
movl -0x8c(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x8c(%rbp)
leaq 0x198e5(%rip), %rdi     # 0x23898
leaq -0x1b8(%rbp), %rsi
callq 0xf860
jmp 0x9fc1
jmp 0x9fc3
leaq -0x78(%rbp), %rdi
callq 0xf8c0
movb %al, -0x315(%rbp)
jmp 0x9fd4
movb -0x315(%rbp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x316(%rbp)
jne 0xa024
jmp 0x9fe9
leaq -0x78(%rbp), %rdi
callq 0xf8e0
movq %rax, -0x320(%rbp)
jmp 0x9ffb
movq -0x320(%rbp), %rdi
callq 0x5b10
movl %eax, -0x324(%rbp)
jmp 0xa00f
movl -0x324(%rbp), %eax
cmpl -0x8c(%rbp), %eax
setle %al
movb %al, -0x316(%rbp)
movb -0x316(%rbp), %al
testb $0x1, %al
jne 0xa030
jmp 0xa084
leaq -0x78(%rbp), %rdi
callq 0xf8e0
movq %rax, -0x330(%rbp)
jmp 0xa042
movq -0x330(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0xfa90
jmp 0xa054
leaq -0x78(%rbp), %rdi
callq 0xf900
jmp 0xa05f
jmp 0x9fc3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x1b8(%rbp), %rdi
callq 0x4190
jmp 0xa6ea
leaq -0x1b8(%rbp), %rdi
callq 0x4190
leaq -0x78(%rbp), %rdi
callq 0xf8c0
movb %al, -0x331(%rbp)
jmp 0xa0a1
movb -0x331(%rbp), %al
testb $0x1, %al
jne 0xa0ad
jmp 0xa0cc
leaq -0x28(%rbp), %rdi
callq 0xfa00
movb %al, -0x332(%rbp)
jmp 0xa0be
movb -0x332(%rbp), %al
testb $0x1, %al
jne 0xa0ca
jmp 0xa0cc
jmp 0xa0d1
jmp 0x9e71
movq -0x2d8(%rbp), %rdi
movl -0x8c(%rbp), %esi
callq 0x8660
leaq 0x1984e(%rip), %rdi     # 0x23938
leaq 0x197a7(%rip), %rsi     # 0x23898
callq 0xf890
jmp 0xa0f8
movq 0x18ec1(%rip), %rdi     # 0x22fc0
movq 0x18eaa(%rip), %rsi     # 0x22fb0
callq 0x3290
jmp 0xa10d
movq 0x18eac(%rip), %rdi     # 0x22fc0
movq 0x18e95(%rip), %rsi     # 0x22fb0
callq 0x3290
jmp 0xa122
movq 0x18e97(%rip), %rdi     # 0x22fc0
movq 0x18e80(%rip), %rsi     # 0x22fb0
callq 0x3290
jmp 0xa137
movq 0x18e82(%rip), %rdi     # 0x22fc0
leaq 0xf1bc(%rip), %rsi      # 0x19301
callq 0x3270
movq %rax, -0x340(%rbp)
jmp 0xa153
movq -0x340(%rbp), %rdi
movq 0x18e4f(%rip), %rsi     # 0x22fb0
callq 0x3290
jmp 0xa168
movq 0x18e51(%rip), %rdi     # 0x22fc0
leaq 0xf0f7(%rip), %rsi      # 0x1926d
callq 0x3270
movq %rax, -0x348(%rbp)
jmp 0xa184
movq -0x348(%rbp), %rdi
movq 0x18e1e(%rip), %rsi     # 0x22fb0
callq 0x3290
jmp 0xa199
movq 0x18e20(%rip), %rdi     # 0x22fc0
leaq 0xf15a(%rip), %rsi      # 0x19301
callq 0x3270
movq %rax, -0x350(%rbp)
jmp 0xa1b5
movq -0x350(%rbp), %rdi
movq 0x18ded(%rip), %rsi     # 0x22fb0
callq 0x3290
jmp 0xa1ca
movl $0x0, -0x1bc(%rbp)
movl $0x1, -0x1c0(%rbp)
movb 0x19814(%rip), %al      # 0x239f8
xorb $-0x1, %al
testb $0x1, %al
jne 0xa1ef
jmp 0xa5cf
movq 0x18dca(%rip), %rdi     # 0x22fc0
leaq 0xf08c(%rip), %rsi      # 0x19289
callq 0x3270
movq %rax, -0x358(%rbp)
jmp 0xa20b
movq -0x358(%rbp), %rdi
movl -0x1bc(%rbp), %esi
callq 0x3440
movq %rax, -0x360(%rbp)
jmp 0xa226
movq -0x360(%rbp), %rdi
leaq 0xf060(%rip), %rsi      # 0x19294
callq 0x3270
movq %rax, -0x368(%rbp)
jmp 0xa242
movq -0x368(%rbp), %rdi
movq 0x18d60(%rip), %rsi     # 0x22fb0
callq 0x3290
jmp 0xa257
jmp 0xa259
leaq 0x19638(%rip), %rdi     # 0x23898
callq 0xf8c0
movb %al, -0x369(%rbp)
jmp 0xa26d
movb -0x369(%rbp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x36a(%rbp)
jne 0xa2ce
jmp 0xa282
movl -0x1bc(%rbp), %eax
movl %eax, -0x37c(%rbp)
leaq 0x19603(%rip), %rdi     # 0x23898
callq 0xf8e0
movq %rax, -0x378(%rbp)
jmp 0xa2a3
movq -0x378(%rbp), %rdi
callq 0x5b10
movl %eax, -0x380(%rbp)
jmp 0xa2b7
movl -0x37c(%rbp), %eax
movl -0x380(%rbp), %ecx
cmpl %ecx, %eax
setge %al
movb %al, -0x36a(%rbp)
movb -0x36a(%rbp), %al
testb $0x1, %al
jne 0xa2dd
jmp 0xa5a6
leaq 0x195b4(%rip), %rdi     # 0x23898
callq 0xf8e0
movq %rax, -0x388(%rbp)
jmp 0xa2f2
movq -0x388(%rbp), %rsi
leaq -0x218(%rbp), %rdi
callq 0x5c00
jmp 0xa307
leaq 0x1958a(%rip), %rdi     # 0x23898
callq 0xf900
jmp 0xa315
leaq -0x1ec(%rbp), %rsi
leaq 0x1969d(%rip), %rdi     # 0x239c0
callq 0xf920
movq %rax, -0x390(%rbp)
jmp 0xa331
movq -0x390(%rbp), %rax
cmpl $0x0, (%rax)
jne 0xa4a7
callq 0x30a0
movl %eax, -0x1c0(%rbp)
cmpl $0x0, -0x1c0(%rbp)
jne 0xa46e
movl $0xd0, %edi
callq 0x3280
movq %rax, -0x398(%rbp)
jmp 0xa36c
movb $0x1, -0x279(%rbp)
leaq -0x278(%rbp), %rdi
leaq -0x218(%rbp), %rsi
callq 0x5c00
jmp 0xa388
movq -0x398(%rbp), %rdi
leaq -0x278(%rbp), %rsi
callq 0x5ec0
jmp 0xa39d
movb $0x0, -0x279(%rbp)
leaq -0x278(%rbp), %rdi
callq 0x4190
movq -0x398(%rbp), %rax
movq %rax, -0x220(%rbp)
movq 0x18bfb(%rip), %rdi     # 0x22fc0
leaq 0xeecb(%rip), %rsi      # 0x19297
callq 0x3270
movq %rax, -0x3a0(%rbp)
jmp 0xa3da
callq 0x3390
movq -0x3a0(%rbp), %rdi
movl %eax, %esi
callq 0x3440
movq %rax, -0x3a8(%rbp)
jmp 0xa3f6
movq -0x3a8(%rbp), %rdi
movq 0x18bac(%rip), %rsi     # 0x22fb0
callq 0x3290
jmp 0xa40b
xorl %edi, %edi
callq 0x32e0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0xa595
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0xa452
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x278(%rbp), %rdi
callq 0x4190
testb $0x1, -0x279(%rbp)
jne 0xa45d
jmp 0xa469
movq -0x398(%rbp), %rdi
callq 0x3250
jmp 0xa595
movl -0x1c0(%rbp), %eax
movl %eax, -0x3b4(%rbp)
leaq -0x1ec(%rbp), %rsi
leaq 0x19538(%rip), %rdi     # 0x239c0
callq 0xf920
movq %rax, -0x3b0(%rbp)
jmp 0xa496
movq -0x3b0(%rbp), %rax
movl -0x3b4(%rbp), %ecx
movl %ecx, (%rax)
jmp 0xa4a7
cmpl $0x0, -0x1c0(%rbp)
je 0xa584
testb $0x1, 0x18dcd(%rip)     # 0x23288
je 0xa50b
movb $0x0, 0x18dc4(%rip)     # 0x23288
leaq 0x1935d(%rip), %rdi     # 0x23828
leaq -0x218(%rbp), %rsi
callq 0xf950
jmp 0xa4d9
leaq 0x1f10(%rip), %rsi      # 0xc3f0
movl $0xe, %edi
callq 0x32d0
leaq -0x218(%rbp), %rdi
callq 0x5b30
movl %eax, -0x3b8(%rbp)
jmp 0xa4fe
movl -0x3b8(%rbp), %edi
callq 0x3480
jmp 0xa582
leaq 0x193d6(%rip), %rdi     # 0x238e8
leaq -0x218(%rbp), %rsi
callq 0xf860
jmp 0xa520
leaq -0x1ec(%rbp), %rsi
leaq 0x19492(%rip), %rdi     # 0x239c0
callq 0xf920
movq %rax, -0x3c0(%rbp)
jmp 0xa53c
movq -0x3c0(%rbp), %rax
movl (%rax), %edi
movl $0x14, %esi
callq 0x3360
movq 0x18a6a(%rip), %rdi     # 0x22fc0
leaq 0xed59(%rip), %rsi      # 0x192b6
callq 0x3270
movq %rax, -0x3c8(%rbp)
jmp 0xa56b
movq -0x3c8(%rbp), %rdi
movq 0x18a37(%rip), %rsi     # 0x22fb0
callq 0x3290
jmp 0xa580
jmp 0xa582
jmp 0xa584
leaq -0x218(%rbp), %rdi
callq 0x4190
jmp 0xa259
leaq -0x218(%rbp), %rdi
callq 0x4190
jmp 0xa6ea
cmpl $0x0, -0x1c0(%rbp)
je 0xa5ca
movl $0x1, %edi
callq 0x31e0
jmp 0xa5bb
movl -0x1bc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1bc(%rbp)
jmp 0xa1de
cmpl $0x0, -0x1c0(%rbp)
je 0xa6cf
movq 0x189dd(%rip), %rdi     # 0x22fc0
leaq 0xec9f(%rip), %rsi      # 0x19289
callq 0x3270
movq %rax, -0x3d0(%rbp)
jmp 0xa5f8
movq -0x3d0(%rbp), %rdi
movl -0x1bc(%rbp), %esi
callq 0x3440
movq %rax, -0x3d8(%rbp)
jmp 0xa613
movq -0x3d8(%rbp), %rdi
leaq 0xec73(%rip), %rsi      # 0x19294
callq 0x3270
movq %rax, -0x3e0(%rbp)
jmp 0xa62f
movq -0x3e0(%rbp), %rdi
movq 0x18973(%rip), %rsi     # 0x22fb0
callq 0x3290
jmp 0xa644
movl $0x2, %edi
callq 0x31e0
jmp 0xa650
movq 0x18969(%rip), %rdi     # 0x22fc0
movq 0x18952(%rip), %rsi     # 0x22fb0
callq 0x3290
jmp 0xa665
movq 0x18954(%rip), %rdi     # 0x22fc0
movq 0x1893d(%rip), %rsi     # 0x22fb0
callq 0x3290
jmp 0xa67a
leaq 0x192b7(%rip), %rsi     # 0x23938
leaq -0x2d0(%rbp), %rdi
callq 0xf9a0
jmp 0xa68f
movq -0x2d8(%rbp), %rdi
leaq -0x2d0(%rbp), %rsi
callq 0xc530
jmp 0xa6a4
leaq -0x2d0(%rbp), %rdi
callq 0x4260
jmp 0xa6cf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x2d0(%rbp), %rdi
callq 0x4260
jmp 0xa6ea
leaq -0x78(%rbp), %rdi
callq 0x4260
leaq -0x28(%rbp), %rdi
callq 0xfae0
addq $0x3e0, %rsp            # imm = 0x3E0
popq %rbp
retq
leaq -0x78(%rbp), %rdi
callq 0x4260
leaq -0x28(%rbp), %rdi
callq 0xfae0
movq -0x80(%rbp), %rdi
callq 0x3450
nopw %cs:(%rax,%rax)