// File: /workspace/github_c_makefile_star2_O2/buf1024[P]mysch/mysch.c
// Function: mytask
int mytask()
{
    while(1) {
        sleep(g_info.sleep_time);

        if(g_usr1) {
            LOG_DEBUG("catch usr1\n");
            handle_sigusr1();
        }
        if(g_usr2) {
            LOG_DEBUG("catch usr2\n");
            log_flush();
            g_usr2 = 0;
        }
        if(g_chld) {
            LOG_DEBUG("catch child\n");
            handle_sigchild();
        }
        if(g_term) {
            LOG_DEBUG("catch term\n");
            break;
        }

        list_iter* it = list_get_iterator(g_info.progq, AL_START_HEAD);
        list_node* n = NULL;

        while((n = list_next(it)) != NULL) {
            prog_t* p = list_node_value(n);
            if(p->flag < 0) {
                if(p->pid == 0) {
                    // 启动程序
                    run_prog(p);
                    break;
                }
                if(!p->has_pid_file) {
                    if (kill(p->pid, 0) == 0) {
                        // 等待程序结束
                        break;
                    }
                }
                // 程序已经结束，继续
                list_del_node(g_info.progq, n, AL_FREE);

            }else if(p->flag == FORK_ONCE || p->flag == FORK_PERIOD) {
				if (p->flag == FORK_PERIOD) {
					if (p->pid && kill(p->pid, 0) == 0) {
						continue;
					}
	                if (judge_condition(p)) {
	                    run_prog(p);
	                    // 程序已经启动，不管它生死存亡
	                    list_del_node(g_info.progq, n, AL_FREE);
	                    continue;
	                }

				}
                run_prog(p);
                // 程序已经启动，不管它生死存亡
                list_del_node(g_info.progq, n, AL_FREE);
            }else {
                prog_t* f = (prog_t*)
                    list_node_value(list_first(g_info.progq));
                if(f->flag < 0) {
                    // 等待程序结束
                    continue;
                }

                if (judge_condition(p)) {
                    if (p->pid > 0) {
                        if(update_pid(p) != 0) {
                            LOG_ERROR("update_pid failed.\n");
                            continue;
                        }
                        if (kill(p->pid, 0) == 0) {
                            // 程序正运行
                            continue;
                        }
                    }
                    run_prog(p);
                }else{
                    if (p->pid > 0) {
                        if (update_pid(p) != 0) {
                            LOG_ERROR("update_pid failed.\n");
                            continue;
                        }
                        if (kill(p->pid, 0) == 0) {
                            // 程序正运行,干掉它
                            LOG_INFO("killing %d\n", p->pid);
                            kill(p->pid, SIGTERM);
                            waitpid(p->pid, NULL, 0);
                            LOG_INFO("%d killed\n", p->pid);

                            dict_delete(g_info.waitq, &p, sizeof(pid_t));
                        }
                    }
                }
            }
        }

        list_release_iterator(it);
    }

    clean_proccess(g_info.kill_flag);

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
leaq 0xbd4(%rip), %r14       # 0x76eb
leaq 0xbd9(%rip), %r15       # 0x76f7
leaq 0xbeb(%rip), %r13       # 0x7710
movl 0x4a3d(%rip), %edi      # 0xb568
callq 0x2380
cmpb $0x1, 0x4c8d(%rip)      # 0xb7c4
jne 0x6b4c
leaq 0xb9f(%rip), %rdi       # 0x76df
xorl %eax, %eax
callq 0x3fa2
callq 0x673d
cmpb $0x1, 0x4c75(%rip)      # 0xb7c8
jne 0x6b6d
movq %r14, %rdi
xorl %eax, %eax
callq 0x3fa2
xorl %eax, %eax
callq 0x3f18
movb $0x0, 0x4c5b(%rip)      # 0xb7c8
cmpb $0x1, 0x4c4c(%rip)      # 0xb7c0
jne 0x6b85
movq %r15, %rdi
xorl %eax, %eax
callq 0x3fa2
callq 0x669b
cmpb $0x1, 0x4c40(%rip)      # 0xb7cc
je 0x6d2e
movq 0x4c17(%rip), %rdi      # 0xb7b0
xorl %esi, %esi
callq 0x32e5
movq %rax, %r12
movq %r12, %rdi
callq 0x3342
testq %rax, %rax
je 0x6d21
movq %rax, %rbx
movq 0x10(%rax), %rdi
movq %rdi, -0x30(%rbp)
movl 0xa50(%rdi), %eax
testl %eax, %eax
js 0x6bf9
je 0x6bf2
cmpl $0x2, %eax
jne 0x6c30
movl (%rdi), %eax
testl %eax, %eax
je 0x6be7
movl %eax, %edi
xorl %esi, %esi
callq 0x2260
testl %eax, %eax
je 0x6ba3
movq -0x30(%rbp), %rdi
callq 0x65a2
movq -0x30(%rbp), %rdi
testl %eax, %eax
callq 0x6843
jmp 0x6c1a
movl (%rdi), %eax
testl %eax, %eax
je 0x6d1c
cmpl $0x0, 0x8(%rdi)
jne 0x6c1a
movl %eax, %edi
xorl %esi, %esi
callq 0x2260
testl %eax, %eax
je 0x6d21
movq 0x4b8f(%rip), %rdi      # 0xb7b0
movq %rbx, %rsi
xorl %edx, %edx
callq 0x3297
jmp 0x6ba3
movq 0x4b79(%rip), %rax      # 0xb7b0
movq (%rax), %rax
movq 0x10(%rax), %rax
cmpl $0x0, 0xa50(%rax)
js 0x6ba3
callq 0x65a2
movq -0x30(%rbp), %rdi
movl (%rdi), %ecx
testl %eax, %eax
je 0x6c8a
testl %ecx, %ecx
jle 0x6c80
callq 0x6a21
testl %eax, %eax
jne 0x6c9b
movq -0x30(%rbp), %rax
movl (%rax), %edi
xorl %esi, %esi
callq 0x2260
testl %eax, %eax
je 0x6ba3
movq -0x30(%rbp), %rdi
callq 0x6843
jmp 0x6ba3
testl %ecx, %ecx
jle 0x6ba3
callq 0x6a21
testl %eax, %eax
je 0x6caa
movq %r13, %rdi
xorl %eax, %eax
callq 0x43ba
jmp 0x6ba3
movq -0x30(%rbp), %rax
movl (%rax), %edi
xorl %esi, %esi
callq 0x2260
testl %eax, %eax
jne 0x6ba3
movq -0x30(%rbp), %rax
movl (%rax), %esi
leaq 0x912(%rip), %rdi       # 0x75de
xorl %eax, %eax
callq 0x4266
movq -0x30(%rbp), %rax
movl (%rax), %edi
pushq $0xf
popq %rsi
callq 0x2260
movq -0x30(%rbp), %rax
movl (%rax), %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x22e0
movq -0x30(%rbp), %rax
movl (%rax), %esi
leaq 0x8ed(%rip), %rdi       # 0x75ea
xorl %eax, %eax
callq 0x4266
movq 0x4aad(%rip), %rdi      # 0xb7b8
leaq -0x30(%rbp), %rsi
pushq $0x4
popq %rdx
callq 0x2a8d
jmp 0x6ba3
callq 0x6843
movq %r12, %rdi
callq 0x3314
jmp 0x6b25
leaq 0x9cf(%rip), %rdi       # 0x7704
xorl %eax, %eax
callq 0x3fa2
movl 0x482a(%rip), %edi      # 0xb56c
callq 0x6783
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq