Пример #1
0
int run_job(JOB* job)
{
	int i, **pipes = NULL;

	if (job == NULL || job->cmd == NULL)
		return -1;

	if (job->ncmd > 1)
	{
		pipes = create_pipes(job->ncmd-1);
		if (pipes == NULL)
			return -1;
	}

	job->run_count = job->ncmd;
	job_list_push(job);
	job->pgid = 0;

	for (i = 0; i < job->ncmd; i++)
	{
		int input, output;
		
		if (i == 0)
		{
			if (job->inputfd != -1)
				input = job->inputfd;
			else
				input = 0;
		}
		else
			input = pipes[i-1][0];

		if (i == job->ncmd-1)
		{
			if (job->outputfd != -1)
				output = job->outputfd;
			else
				output = 1;
		}
		else
			output = pipes[i][1];
		
		job->pid[i] = run_cmd(job->cmd[i], input, output, pipes, job->ncmd-1, job->pgid);
		if (job->pid[i] <= 0)
			job->run_count--;		/* Built-in commands and failed executions are not running processes */
		else if (job->pgid == 0)
		{
			job->pgid = job->pid[i];		
			job->lastmodified = time(NULL); /* A new job is a 'recently modified job' to fg/bg default */
		}
	}
	
	destroy_pipes(&pipes, job->ncmd-1);

	if (job->blocking)
		fg_wait(job);
	
	return 0;
}
Пример #2
0
int try_execute(String command, StringList arguments){
    int c_pid;

    //handlers dos controladores
    struct sigaction sigchld; 
    struct sigaction ctrlc; 
    struct sigaction ctrlz; 

    sigchld.sa_flags = SA_SIGINFO; 
    sigchld.sa_sigaction = sigchld_hand; 

    ctrlc.sa_flags = SA_SIGINFO; 
    ctrlc.sa_sigaction = ctrlc_hand; 

    ctrlz.sa_flags = SA_SIGINFO; 
    ctrlz.sa_sigaction = ctrlz_hand; 

    c_pid = fork();
    if (c_pid == 0) {
        //processo filho
        setpgid(0, 0);
        execv(command, to_array(arguments));
    } else {
        /*processo pai
        //tem controle sobre o sinal SIGCHLD, enviado pelo processo filho apos terminar sua execucao 

	  controle dos sinais:
	*/
        sigaction(SIGCHLD, &sigchld, 0); 
        sigaction(SIGINT, &ctrlc, 0); 
        sigaction(SIGTSTP, &ctrlz, 0); 

        //adiciona na lista de jobs
        job_list_push(job_list, new_job(copy_string(int_to_string(c_pid)), parse_proc_name(command)));
        if(background == 1){ 
            return 1;
        }else{
            wait(NULL); 
        }
    }
    return 1;
}