/* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */ /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */ #include #include #include #include #include #include #include int kill_parent = 0; int main(int argc, char ** argv) { if (argc == 2 && !strcmp("parent", argv[1])) kill_parent = 1; int parent_pid = getpid(); pid_t pid = fork(); if (pid < 0) { printf("failed on fork (%s)\n", strerror(errno)); return -1; } if (pid == 0) { struct timespec rem; rem.tv_sec = kill_parent ? 1 : 60; rem.tv_nsec = 0; printf("[pid=%d|ppid=%d] Going to sleep...\n", getpid(), getppid()); nanosleep(&rem, 0); if (kill_parent) kill(parent_pid, SIGKILL); printf("[pid=%d|ppid=%d] Hello, Dad!\n", getpid(), getppid()); return 0; } else { struct timespec rem; rem.tv_sec = kill_parent ? 60 : 1; rem.tv_nsec = 0; printf("[pid=%d|ppid=%d] Going to sleep...\n", getpid(), getppid()); nanosleep(&rem, 0); if (!kill_parent) kill(pid, SIGKILL); printf("[pid=%d|ppid=%d] Hello, Kid!\n", getpid(), getppid()); return 0; } }