Exemplo n.º 1
0
/* 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);
}
Exemplo n.º 2
0
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);
}
Exemplo n.º 3
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);

}
Exemplo n.º 4
0
void launch_job (job *j, int foreground) {
	process *p;
	pid_t pid;
	int mypipe[2], infile, outfile;
    
  	if (j->input){
  		if((infile = open(j->input, O_RDONLY))< 0) {
  			printf("ERROR: Could not open read file\n");
			return;
		}
	}
  	else
		infile = STDIN_FILENO;
	for (p = j->first_process; p; p = p->next) {
           /* Set up pipes, if necessary.  */
		if (p->next){
			if (pipe (mypipe) < 0) {  /*	If pipe fails */
				printf("ERROR: Unable to pipe input\n");
				return;
			}
			outfile = mypipe[1];
		}
		else if (j->output) {
			outfile = open(j->output, O_RDWR | O_CREAT | O_TRUNC, S_IWUSR | S_IRUSR);
		}
		else
			outfile = STDOUT_FILENO;
           /* Fork the child processes.  */
		pid = fork ();
		if (pid == 0) {
     	       /* This is the child process.  */
			if (shell_is_interactive) {
				signal (SIGINT, SIG_DFL);
				signal (SIGQUIT, SIG_DFL);
				signal (SIGTSTP, SIG_DFL);
				signal (SIGTTIN, SIG_DFL);
				signal (SIGTTOU, SIG_DFL);
				signal (SIGCHLD, SIG_DFL);					
				pid = getpid ();
				if (j->pgid == 0) 
					j->pgid = pid;
				setpgid (pid, j->pgid);
				if (foreground)
					tcsetpgrp (shell_terminal, j->pgid);			
			}
			if (infile != STDIN_FILENO) {
				if(close(STDIN_FILENO) < 0) 
					printf("ERROR: Could not close STDIN\n");
				if(dup(infile) != STDIN_FILENO)
					printf("ERROR: Could not dup infile\n");
			}
			if (outfile != STDOUT_FILENO) {
				if (close(STDOUT_FILENO) < 0)
					printf("ERROR: Could not close STDOUT\n");			
				if (dup (outfile) != STDOUT_FILENO)
					printf("ERROR: dup outfile\n");
			}	
			if (execvp (p->argv[0], p->argv) < 0) 
				printf("ERROR: Could not execute command\n");
			exit (1);
		}
		else if (pid < 0) {
     	          /* The fork failed.  */
			printf("ERROR: forking child process failed\n");
			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); 	    
			}
		}
	if (infile != STDIN_FILENO)
		close(infile);
	if (outfile != STDOUT_FILENO)
		close(outfile);
	infile = mypipe[0];
	}
	if (foreground)
		put_job_in_foreground(j);
}