PROC * KERN_fork(PROC *proc) { PROC *new_proc; new_proc = PROC_create(); PROC_copy(proc, new_proc); new_proc->ppid = proc->pid; new_proc->pid = new_pid(); if (proc->list == NULL) { /* parent was running */ PROC_LIST_append(run_queue, new_proc); } else { PROC_LIST_append(proc->list, new_proc); } return new_proc; }
PROC * KERN_init(void) { /* create all the different queues. a process is either in one of these queues or it's in "cur_proc" */ run_queue = PROC_LIST_create(); sleep_queue = PROC_LIST_create(); zombie_queue = PROC_LIST_create(); /* no process running */ cur_proc = NULL; /* make the "init" process, parent of all processes */ init_proc = PROC_create(); init_proc->pid = new_pid(); init_proc->state = STATE_RUN; PROC_LIST_append(run_queue, init_proc); return init_proc; }
int main(int argc, char *argv[]) { CREW crew; int i; int count = 0; int res; BOOLEAN result; char ** keys; void * statusp; C = new_conf(); parse_cmdline_cfg(C, argc, argv); parse_cfgfile(C); parse_cmdline(C, argc, argv); RUNNER R = new_runner(conf_get_user(C), conf_get_group(C)); runas(R); runner_destroy(R); sigmasker(); if (is_daemon(C)) { res = fork(); if (res == -1 ){ // ERRROR NOTIFY(FATAL, "%s: [error] unable to run in the background\n", program_name); } else if (res == 0) { // CHILD PID P = new_pid(conf_get_pidfile(C)); HASH H = conf_get_items(C); count = conf_get_count(C); keys = hash_get_keys_delim(H, ':'); if ((crew = new_crew(count, count, FALSE)) == NULL) { NOTIFY(FATAL, "%s: [error] unable to allocate memory for %d log files", program_name, count); } set_pid(P, getpid()); pid_destroy(P); for (i = 0; i < count && crew_get_shutdown(crew) != TRUE; i++) { FIDO F = new_fido(C, keys[i]); result = crew_add(crew, (void*)start, F); if (result == FALSE) { NOTIFY(FATAL, "%s: [error] unable to spawn additional threads", program_name); } } crew_join(crew, TRUE, &statusp); conf_destroy(C); } else { // PARENT } } else { HASH H = conf_get_items(C); count = conf_get_count(C); keys = hash_get_keys_delim(H, ':'); if ((crew = new_crew(count, count, FALSE)) == NULL) { NOTIFY(FATAL, "%s: [error] unable to allocate memory for %d log files", program_name, count); } for (i = 0; i < count && crew_get_shutdown(crew) != TRUE; i++) { FIDO F = new_fido(C, keys[i]); result = crew_add(crew, (void*)start, F); } crew_join(crew, TRUE, &statusp); conf_destroy(C); } exit(EXIT_SUCCESS); } /* end of int main **/