Offspring *copy_child(Offspring *child, int m) { int ia, twom; Offspring *res; res = new_child(m); if (!res) return (Offspring *) 0; res->id = child->id; res->affected = child->affected; res->sex = child->sex; res->qt = child->qt; twom = 2*m; for (ia=0; ia<twom; ia++) { res->markers[ia] = child->markers[ia]; } return res; }
void create_children(Trie *v) { int dim, val, max_child_nrules = 0; Band *cut; Trie *u; if (v->depth >= MAX_DEPTH-1) { //dump_path(v, 2); return; } if (v->depth > 0) { for (dim = 0; dim < NFIELDS; dim++) dfs_uncuts[v->depth][dim] = dfs_uncuts[v->depth-1][dim]; } choose_cut(v); cut = &dfs_cuts[v->depth]; dfs_uncuts[v->depth][cut->dim]--; if (v->nrules <= REDUN_NRULES) calc_rule_redun(v, cut); for (val = 0; val < BAND_SIZE; val++) { cut->val = val; u = new_child(v, cut); if (u == NULL) continue; if (u->nrules > LEAF_RULES) { create_children(u); } else if (v->depth > max_depth) { max_depth = v->depth; max_depth_leaf = u; } if (u->nrules > max_child_nrules) max_child_nrules = u->nrules; } calc_cut_efficiency(v->depth, v->nrules, max_child_nrules); depth_max_node[v->depth+1] = max_child_nrules; v->children = realloc(v->children, v->nchildren*sizeof(Trie)); dfs_uncuts[v->depth][cut->dim]++; }
static VALUE node_insert_child(VALUE rcv, SEL sel, VALUE idx, VALUE child) { rb_ary_insert(NODE(rcv)->children, NUM2INT(idx), child); return new_child(rcv, child); }
static VALUE node_add_child(VALUE rcv, SEL sel, VALUE child) { rb_ary_push(NODE(rcv)->children, child); return new_child(rcv, child); }
Family *nuclear(int n, int *ped, int *mem, int *father, int *mother, int *sex, int *aff_status, double *qt, int m, int *markers) { int person, j, pj, twom, placed, prev_placed; int p_ped, p_fat, p_mot, p_mem; Family *res, *f, *f2add, *f_last=NULL; Offspring *child, *prev; twom = 2*m; res = (Family *) 0; /* Repeatedly pass through persons until no-one else can be placed */ prev_placed = -1; placed = 0; while (placed > prev_placed) { prev_placed = placed; for (person=0; person<n; person++) { p_ped = ped[person]; p_mem = mem[person]; p_fat = father[person]; p_mot = mother[person]; /* If person is an offspring ... */ if (p_fat && p_mot) { /* See if this person is a child of an existing family */ for (f2add=(Family *) 0, f=res; f; f= f->next) { if ((p_ped==f->pedigree) && (p_fat==f->father_id) && (p_mot==f->mother_id)) { f2add = f; break; } } /* If not, create new family record */ if (!f2add) { f2add = new_family(m); if (!f2add) goto overflow; f2add->pedigree = p_ped; f2add->father_id = p_fat; f2add->mother_id = p_mot; if (res) f_last->next = f2add; else res = f2add; f_last = f2add; } /* Add child into family */ prev = (Offspring *) 0; for (child=f2add->children; child; child=child->next) prev = child; child = new_child(m); if (!child) goto overflow; if (prev) prev->next = child; else f2add->children = child; child->id = p_mem; child->affected = aff_status[person]; child->qt = qt? qt[person]: 0.0; child->sex = sex? sex[person]: 0; for (pj=person, j=0; j<twom; j++, pj+=n) child->markers[j] = markers[pj]; father[person] = mother[person] = 0; placed++; /* Person placed as an offspring */ } /* Whether or not an offspring, anyone can be a parent ... */ for (f=res; f; f= f->next) { if (p_ped==f->pedigree) { /* If family missing father, is this him? */ if (!(f->check % 2) && (p_mem==f->father_id)) { if (sex && sex[person]!=1) { warn("Father not male", f); } else { f->check++; for (pj=person, j=0; j<twom; j++, pj+=n) f->father[j] = markers[pj]; } } /* If family missing mother, is this her? */ if (!(f->check / 2) && (p_mem==f->mother_id)) { if (sex && sex[person]!=2) { warn("Mother not female", f); } else { f->check+=2; for (pj=person, j=0; j<twom; j++, pj+=n) f->mother[j] = markers[pj]; } } } } } } return res; overflow: warn("Memory overflow while or after storing family", f_last); return NULL; }
void spawn_job(job_t *j, bool fg) { pid_t pid; process_t *p; int countProcess = 0; int loopCounter = -1; int i; //count number of processes in the job for(p = j->first_process; p; p = p->next) { countProcess++; } //printf("count process: %d \n", countProcess); int pipefd[countProcess-1][2]; //initialize 2-dimensional array [number of process][input/output] //printf("test"); //open all pipes if(countProcess > 1) { for(i = 0; i < (countProcess-1); i++) { //printf("piped %d \n", i); pipe(pipefd[i]); } } for(p = j->first_process; p; p = p->next) { /* YOUR CODE HERE? */ /* Builtin commands are already taken care earlier */ loopCounter++; switch (pid = fork()) { // int status; case -1: /* fork failure */ perror("fork"); exit(EXIT_FAILURE); case 0: /* child process */ p->pid = getpid(); new_child(j, p, fg); /* YOUR CODE HERE? Child-side code for new process. */ if (p->ifile != NULL) { int fd = open(p->ifile, O_RDONLY); dup2(fd, STDIN_FILENO); } if (p->ofile != NULL) { int fd = open(p->ofile, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); dup2(fd, STDOUT_FILENO); } //pipe if(countProcess > 1) { //printf("begin piping \n"); //first process is output //need to loop through number of process and close all pipes if(p == j->first_process) { dup2(pipefd[loopCounter][1], 1); //duplicate output as output for next process closePipe(pipefd, countProcess); } //last process else if (p->next == NULL) { dup2(pipefd[loopCounter-1][0], 0); closePipe(pipefd, countProcess); } //for middle: dup both input and output else { dup2(pipefd[loopCounter][1], 1); dup2(pipefd[loopCounter-1][0], 0); closePipe(pipefd, countProcess); } } printf("%d (Launched): %s\n", j->pgid, j->commandinfo); execvp(p->argv[0], p->argv); perror("New child should have done an exec"); exit(EXIT_FAILURE); /* NOT REACHED */ break; /* NOT REACHED */ default: /* parent */ /* establish child process group */ p->pid = pid; set_child_pgid(j, p); /* YOUR CODE HERE? Parent-side code for new process. */ if (p->next == NULL) //if last process { process_t *p2; p2 = j->first_process; while(p2 != NULL) { //printf("waiting for process \n"); //if fg true waiting, else bg stuff if(fg == true) { waiting(p2); } else { struct sigaction action; action.sa_sigaction = sighandler; sigfillset(&action.sa_mask); action.sa_flags = SA_SIGINFO; sigaction(SIGCHLD, &action, NULL); } closePipe(pipefd, countProcess); p2 = p2->next; } } //wait for child to finish } /* YOUR CODE HERE? Parent-side code for new job.*/ //printf("assign terminal back to dsh\n"); seize_tty(getpid()); // assign the terminal back to dsh } }