// File: /workspace/github_c_makefile_star2_O1/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 0x14cf(%rip), %r14      # 0x8709
leaq 0x14d4(%rip), %r15      # 0x8715
leaq 0x14e6(%rip), %r13      # 0x872e
movl 0x531a(%rip), %edi      # 0xc568
callq 0x2380
cmpb $0x1, 0x556a(%rip)      # 0xc7c4
jne 0x726f
leaq 0x149a(%rip), %rdi      # 0x86fd
xorl %eax, %eax
callq 0x4433
callq 0x6e3c
cmpb $0x1, 0x5552(%rip)      # 0xc7c8
jne 0x7290
movq %r14, %rdi
xorl %eax, %eax
callq 0x4433
xorl %eax, %eax
callq 0x43e2
movb $0x0, 0x5538(%rip)      # 0xc7c8
cmpb $0x1, 0x5529(%rip)      # 0xc7c0
jne 0x72a8
movq %r15, %rdi
xorl %eax, %eax
callq 0x4433
callq 0x6d7b
cmpb $0x1, 0x551d(%rip)      # 0xc7cc
je 0x7458
movq 0x54f4(%rip), %rdi      # 0xc7b0
xorl %esi, %esi
callq 0x3670
movq %rax, %r12
movq %rax, %rdi
callq 0x36d6
testq %rax, %rax
je 0x744b
movq %rax, %rbx
movq 0x10(%rbx), %rdi
movq %rdi, -0x30(%rbp)
movl 0xa50(%rdi), %eax
testl %eax, %eax
js 0x731a
je 0x730f
cmpl $0x2, %eax
jne 0x7365
movl (%rdi), %edi
testl %edi, %edi
je 0x7304
xorl %esi, %esi
callq 0x2260
testl %eax, %eax
je 0x734c
movq -0x30(%rbp), %rdi
callq 0x6c67
testl %eax, %eax
movq -0x30(%rbp), %rdi
callq 0x6f58
jmp 0x733b
movl (%rdi), %eax
testl %eax, %eax
je 0x7446
cmpl $0x0, 0x8(%rdi)
jne 0x733b
movl %eax, %edi
xorl %esi, %esi
callq 0x2260
testl %eax, %eax
je 0x744b
movq 0x546e(%rip), %rdi      # 0xc7b0
movq %rbx, %rsi
xorl %edx, %edx
callq 0x361e
movq %r12, %rdi
callq 0x36d6
movq %rax, %rbx
testq %rax, %rax
jne 0x72da
jmp 0x744b
movq 0x5444(%rip), %rax      # 0xc7b0
movq (%rax), %rax
movq 0x10(%rax), %rax
cmpl $0x0, 0xa50(%rax)
js 0x734c
callq 0x6c67
movq -0x30(%rbp), %rdi
movl (%rdi), %ecx
testl %eax, %eax
je 0x73b4
testl %ecx, %ecx
jle 0x73a9
callq 0x7140
testl %eax, %eax
jne 0x73c1
movq -0x30(%rbp), %rax
movl (%rax), %edi
xorl %esi, %esi
callq 0x2260
testl %eax, %eax
je 0x734c
movq -0x30(%rbp), %rdi
callq 0x6f58
jmp 0x734c
testl %ecx, %ecx
jle 0x734c
callq 0x7140
testl %eax, %eax
je 0x73d0
movq %r13, %rdi
xorl %eax, %eax
callq 0x4838
jmp 0x734c
movq -0x30(%rbp), %rax
movl (%rax), %edi
xorl %esi, %esi
callq 0x2260
testl %eax, %eax
jne 0x734c
movq -0x30(%rbp), %rax
movl (%rax), %esi
leaq 0x120a(%rip), %rdi      # 0x85fc
xorl %eax, %eax
callq 0x46e4
movq -0x30(%rbp), %rax
movl (%rax), %edi
movl $0xf, %esi
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 0x11e3(%rip), %rdi      # 0x8608
xorl %eax, %eax
callq 0x46e4
movq 0x5385(%rip), %rdi      # 0xc7b8
leaq -0x30(%rbp), %rsi
movl $0x4, %edx
callq 0x2d60
jmp 0x734c
callq 0x6f58
movq %r12, %rdi
callq 0x36a5
jmp 0x7248
leaq 0x12c3(%rip), %rdi      # 0x8722
xorl %eax, %eax
callq 0x4433
movl 0x5100(%rip), %edi      # 0xc56c
callq 0x6e86
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq