// File: /workspace/github_cpp_makefile_star2_O2/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)
andq $0x0, -0x40(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x2ed2
movq %rbx, -0x30(%rbp)
movl 0x58(%rbx), %eax
xorl %r13d, %r13d
testl %eax, %eax
cmovlel %r13d, %eax
imulq $0x58, %rax, %r12
leaq -0x1f8(%rbp), %rbx
leaq -0xf8(%rbp), %r14
movq 0xd936(%rip), %r15      # 0x12810
cmpq %r13, %r12
je 0x4f36
addq %r13, %r15
movq %r15, %rdi
callq 0x34be
cmpl $-0x1, %eax
jne 0x4f30
andl $0x0, -0xf8(%rbp)
movq 0xd913(%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 0xd8ee(%rip), %rdi      # 0x12810
addq %r13, %rdi
movl -0xf8(%rbp), %esi
callq 0x3488
addq $0x58, %r13
jmp 0x4ed3
movq 0xd8db(%rip), %rbx      # 0x12818
cmpq %rbx, %r15
je 0x4fa4
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 0x6a0c
cmpq $0x581, %r14            # imm = 0x581
jl 0x4f99
leaq 0x580(%r15), %r14
movq %r15, %rdi
movq %r14, %rsi
callq 0x6e39
cmpq %rbx, %r14
je 0x4fa4
movq %r14, %rdi
callq 0x6ed7
addq $0x58, %r14
jmp 0x4f86
movq %r15, %rdi
movq %rbx, %rsi
callq 0x6e39
xorl %r14d, %r14d
leaq -0xa0(%rbp), %rbx
cmpq %r14, %r12
je 0x4fcb
movq 0xd856(%rip), %rsi      # 0x12810
addq %r14, %rsi
movq %rbx, %rdi
callq 0xb0c6
addq $0x58, %r14
jmp 0x4fae
movq -0x90(%rbp), %rdi
callq 0x34b4
movl %eax, %r13d
leaq -0xf8(%rbp), %rbx
leaq -0xa0(%rbp), %r14
leaq -0x50(%rbp), %r12
movq -0x50(%rbp), %rsi
cmpq -0x48(%rbp), %rsi
je 0x502d
movq %rbx, %rdi
callq 0x350a
movq %r12, %rdi
callq 0x75ca
movq %rbx, %rdi
callq 0x34be
movl %eax, %r15d
leaq 0xd810(%rip), %rdi      # 0x12828
movq %rbx, %rsi
callq 0xb0c6
addl %r13d, %r15d
movq %rbx, %rdi
callq 0x2100
jmp 0x5030
movl %r13d, %r15d
movq -0x90(%rbp), %rsi
cmpq %rsi, -0x70(%rbp)
je 0x50b5
movq %rbx, %rdi
callq 0x350a
movq %r14, %rdi
callq 0xa080
movq %rbx, %rdi
callq 0x34be
movl %eax, %r13d
leaq 0xd7c9(%rip), %rdi      # 0x12828
movq %rbx, %rsi
callq 0xb0c6
addl %r15d, %r13d
movq -0x90(%rbp), %rdi
cmpq %rdi, -0x70(%rbp)
je 0x509a
callq 0x34b4
cmpl %r13d, %eax
jg 0x509a
movq -0x90(%rbp), %rsi
movq %r12, %rdi
callq 0x75ee
movq %r14, %rdi
callq 0xa080
jmp 0x506a
movq %rbx, %rdi
callq 0x2100
movq -0x70(%rbp), %rax
cmpq -0x90(%rbp), %rax
jne 0x4fec
jmp 0x50b8
movl %r15d, %r13d
movq -0x50(%rbp), %rax
cmpq -0x48(%rbp), %rax
jne 0x4fec
movq -0x30(%rbp), %rax
movl %r13d, 0x5c(%rax)
leaq 0xd7f3(%rip), %rdi      # 0x128c8
leaq 0xd74c(%rip), %rsi      # 0x12828
callq 0x76d2
movq 0xced8(%rip), %rdi      # 0x11fc0
callq 0x2110
movq 0xcecc(%rip), %rdi      # 0x11fc0
callq 0x2110
movq 0xcec0(%rip), %rdi      # 0x11fc0
callq 0x2110
movq 0xceb4(%rip), %rdi      # 0x11fc0
leaq 0x71ea(%rip), %rsi      # 0xc2fd
callq 0x2230
movq %rax, %rdi
callq 0x2110
movq 0xce99(%rip), %rdi      # 0x11fc0
leaq 0x713b(%rip), %rsi      # 0xc269
callq 0x2230
movq %rax, %rdi
callq 0x2110
movq 0xce7e(%rip), %rdi      # 0x11fc0
leaq 0x71b4(%rip), %rsi      # 0xc2fd
callq 0x2230
movq %rax, %rdi
callq 0x2110
xorl %r14d, %r14d
leaq -0xcc(%rbp), %r15
leaq -0xf8(%rbp), %rbx
leaq 0xd7e2(%rip), %r12      # 0x12950
cmpb $0x0, 0xd813(%rip)      # 0x12988
jne 0x52b6
movq 0xce3e(%rip), %rdi      # 0x11fc0
leaq 0x70fc(%rip), %rsi      # 0xc285
callq 0x2230
movq %rax, %rdi
movl %r14d, %esi
callq 0x23a0
movq %rax, %rdi
leaq 0x70ed(%rip), %rsi      # 0xc290
callq 0x2230
movq %rax, %rdi
callq 0x2110
movq 0xd681(%rip), %rdi      # 0x12838
cmpq %rdi, 0xd69a(%rip)      # 0x12858
je 0x52a6
callq 0x34b4
cmpl %eax, %r14d
jl 0x52a6
movq 0xd65f(%rip), %rsi      # 0x12838
movq %rbx, %rdi
callq 0x350a
leaq 0xd640(%rip), %rdi      # 0x12828
callq 0xa080
movq %r12, %rdi
movq %r15, %rsi
callq 0x9faa
cmpl $0x0, (%rax)
jne 0x521b
callq 0x2090
testl %eax, %eax
je 0x5361
movl %eax, %r13d
movq %r12, %rdi
movq %r15, %rsi
callq 0x9faa
movl %r13d, (%rax)
cmpb $0x0, 0xd767(%rip)      # 0x12989
je 0x5265
leaq 0xd64d(%rip), %rdi      # 0x12878
movq %rbx, %rsi
callq 0xb0c6
movq %r12, %rdi
movq %r15, %rsi
callq 0x9faa
movl (%rax), %edi
pushq $0x14
popq %rsi
callq 0x22d0
movq 0xcd71(%rip), %rdi      # 0x11fc0
leaq 0x705c(%rip), %rsi      # 0xc2b2
callq 0x2230
movq %rax, %rdi
callq 0x2110
jmp 0x5299
movb $0x1, 0xd71d(%rip)      # 0x12989
leaq 0xd545(%rip), %rdi      # 0x127b8
movq %rbx, %rsi
callq 0x758c
pushq $0xe
popq %rdi
leaq 0x1168(%rip), %rsi      # 0x63ed
callq 0x2260
movq %rbx, %rdi
callq 0x34be
movl %eax, %edi
callq 0x23d0
movq %rbx, %rdi
callq 0x2100
jmp 0x51b0
pushq $0x1
popq %rdi
callq 0x21c0
incl %r14d
jmp 0x516e
movq 0xcd03(%rip), %rdi      # 0x11fc0
leaq 0x6fc1(%rip), %rsi      # 0xc285
callq 0x2230
movq %rax, %rdi
movl %r14d, %esi
callq 0x23a0
leaq 0x6fb5(%rip), %rsi      # 0xc290
movq %rax, %rdi
callq 0x2230
movq %rax, %rdi
callq 0x2110
pushq $0x2
popq %rdi
callq 0x21c0
movq 0xccc6(%rip), %rdi      # 0x11fc0
callq 0x2110
movq 0xccba(%rip), %rdi      # 0x11fc0
callq 0x2110
leaq 0xd5b6(%rip), %rsi      # 0x128c8
leaq -0x148(%rbp), %rdi
callq 0x9e98
leaq -0x148(%rbp), %rsi
movq -0x30(%rbp), %rdi
callq 0x64d6
leaq -0x148(%rbp), %rdi
callq 0x2f36
leaq -0xa0(%rbp), %rdi
callq 0x2f36
leaq -0x50(%rbp), %rdi
callq 0x2ea6
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xd0, %edi
callq 0x2240
movq %rax, %r14
leaq -0x1a0(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x350a
leaq -0x1a0(%rbp), %rsi
movq %r14, %rdi
callq 0x36ca
leaq -0x1a0(%rbp), %rdi
callq 0x2100
movq 0xcc1d(%rip), %rdi      # 0x11fc0
leaq 0x6ee9(%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 -0x148(%rbp), %rdi
callq 0x2f36
jmp 0x5440
movq %rax, %rbx
jmp 0x544c
jmp 0x542a
jmp 0x542a
movq %rax, %rbx
leaq -0x1f8(%rbp), %rdi
jmp 0x543b
jmp 0x542a
movq %rax, %rbx
leaq -0x1a0(%rbp), %rdi
callq 0x2100
jmp 0x5416
jmp 0x542a
jmp 0x542a
jmp 0x542a
jmp 0x542a
jmp 0x542a
movq %rax, %rbx
movq %r14, %rdi
callq 0x2220
jmp 0x5434
jmp 0x5431
jmp 0x5431
jmp 0x5431
jmp 0x542a
jmp 0x542a
movq %rax, %rbx
jmp 0x5440
jmp 0x5431
movq %rax, %rbx
leaq -0xf8(%rbp), %rdi
callq 0x2100
leaq -0xa0(%rbp), %rdi
callq 0x2f36
leaq -0x50(%rbp), %rdi
callq 0x2ea6
movq %rbx, %rdi
callq 0x23b0
nop