// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %r15
movq %rdi, %r14
movl $0x0, 0x8(%rdi)
leaq 0x78(%rdi), %rbx
leaq 0x88(%rdi), %r13
xorps %xmm0, %xmm0
movups %xmm0, 0x60(%rdi)
movq $0x0, 0x70(%rdi)
movq %r13, 0x78(%rdi)
movq (%rsi), %rsi
movq 0x8(%r15), %rdx
addq %rsi, %rdx
movq %rbx, %rdi
callq 0x37e4
movq 0x4e(%r15), %rax
movq %rax, 0xc6(%r14)
movupd 0x20(%r15), %xmm0
movupd 0x30(%r15), %xmm1
movups 0x40(%r15), %xmm2
movups %xmm2, 0xb8(%r14)
movupd %xmm1, 0xa8(%r14)
movupd %xmm0, 0x98(%r14)
leaq -0x58(%rbp), %rdi
movq %r15, %rsi
callq 0x372c
leaq -0x58(%rbp), %rsi
callq 0x38ac
movq %rax, %r15
leaq -0x48(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3b20
callq 0x2250
callq 0x2340
movl %eax, 0xc(%r14)
leaq 0x20(%r14), %rdi
callq 0x2390
movq %rax, 0x10(%r14)
callq 0x20a0
testl %eax, %eax
js 0x3cfc
je 0x3d0b
movl %eax, %r12d
movl %eax, 0x8(%r14)
callq 0x2340
leaq -0x34(%rbp), %rsi
movl %eax, (%rsi)
leaq 0x11bfe(%rip), %rdi     # 0x15760
callq 0x44ba
cmpl $0x0, (%rax)
jne 0x3b86
callq 0x2340
leaq -0x34(%rbp), %rsi
movl %eax, (%rsi)
leaq 0x11be2(%rip), %rdi     # 0x15760
callq 0x44ba
movl %r12d, (%rax)
leaq 0x2a6(%rip), %r15       # 0x3e33
movl $0x14, %edi
movq %r15, %rsi
callq 0x22b0
movl $0x12, %edi
movq %r15, %rsi
callq 0x22b0
movl $0xf, %edi
movq %r15, %rsi
callq 0x22b0
leaq -0x38(%rbp), %rsi
movl %r12d, %edi
xorl %edx, %edx
callq 0x2220
movq 0x113ff(%rip), %r15     # 0x14fc8
leaq 0xc51d(%rip), %rsi      # 0x100ed
movl $0x17, %edx
movq %r15, %rdi
callq 0x22a0
movq (%r15), %rax
addq -0x18(%rax), %r15
movq %r15, %rdi
movl $0xa, %esi
callq 0x2260
movsbl %al, %esi
movq 0x113cd(%rip), %r15     # 0x14fc8
movq %r15, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2190
leaq 0x40(%r14), %rdi
callq 0x2390
movq %rax, %r12
movq %rax, 0x18(%r14)
subq 0x10(%r14), %r12
movl $0x2, %edi
callq 0x2180
cvtsi2sd %r12, %xmm0
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x70(%r14)
xorps %xmm0, %xmm0
cvtsi2sdq 0x50(%r14), %xmm0
movsd %xmm0, -0x30(%rbp)
movl $0x2, %edi
callq 0x2180
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movsd -0x30(%rbp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x60(%r14)
xorps %xmm0, %xmm0
cvtsi2sdq 0x58(%r14), %xmm0
movsd %xmm0, -0x30(%rbp)
movl $0x2, %edi
callq 0x2180
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movsd -0x30(%rbp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x68(%r14)
movq (%r15), %rax
addq -0x18(%rax), %r15
movq %r15, %rdi
movl $0xa, %esi
callq 0x2260
movsbl %al, %esi
movq 0x1130f(%rip), %r15     # 0x14fc8
movq %r15, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2190
movq (%r15), %rax
addq -0x18(%rax), %r15
movq %r15, %rdi
movl $0xa, %esi
callq 0x2260
movsbl %al, %esi
movq 0x112e1(%rip), %rdi     # 0x14fc8
callq 0x2040
movq %rax, %rdi
callq 0x2190
movq %r14, %rdi
callq 0x405e
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x112b6(%rip), %rdi     # 0x14fc8
leaq 0xc629(%rip), %rsi      # 0x10342
movl $0x31, %edx
callq 0x22a0
movq 0x1129e(%rip), %rdi     # 0x14fc8
callq 0x2120
movq 0x11292(%rip), %rdi     # 0x14fc8
leaq 0xc372(%rip), %rsi      # 0x100af
movl $0x14, %edx
callq 0x22a0
callq 0x2340
movq 0x11275(%rip), %rdi     # 0x14fc8
movl %eax, %esi
callq 0x23d0
movq %rax, %rdi
callq 0x2120
movq 0x1125f(%rip), %rdi     # 0x14fc8
callq 0x2120
movq 0x11253(%rip), %rdi     # 0x14fc8
leaq 0xc348(%rip), %rsi      # 0x100c4
movl $0x8, %edx
callq 0x22a0
movq (%r15), %r14
movq 0x11238(%rip), %rdi     # 0x14fc8
movq %r14, %rsi
callq 0x2270
movq %rax, %rdi
callq 0x2120
movq %r14, %rdi
movq %r15, %rsi
callq 0x2200
movl $0x10, %edi
callq 0x2130
movq %rax, %r15
leaq 0xc30e(%rip), %rsi      # 0x100cd
movq %rax, %rdi
callq 0x2420
movq 0x11212(%rip), %rsi     # 0x14fe0
movq 0x111fb(%rip), %rdx     # 0x14fd0
movq %r15, %rdi
callq 0x23c0
movq %rax, %r14
movq %r15, %rdi
callq 0x21d0
jmp 0x3e0a
jmp 0x3e07
jmp 0x3e07
movq %rax, %r14
leaq -0x48(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3e0a
callq 0x2250
jmp 0x3e0a
jmp 0x3e07
movq %rax, %r14
movq (%rbx), %rdi
cmpq %r13, %rdi
je 0x3e17
callq 0x2250
movq %r14, %rdi
callq 0x23e0
nop