// File: /workspace/github_cpp_makefile_star2_O2/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 %r12
pushq %rbx
subq $0x30, %rsp
movq %rdi, %r14
andl $0x0, 0x8(%rdi)
movq %rsi, %r15
leaq 0x78(%rdi), %rbx
xorps %xmm0, %xmm0
movups %xmm0, 0x60(%rdi)
andq $0x0, 0x70(%rdi)
movq %rbx, %rdi
callq 0x350a
leaq -0x48(%rbp), %rdi
movq %r15, %rsi
callq 0x349c
leaq -0x48(%rbp), %rsi
callq 0x356a
movq %rax, %r15
leaq -0x48(%rbp), %rdi
callq 0x2100
callq 0x2300
movl %eax, 0xc(%r14)
leaq 0x20(%r14), %rdi
callq 0x2360
movq %rax, 0x10(%r14)
callq 0x2090
testl %eax, %eax
js 0x3827
je 0x3834
movl %eax, %r12d
movl %eax, 0x8(%r14)
callq 0x2300
leaq -0x24(%rbp), %rsi
movl %eax, (%rsi)
leaq 0xefe2(%rip), %rdi      # 0x12740
callq 0x3d9e
cmpl $0x0, (%rax)
jne 0x3782
callq 0x2300
leaq -0x24(%rbp), %rsi
movl %eax, (%rsi)
leaq 0xefc6(%rip), %rdi      # 0x12740
callq 0x3d9e
movl %r12d, (%rax)
leaq 0x1aa(%rip), %r15       # 0x3933
pushq $0x14
popq %rdi
movq %r15, %rsi
callq 0x2260
pushq $0x12
popq %rdi
movq %r15, %rsi
callq 0x2260
pushq $0xf
popq %rdi
movq %r15, %rsi
callq 0x2260
leaq -0x28(%rbp), %rsi
movl %r12d, %edi
xorl %edx, %edx
callq 0x21f0
movq 0xe801(%rip), %rdi      # 0x11fc0
leaq 0x88fd(%rip), %rsi      # 0xc0c3
callq 0x2230
movq %rax, %rdi
callq 0x2110
leaq 0x40(%r14), %rdi
callq 0x2360
movq %rax, 0x18(%r14)
subq 0x10(%r14), %rax
movq %r14, %rdi
movq %rax, %rsi
callq 0x3a2a
movq 0x50(%r14), %rsi
movq %r14, %rdi
callq 0x3a44
movq 0x58(%r14), %rsi
movq %r14, %rdi
callq 0x3a5e
movq 0xe7b2(%rip), %rdi      # 0x11fc0
callq 0x2110
movq 0xe7a6(%rip), %rdi      # 0x11fc0
callq 0x2110
movq %r14, %rdi
callq 0x3a78
addq $0x30, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq 0xe785(%rip), %rdi      # 0x11fc0
leaq 0x8abd(%rip), %rsi      # 0xc2ff
callq 0x2230
movq %rax, %rdi
callq 0x2110
movq 0xe76a(%rip), %rdi      # 0x11fc0
leaq 0x8828(%rip), %rsi      # 0xc085
callq 0x2230
movq %rax, %r14
callq 0x2300
movq %r14, %rdi
movl %eax, %esi
callq 0x23a0
movq %rax, %rdi
callq 0x2110
movq 0xe73d(%rip), %rdi      # 0x11fc0
callq 0x2110
movq 0xe731(%rip), %rdi      # 0x11fc0
leaq 0x8804(%rip), %rsi      # 0xc09a
callq 0x2230
movq (%r15), %r14
movq %rax, %rdi
movq %r14, %rsi
callq 0x2230
movq %rax, %rdi
callq 0x2110
movq %r14, %rdi
movq %r15, %rsi
callq 0x21d0
pushq $0x10
popq %rdi
callq 0x2120
movq %rax, %r15
leaq 0x87d5(%rip), %rsi      # 0xc0a3
movq %rax, %rdi
callq 0x23e0
movq 0xe703(%rip), %rsi      # 0x11fe0
movq 0xe6ec(%rip), %rdx      # 0x11fd0
movq %r15, %rdi
callq 0x2390
movq %rax, %r14
movq %r15, %rdi
callq 0x21a0
jmp 0x3910
jmp 0x390d
jmp 0x390d
movq %rax, %r14
leaq -0x48(%rbp), %rdi
callq 0x2100
jmp 0x3910
jmp 0x390d
movq %rax, %r14
movq %rbx, %rdi
callq 0x2100
movq %r14, %rdi
callq 0x23b0