// File: MUST-Quick-Fry[P]Process-Scheduler/scheduler.cpp
// Function: Project::Scheduler::driveFIFO()
void Scheduler::driveFIFO()
{
    // create a scheduler
    vector<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);
        }

        pq_arr.emplace_back(job_queue[i]);  
    }
    
    sort(pq_arr.begin(), pq_arr.end()); 
    
    time = pq_arr[0].get_arr_time();
    
    for(int i = 0; i < len; ++i){
        auto it = pq_arr[i];
        time += it.get_dur_time();
        scheduler.emplace(it);
    }
   
       
    // 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 $0x2f0, %rsp            # imm = 0x2F0
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x228(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x41b0
movq -0x228(%rbp), %rdi
callq 0x86a0
movl %eax, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x9513
movslq -0x2c(%rbp), %rsi
leaq 0x1a459(%rip), %rdi     # 0x23880
callq 0xf730
movq %rax, %rdi
callq 0x5b30
movl %eax, -0x22c(%rbp)
jmp 0x943c
movl -0x22c(%rbp), %eax
cmpl $-0x1, %eax
jne 0x94e5
movl $0x0, -0x40(%rbp)
movslq -0x2c(%rbp), %rsi
leaq 0x1a423(%rip), %rdi     # 0x23880
callq 0xf730
movq %rax, %rsi
leaq -0x98(%rbp), %rdi
callq 0x5c00
jmp 0x9473
movq -0x228(%rbp), %rdi
leaq -0x98(%rbp), %rsi
leaq -0x40(%rbp), %rdx
callq 0x9240
jmp 0x948c
leaq -0x98(%rbp), %rdi
callq 0x4190
movslq -0x2c(%rbp), %rsi
leaq 0x1a3dd(%rip), %rdi     # 0x23880
callq 0xf730
movq %rax, %rdi
movl -0x40(%rbp), %esi
callq 0x5aa0
jmp 0x94b5
jmp 0x94e5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x9c27
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x4190
jmp 0x9c27
movslq -0x2c(%rbp), %rsi
leaq 0x1a390(%rip), %rdi     # 0x23880
callq 0xf730
movq %rax, %rsi
leaq -0x20(%rbp), %rdi
callq 0xf750
jmp 0x9503
jmp 0x9505
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x9410
leaq -0x20(%rbp), %rdi
movq %rdi, -0x238(%rbp)
callq 0xf800
movq -0x238(%rbp), %rdi
movq %rax, -0xa0(%rbp)
callq 0xf830
movq %rax, -0xa8(%rbp)
movq -0xa0(%rbp), %rdi
movq -0xa8(%rbp), %rsi
callq 0xf7c0
jmp 0x9552
xorl %eax, %eax
movl %eax, %esi
leaq -0x20(%rbp), %rdi
callq 0xf730
movq %rax, %rdi
callq 0x5b10
movl %eax, -0x23c(%rbp)
jmp 0x956f
movl -0x23c(%rbp), %eax
movl %eax, -0x28(%rbp)
movl $0x0, -0xac(%rbp)
movl -0xac(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x9626
movslq -0xac(%rbp), %rsi
leaq -0x20(%rbp), %rdi
callq 0xf730
movq %rax, %rsi
leaq -0x108(%rbp), %rdi
callq 0x5c00
jmp 0x95b2
leaq -0x108(%rbp), %rdi
callq 0x5b30
movl %eax, -0x240(%rbp)
jmp 0x95c6
movl -0x240(%rbp), %ecx
movl -0x28(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x28(%rbp)
leaq 0x1a2bd(%rip), %rdi     # 0x23898
leaq -0x108(%rbp), %rsi
callq 0xf860
jmp 0x95e9
leaq -0x108(%rbp), %rdi
callq 0x4190
movl -0xac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xac(%rbp)
jmp 0x9582
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x108(%rbp), %rdi
callq 0x4190
jmp 0x9c27
movq -0x228(%rbp), %rdi
movl -0x28(%rbp), %esi
callq 0x8660
leaq 0x1a2fc(%rip), %rdi     # 0x23938
leaq 0x1a255(%rip), %rsi     # 0x23898
callq 0xf890
jmp 0x964a
movq 0x1996f(%rip), %rdi     # 0x22fc0
movq 0x19958(%rip), %rsi     # 0x22fb0
callq 0x3290
jmp 0x965f
movq 0x1995a(%rip), %rdi     # 0x22fc0
movq 0x19943(%rip), %rsi     # 0x22fb0
callq 0x3290
jmp 0x9674
movq 0x19945(%rip), %rdi     # 0x22fc0
movq 0x1992e(%rip), %rsi     # 0x22fb0
callq 0x3290
jmp 0x9689
movq 0x19930(%rip), %rdi     # 0x22fc0
leaq 0xfc6a(%rip), %rsi      # 0x19301
callq 0x3270
movq %rax, -0x248(%rbp)
jmp 0x96a5
movq -0x248(%rbp), %rdi
movq 0x198fd(%rip), %rsi     # 0x22fb0
callq 0x3290
jmp 0x96ba
movq 0x198ff(%rip), %rdi     # 0x22fc0
leaq 0xfba5(%rip), %rsi      # 0x1926d
callq 0x3270
movq %rax, -0x250(%rbp)
jmp 0x96d6
movq -0x250(%rbp), %rdi
movq 0x198cc(%rip), %rsi     # 0x22fb0
callq 0x3290
jmp 0x96eb
movq 0x198ce(%rip), %rdi     # 0x22fc0
leaq 0xfc08(%rip), %rsi      # 0x19301
callq 0x3270
movq %rax, -0x258(%rbp)
jmp 0x9707
movq -0x258(%rbp), %rdi
movq 0x1989b(%rip), %rsi     # 0x22fb0
callq 0x3290
jmp 0x971c
movl $0x0, -0x10c(%rbp)
movl $0x1, -0x110(%rbp)
movb 0x1a2c2(%rip), %al      # 0x239f8
xorb $-0x1, %al
testb $0x1, %al
jne 0x9741
jmp 0x9b18
movq 0x19878(%rip), %rdi     # 0x22fc0
leaq 0xfb3a(%rip), %rsi      # 0x19289
callq 0x3270
movq %rax, -0x260(%rbp)
jmp 0x975d
movq -0x260(%rbp), %rdi
movl -0x10c(%rbp), %esi
callq 0x3440
movq %rax, -0x268(%rbp)
jmp 0x9778
movq -0x268(%rbp), %rdi
leaq 0xfb0e(%rip), %rsi      # 0x19294
callq 0x3270
movq %rax, -0x270(%rbp)
jmp 0x9794
movq -0x270(%rbp), %rdi
movq 0x1980e(%rip), %rsi     # 0x22fb0
callq 0x3290
jmp 0x97a9
jmp 0x97ab
leaq 0x1a0e6(%rip), %rdi     # 0x23898
callq 0xf8c0
movb %al, -0x271(%rbp)
jmp 0x97bf
movb -0x271(%rbp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x272(%rbp)
jne 0x9820
jmp 0x97d4
movl -0x10c(%rbp), %eax
movl %eax, -0x284(%rbp)
leaq 0x1a0b1(%rip), %rdi     # 0x23898
callq 0xf8e0
movq %rax, -0x280(%rbp)
jmp 0x97f5
movq -0x280(%rbp), %rdi
callq 0x5b10
movl %eax, -0x288(%rbp)
jmp 0x9809
movl -0x284(%rbp), %eax
movl -0x288(%rbp), %ecx
cmpl %ecx, %eax
setge %al
movb %al, -0x272(%rbp)
movb -0x272(%rbp), %al
testb $0x1, %al
jne 0x982f
jmp 0x9aef
leaq 0x1a062(%rip), %rdi     # 0x23898
callq 0xf8e0
movq %rax, -0x290(%rbp)
jmp 0x9844
movq -0x290(%rbp), %rsi
leaq -0x168(%rbp), %rdi
callq 0x5c00
jmp 0x9859
leaq 0x1a038(%rip), %rdi     # 0x23898
callq 0xf900
jmp 0x9867
leaq -0x13c(%rbp), %rsi
leaq 0x1a14b(%rip), %rdi     # 0x239c0
callq 0xf920
movq %rax, -0x298(%rbp)
jmp 0x9883
movq -0x298(%rbp), %rax
cmpl $0x0, (%rax)
jne 0x99f0
callq 0x30a0
movl %eax, -0x110(%rbp)
cmpl $0x0, -0x110(%rbp)
jne 0x99b7
movl $0xd0, %edi
callq 0x3280
movq %rax, -0x2a0(%rbp)
jmp 0x98be
movb $0x1, -0x1c9(%rbp)
leaq -0x1c8(%rbp), %rdi
leaq -0x168(%rbp), %rsi
callq 0x5c00
jmp 0x98da
movq -0x2a0(%rbp), %rdi
leaq -0x1c8(%rbp), %rsi
callq 0x5ec0
jmp 0x98ef
movb $0x0, -0x1c9(%rbp)
leaq -0x1c8(%rbp), %rdi
callq 0x4190
movq -0x2a0(%rbp), %rax
movq %rax, -0x170(%rbp)
movq 0x196a9(%rip), %rdi     # 0x22fc0
leaq 0xf979(%rip), %rsi      # 0x19297
callq 0x3270
movq %rax, -0x2a8(%rbp)
jmp 0x992c
callq 0x3390
movq -0x2a8(%rbp), %rdi
movl %eax, %esi
callq 0x3440
movq %rax, -0x2b0(%rbp)
jmp 0x9948
movq -0x2b0(%rbp), %rdi
movq 0x1965a(%rip), %rsi     # 0x22fb0
callq 0x3290
jmp 0x995d
xorl %edi, %edi
callq 0x32e0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x9ade
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x999b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x1c8(%rbp), %rdi
callq 0x4190
testb $0x1, -0x1c9(%rbp)
jne 0x99a6
jmp 0x99b2
movq -0x2a0(%rbp), %rdi
callq 0x3250
jmp 0x9ade
movl -0x110(%rbp), %eax
movl %eax, -0x2bc(%rbp)
leaq -0x13c(%rbp), %rsi
leaq 0x19fef(%rip), %rdi     # 0x239c0
callq 0xf920
movq %rax, -0x2b8(%rbp)
jmp 0x99df
movq -0x2b8(%rbp), %rax
movl -0x2bc(%rbp), %ecx
movl %ecx, (%rax)
jmp 0x99f0
cmpl $0x0, -0x110(%rbp)
je 0x9acd
testb $0x1, 0x19884(%rip)     # 0x23288
je 0x9a54
movb $0x0, 0x1987b(%rip)     # 0x23288
leaq 0x19e14(%rip), %rdi     # 0x23828
leaq -0x168(%rbp), %rsi
callq 0xf950
jmp 0x9a22
leaq 0x29c7(%rip), %rsi      # 0xc3f0
movl $0xe, %edi
callq 0x32d0
leaq -0x168(%rbp), %rdi
callq 0x5b30
movl %eax, -0x2c0(%rbp)
jmp 0x9a47
movl -0x2c0(%rbp), %edi
callq 0x3480
jmp 0x9acb
leaq 0x19e8d(%rip), %rdi     # 0x238e8
leaq -0x168(%rbp), %rsi
callq 0xf860
jmp 0x9a69
leaq -0x13c(%rbp), %rsi
leaq 0x19f49(%rip), %rdi     # 0x239c0
callq 0xf920
movq %rax, -0x2c8(%rbp)
jmp 0x9a85
movq -0x2c8(%rbp), %rax
movl (%rax), %edi
movl $0x14, %esi
callq 0x3360
movq 0x19521(%rip), %rdi     # 0x22fc0
leaq 0xf810(%rip), %rsi      # 0x192b6
callq 0x3270
movq %rax, -0x2d0(%rbp)
jmp 0x9ab4
movq -0x2d0(%rbp), %rdi
movq 0x194ee(%rip), %rsi     # 0x22fb0
callq 0x3290
jmp 0x9ac9
jmp 0x9acb
jmp 0x9acd
leaq -0x168(%rbp), %rdi
callq 0x4190
jmp 0x97ab
leaq -0x168(%rbp), %rdi
callq 0x4190
jmp 0x9c27
cmpl $0x0, -0x110(%rbp)
je 0x9b13
movl $0x1, %edi
callq 0x31e0
jmp 0x9b04
movl -0x10c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10c(%rbp)
jmp 0x9730
cmpl $0x0, -0x110(%rbp)
je 0x9c15
movq 0x19494(%rip), %rdi     # 0x22fc0
leaq 0xf756(%rip), %rsi      # 0x19289
callq 0x3270
movq %rax, -0x2d8(%rbp)
jmp 0x9b41
movq -0x2d8(%rbp), %rdi
movl -0x10c(%rbp), %esi
callq 0x3440
movq %rax, -0x2e0(%rbp)
jmp 0x9b5c
movq -0x2e0(%rbp), %rdi
leaq 0xf72a(%rip), %rsi      # 0x19294
callq 0x3270
movq %rax, -0x2e8(%rbp)
jmp 0x9b78
movq -0x2e8(%rbp), %rdi
movq 0x1942a(%rip), %rsi     # 0x22fb0
callq 0x3290
jmp 0x9b8d
movl $0x2, %edi
callq 0x31e0
jmp 0x9b99
movq 0x19420(%rip), %rdi     # 0x22fc0
movq 0x19409(%rip), %rsi     # 0x22fb0
callq 0x3290
jmp 0x9bae
movq 0x1940b(%rip), %rdi     # 0x22fc0
movq 0x193f4(%rip), %rsi     # 0x22fb0
callq 0x3290
jmp 0x9bc3
leaq 0x19d6e(%rip), %rsi     # 0x23938
leaq -0x220(%rbp), %rdi
callq 0xf9a0
jmp 0x9bd8
movq -0x228(%rbp), %rdi
leaq -0x220(%rbp), %rsi
callq 0xc530
jmp 0x9bed
leaq -0x220(%rbp), %rdi
callq 0x4260
jmp 0x9c15
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x220(%rbp), %rdi
callq 0x4260
jmp 0x9c27
leaq -0x20(%rbp), %rdi
callq 0x41d0
addq $0x2f0, %rsp            # imm = 0x2F0
popq %rbp
retq
leaq -0x20(%rbp), %rdi
callq 0x41d0
movq -0x38(%rbp), %rdi
callq 0x3450
nopl (%rax)