#include #include #include #include #include // need a command loop that accepts three commands: // 1) list 2) forkoff 3) exit // fork off children, maintain a list of children, kill children on exit //need a list of children #define MAX_CHILDREN 5 #define MAX_LINE_LENGTH 256 pid_t kids[MAX_CHILDREN]; void insert_child(pid_t); void cleanup_children(void); void do_work(void); void print_children(void); int highest_unused = 0; /** * Iterate over list of children, kill them //signal child to SIGINT //waitpid? */ void cleanup_children() { int i = 0; for(i=0;i "); fscanf(stdin, "%s", line); line[MAX_LINE_LENGTH]='\0'; if (0==strncmp("list",line,4)) { print_children(); } else if(0==strncmp("forkoff",line,7)) { if(highest_unused >= MAX_CHILDREN) { fprintf(stdout, "too many kids, not forking\n"); continue; } pid = fork(); if(0==pid) { //child is executing here while ( 1 ) { sleep(2); } } else if ( pid < 0 ) { //error, could not fork fprintf(stderr, "could not fork\n"); } else { //parent is executing here insert_child(pid); } } else if (0==strncmp("\0",line,1)) { return; } else if (0==strncmp("exit",line,4)) { return; } else { fprintf(stderr, "unknown command\n"); } } free(line); line=NULL; } int main(int argc, char* argv[]) { do_work(); cleanup_children(); return 0; }