/* * "THE CHEESE-WARE LICENSE" (Revision 1): * wrote this file. As long as you retain this notice * you can do whatever you want with this stuff. If we meet some day, * and you think this stuff is worth it, you can buy me some cheese * in return */ #include #include #include #include #include #include "common.h" #define PROG_PREFIX "father: " #define log(fmt, args...) printf(PROG_PREFIX fmt "\n", ##args) #define CMD_SIZE 256 int main(int argc, const char **argv) { if (argc != 2) { log("Wrong program usage. 1 argument shuld be passed - program mode"); log("%d - wait for child", WAIT_FOR_CHILD); log("%d - father doesn't wait for child", DONT_WAIT_FOR_CHILD); log("%d - father doesn't wait for child, but child finishes", ZOMBIE_PROCESS); return EXIT_FAILURE; } long mode = strtol(argv[1], NULL, 10); if (fork() == 0) { execl("son", "son", argv[1], NULL); } char cmd[CMD_SIZE] = {0}; snprintf(cmd, CMD_SIZE, "ps fx > task_dump-%ld", mode); if (mode == WAIT_FOR_CHILD) { system(cmd); int status = 0; int ret = wait(&status); if (ret < 0) { log("waiting for son ended with error code %d", ret); return EXIT_FAILURE; } } else if (mode == DONT_WAIT_FOR_CHILD) { system(cmd); } else if (mode == ZOMBIE_PROCESS) { sleep(2); system(cmd); } else { log("Invalid argument. No mode correspoiding to %ld", mode); return EXIT_FAILURE; } return EXIT_SUCCESS; }