// File: MUST-Quick-Fry[P]Process-Scheduler/monitor.cpp
// Function: Project::Monitor::Monitor(Project::Job)
Monitor::Monitor(Job j):job(j)
{
    //cout<<j.get_cmd()<<endl;
    
    char** command = cmdSplit(j.get_cmd());
    
    int process;
    int status;
     
    set_self_pid(getpid());
    start = times(&t_start);
    process = fork();
    
    if(process < 0){
	// error
        status = -1;
    }
    else if(process == 0){ 
     
        //cout << "child suspend!!!!!!!!!!!!" << endl;
        //kill(getpid(), SIGTSTP); // suspend child
        
        // child process
        cout << "=================================================" << endl;
        cout << "Job Process Start ! " << getpid() << endl;
        cout << endl;
        cout<<"command "<<command[0]<<endl;
        execvp(command[0],command);
        throw invalid_argument("Fail to execute Child Process!\n");
    }
    else{
        
        set_pid(process);
        
        if(m[getpid()]==0){m[getpid()] = process;}
                
                   
        signal(SIGTSTP, sig_handler);
        signal(SIGCONT, sig_handler);
        signal(SIGTERM, sig_handler);
        
        //cout << process << " " << getpid() << endl;
                            
  
	// parent process
        waitpid(process,&status,0);
        std::cout<<"Child Process Terminate"<<std::endl;
                 
        //std::cout << getpid() << " " << m[getpid()] << std::endl;

        //signal(SIGTERM, sig_handler);
        //signal(SIGCHLD, sig_handler);
        
        //if(this->job.get_dur_time()>=0){
          //  alarm(this->job.get_dur_time());
        //}
        
        // calculate time
        end = times(&t_end);  
        set_timeElapsed(end-start);
        set_userTime(t_end.tms_cutime);
        set_systermTime(t_end.tms_cstime);
        
        // output 
        cout << endl;        
        cout << endl;
        print_time();
        
    }
    
    
    //if(this->job.get_dur_time()>=0){
    //    double remain_time=this->job.get_dur_time()-this->get_timeElapsed();
    //    if(remain_time>0)
    //    {
    //        sleep(remain_time);
    //    }         
    //}       
    //else{
    //   this->job.set_dur_time(this->get_timeElapsed());
    //}
    
    
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rsi, -0x60(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x70(%rbp)
movl $0x0, 0x8(%rdi)
movq $0x0, 0x60(%rdi)
movq $0x0, 0x68(%rdi)
movq $0x0, 0x70(%rdi)
addq $0x78, %rdi
movq %rdi, -0x68(%rbp)
callq 0x5c00
movq -0x60(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x5ae0
jmp 0x5f1a
movq -0x70(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x5c90
movq %rax, -0x78(%rbp)
jmp 0x5f2d
leaq -0x38(%rbp), %rdi
callq 0x3170
movq -0x78(%rbp), %rax
movq %rax, -0x18(%rbp)
callq 0x3390
movq -0x70(%rbp), %rdi
movl %eax, %esi
callq 0x6290
jmp 0x5f50
movq -0x70(%rbp), %rdi
addq $0x20, %rdi
callq 0x33f0
movq %rax, %rcx
movq -0x70(%rbp), %rax
movq %rcx, 0x10(%rax)
callq 0x30a0
movl %eax, -0x48(%rbp)
cmpl $0x0, -0x48(%rbp)
jge 0x5fad
movl $0xffffffff, -0x4c(%rbp) # imm = 0xFFFFFFFF
jmp 0x626b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x6274
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x3170
jmp 0x6274
cmpl $0x0, -0x48(%rbp)
jne 0x60fe
movq 0x1d002(%rip), %rdi     # 0x22fc0
leaq 0x1333e(%rip), %rsi     # 0x19303
callq 0x3270
movq %rax, -0x80(%rbp)
jmp 0x5fd0
movq -0x80(%rbp), %rdi
movq 0x1cfd5(%rip), %rsi     # 0x22fb0
callq 0x3290
jmp 0x5fe2
movq 0x1cfd7(%rip), %rdi     # 0x22fc0
leaq 0x13099(%rip), %rsi     # 0x19089
callq 0x3270
movq %rax, -0x88(%rbp)
jmp 0x5ffe
callq 0x3390
movq -0x88(%rbp), %rdi
movl %eax, %esi
callq 0x3440
movq %rax, -0x90(%rbp)
jmp 0x601a
movq -0x90(%rbp), %rdi
movq 0x1cf88(%rip), %rsi     # 0x22fb0
callq 0x3290
jmp 0x602f
movq 0x1cf8a(%rip), %rdi     # 0x22fc0
movq 0x1cf73(%rip), %rsi     # 0x22fb0
callq 0x3290
jmp 0x6044
movq 0x1cf75(%rip), %rdi     # 0x22fc0
leaq 0x1304c(%rip), %rsi     # 0x1909e
callq 0x3270
movq %rax, -0x98(%rbp)
jmp 0x6060
movq -0x98(%rbp), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rsi
callq 0x3270
movq %rax, -0xa0(%rbp)
jmp 0x607c
movq -0xa0(%rbp), %rdi
movq 0x1cf26(%rip), %rsi     # 0x22fb0
callq 0x3290
jmp 0x6091
movq -0x18(%rbp), %rsi
movq (%rsi), %rdi
callq 0x31f0
movl $0x10, %edi
callq 0x3120
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xa8(%rbp)
leaq 0x12fec(%rip), %rsi     # 0x190a7
callq 0x3490
jmp 0x60c2
movq -0xa8(%rbp), %rdi
movq 0x1cf10(%rip), %rsi     # 0x22fe0
movq 0x1cef9(%rip), %rdx     # 0x22fd0
callq 0x3430
jmp 0x6286
movq -0xa8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
callq 0x31c0
jmp 0x6274
movq -0x70(%rbp), %rdi
movl -0x48(%rbp), %esi
callq 0x62b0
jmp 0x610c
callq 0x3390
movl %eax, -0x50(%rbp)
leaq 0x1d695(%rip), %rdi     # 0x237b0
leaq -0x50(%rbp), %rsi
callq 0x6a50
movq %rax, -0xb0(%rbp)
jmp 0x612d
movq -0xb0(%rbp), %rax
cmpl $0x0, (%rax)
jne 0x6172
movl -0x48(%rbp), %eax
movl %eax, -0xbc(%rbp)
callq 0x3390
movl %eax, -0x54(%rbp)
leaq 0x1d65f(%rip), %rdi     # 0x237b0
leaq -0x54(%rbp), %rsi
callq 0x6a50
movq %rax, -0xb8(%rbp)
jmp 0x6163
movq -0xb8(%rbp), %rax
movl -0xbc(%rbp), %ecx
movl %ecx, (%rax)
leaq 0x157(%rip), %rsi       # 0x62d0
movq %rsi, -0xc8(%rbp)
movl $0x14, %edi
callq 0x32d0
movq -0xc8(%rbp), %rsi
movl $0x12, %edi
callq 0x32d0
movq -0xc8(%rbp), %rsi
movl $0xf, %edi
callq 0x32d0
movl -0x48(%rbp), %edi
leaq -0x4c(%rbp), %rsi
xorl %edx, %edx
callq 0x3210
jmp 0x61bc
movq 0x1cdfd(%rip), %rdi     # 0x22fc0
leaq 0x12efd(%rip), %rsi     # 0x190c7
callq 0x3270
movq %rax, -0xd0(%rbp)
jmp 0x61d8
movq -0xd0(%rbp), %rdi
movq 0x1cdca(%rip), %rsi     # 0x22fb0
callq 0x3290
jmp 0x61ed
movq -0x70(%rbp), %rdi
addq $0x40, %rdi
callq 0x33f0
movq -0x70(%rbp), %rdi
movq %rax, 0x18(%rdi)
movq 0x10(%rdi), %rax
movq 0x18(%rdi), %rsi
subq %rax, %rsi
callq 0x6410
jmp 0x6214
movq -0x70(%rbp), %rdi
movq 0x50(%rdi), %rsi
callq 0x6440
jmp 0x6223
movq -0x70(%rbp), %rdi
movq 0x58(%rdi), %rsi
callq 0x6470
jmp 0x6232
movq 0x1cd87(%rip), %rdi     # 0x22fc0
movq 0x1cd70(%rip), %rsi     # 0x22fb0
callq 0x3290
jmp 0x6247
movq 0x1cd72(%rip), %rdi     # 0x22fc0
movq 0x1cd5b(%rip), %rsi     # 0x22fb0
callq 0x3290
jmp 0x625c
movq -0x70(%rbp), %rdi
callq 0x64a0
jmp 0x6267
jmp 0x6269
jmp 0x626b
addq $0xd0, %rsp
popq %rbp
retq
movq -0x68(%rbp), %rdi
callq 0x4190
movq -0x40(%rbp), %rdi
callq 0x3450
nopw %cs:(%rax,%rax)