// File: /workspace/github_cpp_makefile_star2_O1/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 0x37c8
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 0x3710
leaq -0x58(%rbp), %rsi
callq 0x388c
movq %rax, %r15
leaq -0x48(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3b00
callq 0x2250
callq 0x2340
movl %eax, 0xc(%r14)
leaq 0x20(%r14), %rdi
callq 0x2390
movq %rax, 0x10(%r14)
callq 0x20a0
testl %eax, %eax
js 0x3cde
je 0x3cf4
movl %eax, %r12d
movl %eax, 0x8(%r14)
callq 0x2340
leaq -0x34(%rbp), %rsi
movl %eax, (%rsi)
leaq 0x11c1e(%rip), %rdi     # 0x15760
callq 0x44a4
cmpl $0x0, (%rax)
jne 0x3b66
callq 0x2340
leaq -0x34(%rbp), %rsi
movl %eax, (%rsi)
leaq 0x11c02(%rip), %rdi     # 0x15760
callq 0x44a4
movl %r12d, (%rax)
leaq 0x2ac(%rip), %r15       # 0x3e19
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 0x1141f(%rip), %r15     # 0x14fc8
leaq 0xc53d(%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 0x113ed(%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 0x1132f(%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 0x11301(%rip), %rdi     # 0x14fc8
callq 0x2040
movq %rax, %rdi
callq 0x2190
movq %r14, %rdi
callq 0x4044
jmp 0x3ce5
movl $0xffffffff, -0x38(%rbp) # imm = 0xFFFFFFFF
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x112cd(%rip), %rdi     # 0x14fc8
leaq 0xc640(%rip), %rsi      # 0x10342
movl $0x31, %edx
callq 0x22a0
movq 0x112b5(%rip), %rdi     # 0x14fc8
callq 0x2120
movq 0x112a9(%rip), %rdi     # 0x14fc8
leaq 0xc389(%rip), %rsi      # 0x100af
movl $0x14, %edx
callq 0x22a0
callq 0x2340
movq 0x1128c(%rip), %rdi     # 0x14fc8
movl %eax, %esi
callq 0x23d0
movq %rax, %rdi
callq 0x2120
movq 0x11276(%rip), %rdi     # 0x14fc8
callq 0x2120
movq 0x1126a(%rip), %rdi     # 0x14fc8
leaq 0xc35f(%rip), %rsi      # 0x100c4
movl $0x8, %edx
callq 0x22a0
movq (%r15), %rsi
movq 0x1124f(%rip), %rdi     # 0x14fc8
callq 0x2270
movq %rax, %rdi
callq 0x2120
movq (%r15), %rdi
movq %r15, %rsi
callq 0x2200
movl $0x10, %edi
callq 0x2130
movq %rax, %r15
leaq 0xc328(%rip), %rsi      # 0x100cd
movq %rax, %rdi
callq 0x2420
movq 0x1122c(%rip), %rsi     # 0x14fe0
movq 0x11215(%rip), %rdx     # 0x14fd0
movq %r15, %rdi
callq 0x23c0
movq %rax, %r14
movq %r15, %rdi
callq 0x21d0
jmp 0x3df0
jmp 0x3ded
jmp 0x3ded
movq %rax, %r14
leaq -0x48(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3df0
callq 0x2250
jmp 0x3df0
jmp 0x3ded
movq %rax, %r14
movq (%rbx), %rdi
cmpq %r13, %rdi
je 0x3dfd
callq 0x2250
movq %r14, %rdi
callq 0x23e0
nop