int main(int argc, char *argv[]) { pid_t pid; int status; struct tree_node *root; if (argc != 2) { fprintf(stderr, "Usage: %s <input_tree_file>\n\n", argv[0]); exit(1); } root = get_tree_from_file(argv[1]); print_tree(root); pid=fork(); if(pid<0){ perror("main: fork"); exit(1); } else if(pid==0){ fork_procs(root); exit(1); } wait_for_ready_children(1); show_pstree(pid); kill(pid,SIGCONT); pid=wait(&status); explain_wait_status(pid,status); return 0; }
int main(int argc, char **argv) { FILE *f, *o; int procs, i; char *word; struct timeval begin, end; float time; if (argc < 2) return EXIT_FAILURE; gettimeofday(&begin, NULL); if (argc < 3) f = fopen("books/book1.txt", "r"); else f = fopen(argv[2], "r"); if (f == NULL) return EXIT_FAILURE; procs = atoi(argv[1]); int pipes[procs][2]; int p_outs[procs][2]; if (fork_procs(procs, pipes, p_outs) == -1) return EXIT_FAILURE; deal_words(f, pipes, procs); for (i = 0; i < procs; i++) close(pipes[i][1]); for (i = 0; i < procs+1; i++) wait(NULL); fclose(f); gettimeofday(&end, NULL); printf("Start: %f\n", begin.tv_usec/1000.0); printf("End: %f\n", end.tv_usec/1000.0); time = (end.tv_usec-begin.tv_usec)/1000.0; if (time < 0) time = (1000000+end.tv_usec-begin.tv_usec)/1000.0; printf("Time: %f\n", time); return EXIT_SUCCESS; }
void fork_procs(struct tree_node *root) { pid_t child ; int i,status ; pid_t* children; children = (pid_t*)malloc((sizeof(pid_t)*root->nr_children)); if (children == NULL){ fprintf(stderr, "allocate children failed\n"); exit(1); } printf("PID = %ld, name %s, starting...\n",(long)getpid(), root->name); change_pname(root->name); for (i = 0 ; i < root->nr_children; i++){ printf("%s:Creating child %s...\n",root->name,root->children[i].name); child = fork(); if (child < 0) { perror("child fork"); exit(1); } if (child == 0) { /* Child */ fork_procs(&root->children[i]); } /* Father saves child's pid ! */ children[i] = child; } wait_for_ready_children(root->nr_children); printf("%s: All children ready ! Suspending with SIGSTOP...\n",root->name); /* * Suspend Self */ raise(SIGSTOP); printf("PID = %ld, name = %s is awake\n",(long)getpid(), root->name); for (i = 0 ; i < root->nr_children;i++){ printf("%s: Activating child %s...\n",root->name,root->children[i].name); kill(children[i],SIGCONT); children[i] = wait(&status); explain_wait_status(children[i],status); } printf("%s: Exiting...\n",root->name); free(children); exit(0); }
int main(int argc, char *argv[]) { pid_t pid; int status; struct tree_node *root; if (argc < 2){ fprintf(stderr, "Usage: %s <tree_file>\n", argv[0]); exit(1); } /* Read tree into memory */ root = get_tree_from_file(argv[1]); /* Fork root of process tree */ pid = fork(); if (pid < 0) { perror("main: fork"); exit(1); } if (pid == 0) { /* Child */ fork_procs(root); exit(1); } /* * Father */ /* for ask2-signals */ wait_for_ready_children(1); /* Print the process tree root at pid */ show_pstree(pid); /* for ask2-signals */ kill(pid, SIGCONT); /* Wait for the root of the process tree to terminate */ pid = wait(&status); explain_wait_status(pid, status); return 0; }
void fork_procs(struct tree_node *current){ pid_t p_new[20]; int status; int i; struct tree_node *temp; change_pname(current->name); printf("%s:Starting..\n",current->name); for(i=0;i<current->nr_children;i++){ p_new[i]=fork(); if(p_new[i]<0){ perror("fork"); exit(2); } else if(p_new[i]==0){ fork_procs(current->children+i); } } wait_for_ready_children(current->nr_children); printf("%s:I gave birth to %d childs and then suspended myself..\n",current->name,current->nr_children); raise(SIGSTOP); printf("%s:Before you know,awake!..\n",current->name); for(i=0;i<current->nr_children;i++){ temp=current->children+i; printf("I, %s , am your father %s,and i say wake!\n",current->name,temp->name); kill(p_new[i],SIGCONT); p_new[i]=wait(&status); explain_wait_status(p_new[i],status); } printf("%s:Exiting..\n",current->name); exit(3); }
/********************************************************************* * new_pgrg() - handle the creation of the pgrp managers and their * children ********************************************************************/ void manager(int num_procs) { struct sigaction sa; /* Wait for the parent to change our pgid before we start forking */ while (!create_procs_flag) { alarm(1); if (debug_flag >= 3) printf("%d: Manager pausing, not cleared to fork\n", mypid); pause(); } /* set up the signal handling the children will use */ /* ignore HUP */ sa.sa_handler = SIG_IGN; sigemptyset(&sa.sa_mask); sa.sa_flags = 0; if (debug_flag >= 4) printf("%d: setting SIGHUP -> SIG_IGN\n", mypid); k_sigaction(SIGHUP, &sa, 0); /* We use ALRM to make sure that we don't miss the signal effects ! */ sa.sa_handler = wakeup; if (debug_flag >= 4) printf("%d: setting SIGALRM -> wakeup\n", mypid); k_sigaction(SIGALRM, &sa, 0); /* exit on QUIT */ sa.sa_handler = graceful_exit; if (debug_flag >= 4) printf("%d: setting SIGQUIT -> graceful_exit\n", mypid); k_sigaction(SIGQUIT, &sa, 0); /* start signaling on USR1 */ sa.sa_handler = set_signal_parents; sigfillset(&sa.sa_mask); if (debug_flag >= 7) printf("%d: setting SIGUSR1 -> set_signal_parents\n", mypid); k_sigaction(SIGUSR1, &sa, 0); /* stop signaling on USR2 */ sa.sa_handler = clear_signal_parents; if (debug_flag >= 7) printf("%d: setting SIGUSR2 -> clear_signal_parents\n", mypid); k_sigaction(SIGUSR2, &sa, 0); fork_procs(num_procs); sleep(1); /* wait a sec to let all the children pause */ /* now set up my signal handling */ /* continue on ALRM */ sa.sa_handler = wakeup; if (debug_flag >= 4) printf("%d: setting SIGALRM -> wakeup\n", mypid); k_sigaction(SIGALRM, &sa, 0); /* mark ready confirmation on USR1 */ sa.sa_handler = set_confirmed_ready; if (debug_flag >= 4) printf("%d: setting SIGUSR1 -> set_confirmed_ready\n", mypid); k_sigaction(SIGUSR1, &sa, 0); /* reset our counter on HUP */ sa.sa_handler = reset_counter; if (debug_flag >= 4) printf("%d: setting SIGHUP -> reset_counter\n", mypid); k_sigaction(SIGHUP, &sa, 0); /* reply to child on USR2 */ sa.sa_handler = NULL; sa.sa_sigaction = reply_to_child; sa.sa_flags = SA_SIGINFO; if (debug_flag >= 4) printf("%d: setting SIGUSR2 -> reply_to_child\n", mypid); k_sigaction(SIGUSR2, &sa, 0); /* tell our parent that we are ready to rock */ while (!confirmed_ready_flag) { if (debug_flag >= 3) printf("%d: Manager, SIGUSR1 -> %d\n", mypid, getppid()); if (kill(getppid(), SIGUSR1) == -1) { tst_resm(TWARN, "%d: Couldn't signal master (%d) that we're ready. %d: %s", mypid, getppid(), errno, strerror(errno)); exit(errno); } usleep(100); } /* handle pgroup management while the tests are running */ while (1) { alarm(1); if (debug_flag >= 5) printf("%d: Manager pausing (%d/%d)\n", mypid, child_signal_counter, num_procs); pause(); if (child_signal_counter >= num_procs) { confirmed_ready_flag = 0; tst_resm(TINFO, "%d: All %d children reported in", mypid, child_signal_counter); while (child_signal_counter) { if (debug_flag >= 3) printf("%d: Manager, SIGUSR2 -> %d\n", mypid, getppid()); if (kill(getppid(), SIGUSR2) == -1) { tst_resm(TINFO, "%d: Couldn't signal master (%d) that we're ready. %d: %s", mypid, getppid(), errno, strerror(errno)); exit(errno); } usleep(100); } } } }