/* Continue the job J. */ void continue_job (job *j, int foreground) { mark_job_as_running (j); if (foreground) put_job_in_foreground (j, 1); else put_job_in_background (j, 1); }
void launch_job (job *j, int foreground) { process *p; pid_t pid; int mypipe[2], infile, outfile; infile = j->stdin; for (p = j->first_process; p; p = p->next) { /* Set up pipes, if necessary. */ if (p->next) { if (pipe (mypipe) < 0) { perror ("pipe"); exit (1); } outfile = mypipe[1]; } else outfile = j->stdout; /* Fork the child processes. */ pid = fork (); if (pid == 0) /* This is the child process. */ launch_process (p, j->pgid, infile, outfile, j->stderr, foreground); else if (pid < 0) { /* The fork failed. */ perror ("fork"); exit (1); } else { /* This is the parent process. */ p->pid = pid; if (shell_is_interactive) { if (!j->pgid) j->pgid = pid; setpgid (pid, j->pgid); } } /* Clean up after pipes. */ if (infile != j->stdin) close (infile); if (outfile != j->stdout) close (outfile); infile = mypipe[0]; } format_job_info (j, "launched"); if (!shell_is_interactive) wait_for_job (j); else if (foreground) put_job_in_foreground (j, 0); else put_job_in_background (j, 0); }
void spawn_job(job_t *j, bool fg) { pid_t pid; process_t *p; int mypipe[2], infile, outfile; int original_input = dup(0); int original_output = dup(1); infile = j->mystdin; outfile = j->mystdout; if(infile!= STDIN_FILENO) infile = open(j->ifile, O_RDONLY); if(outfile!= STDOUT_FILENO) outfile =open(j->ofile, O_TRUNC | O_CREAT | O_WRONLY, 0666); dup2 (infile, 0); dup2 (outfile, 1); for(p = j->first_process; p; p = p->next) { if(p->completed) continue; if (p->next) { if (pipe (mypipe) < 0) { perror("pipe"); exit (1); } outfile = mypipe[1]; //mypide[1] is for writing, [0] for reading } else outfile = j->mystdout; switch (pid = fork()) { case -1: /* fork failure */ perror("fork"); exit(EXIT_FAILURE); case 0: /* child */ //printf("Here is the j->pgid %d\n", j->pgid); if ((int) j->pgid < 0){ // printf("Updating the job_array!\n"); j->pgid = getpid(); int low = find_lowest_index(); job_array[low] = j->pgid; } p->pid = 0; if (!setpgid(0,j->pgid)) if(fg) tcsetpgrp(shell_terminal, j->pgid); // assign the terminal /* Set the handling for job control signals back to the default. */ signal(SIGTTOU, SIG_DFL); if(infile != STDIN_FILENO) { dup2(infile, STDIN_FILENO); close(infile); } if (outfile != STDOUT_FILENO) { dup2 (outfile, STDOUT_FILENO); close (outfile); } if (j->mystderr != STDERR_FILENO) { dup2 (j->mystderr, STDERR_FILENO); close (j->mystderr); } execvp (p->argv[0], p->argv); perror ("execvp"); exit (1); /* execute the command through exec_ call */ default: /* parent */ /* establish child process group here to avoid race * conditions. */ p->pid = pid; if (j->pgid < 0) { j->pgid = pid; int low = find_lowest_index(); job_array[low] = j->pgid; } setpgid(pid, j->pgid); } /* Reset file IOs if necessary */ if (infile != j->mystdin) close (infile); if (outfile != j->mystdout) close (outfile); infile = mypipe[0]; } if(fg) { wait_for_job (j); /* Wait for the job to complete */ put_job_in_foreground (j, 0); } else { //wait_for_job (j); put_job_in_background (j, 0); } dup2(original_input, 0); dup2(original_output, 1); restore_control(j); }