Example #1
0
/*
 * execute a given pipeline of processes
 */
pid_t execute(pipeline_t *p) {
	pipeline_t *j;
	pid_t pid;
	int fd[2];
	int in, out, err;

	in = p->stdin;
	for (j = p; j != NULL; j = j->next) {
		if (j->next) {
			if (pipe(fd) < 0) {
				perror("pipe");
				return -1;
			}
			out = fd[1];
			if (j->stdout == j->stderr) {
				err = out;
			}
		} else {
			out = j->stdout;
			err = j->stderr;
		}
		pid = fork();
		if (pid == 0) {
			spawn_job(j, in, out, err);
			return 0;
		} else {
			j->pid = pid;
			if (p->pid <= 0) {
				p->pid = pid;
			}
			setpgid(pid, p->pid);
		}

		/* close all the file descriptors! */
		if (in  != j->stdin ) close(in );
		if (out != j->stdout) close(out);
		if (err != j->stderr) close(err);
		if (j->stdin  > 2) close(j->stdin );
		if (j->stdout > 2) close(j->stdout);
		if (j->stderr > 2) close(j->stderr);

		in = fd[0];
	}

	return pid;
}
Example #2
0
static int receive_command(int sd, int events, void *discard)
{
	int ioc_ret;
	char *buf;
	unsigned long size;

	if (!ioc) {
		ioc = iocache_create(512 * 1024);
	}
	ioc_ret = iocache_read(ioc, sd);

	/* master closed the connection, so we exit */
	if (ioc_ret == 0) {
		iobroker_close(iobs, sd);
		exit_worker();
	}
	if (ioc_ret < 0) {
		/* XXX: handle this somehow */
	}

#if 0
	/* debug-volley */
	buf = iocache_use_size(ioc, ioc_ret);
	write(master_sd, buf, ioc_ret);
	return 0;
#endif
	/*
	 * now loop over all inbound messages in the iocache.
	 * Since KV_TERMINATOR is a nul-byte, they're separated by 3 nuls
	 */
	while ((buf = iocache_use_delim(ioc, MSG_DELIM, MSG_DELIM_LEN_RECV, &size))) {
		struct kvvec *kvv;
		/* we must copy vars here, as we preserve them for the response */
		kvv = buf2kvvec(buf, (unsigned int)size, KV_SEP, PAIR_SEP, KVVEC_COPY);
		if (kvv)
			spawn_job(kvv);
	}

	return 0;
}
Example #3
0
File: dsh.c Project: bsb20/dsh
void execute_job(job_t* job) {
	/*Check commandline for built-in commands*/
    if (check_command(job, "cd")) {         //change directory - alter current directory and remove completed job
        chdir(job->first_process->argv[1]);
        remove_job(job);
    }
    else if (check_command(job, "jobs")) {  //display current jobs and their statuses (stati?)
        remove_job(job);
        builtin_jobs();
    }
    else if (check_command(job, "bg")) {    //resume background job with specified number 
        resume_background_job(atoi(job->first_process->argv[1])); //atoi interprets string as integer value
        remove_job(job);
    }
    else if (check_command(job, "fg")) {    //continue foreground job with specified number 
        resume_foreground_job(atoi(job->first_process->argv[1]));
        remove_job(job);
    } 
    else {                                  //Default case, not built in command - spawn the new job
		spawn_job(job, !job->bg);
    }
}
Example #4
0
int main() {

	init_shell();
	job_array = (pid_t *) malloc(20*sizeof(pid_t));
	while(1) {
		if(!readcmdline(promptmsg())) {
			if (feof(stdin)) { /* End of file (ctrl-d) */
				fflush(stdout);
				printf("\n");
				exit(EXIT_SUCCESS);
             	}
			continue; /* NOOP; user entered return or spaces with return */
		}
		/* Only for debugging purposes and to show parser output */
		//print_job();

		job_t * next_job = first_job;
		while(next_job){
			if(next_job->pgid ==-1){
				int lowest = find_lowest_index();
				//printf("%s %d\n","Lowest index: ", lowest);
				if(lowest != -1){
					bool bg = next_job->bg;
					process_t * p = next_job->first_process;
					char* cmd = p->argv[0];

					if(strcmp (cmd,"cd") == 0){
						//printf("%s\n", "found cd");
					} 
					else if (strcmp (cmd, "jobs") == 0) {
						list_jobs(next_job, 0);
						job_t *tmp = next_job;
						if (next_job->next) next_job=next_job->next;
						remove_and_free(tmp); //why are we remove and freeing a built in command 
					} 

					else if (strcmp(cmd, "fg") == 0) { 

					}

					else if (strcmp(cmd, "bg") == 0) {
						
					}


					else {					/*If not built-in*/
						spawn_job(next_job, !bg);
						next_job = next_job->next;
					}

					break;
				}
			}
			else
				next_job = next_job->next;

		}
		/* You need to loop through jobs list since a command line can contain ;*/
		/* Check for built-in commands */
		/* If not built-in */
			/* If job j runs in foreground */
			/* spawn_job(j,true) */
			/* else */
			/* spawn_job(j,false) */
	}
}
Example #5
0
int main() 
{
	init_dsh();
	DEBUG("Successfully initialized\n");

	while(1) {
    job_t *j = NULL;
    if(!(j = readcmdline(promptmsg(getpid())))) {
			if (feof(stdin)) { /* End of file (ctrl-d) */
        fflush(stdout);
        printf("\n");
        exit(EXIT_SUCCESS);
      }
			continue; /* NOOP; user entered return or spaces with return */
    }

        /* Only for debugging purposes to show parser output; turn off in the
         * final code */
    // if(PRINT_INFO) print_job(j);

        /* Your code goes here */
        /* You need to loop through jobs list since a command line can contain ;*/
        /* Check for built-in commands */
        /* If not built-in */
            /* If job j runs in foreground */
            /* spawn_job(j,true) */
            /* else */
            /* spawn_job(j,false) */

    while (j != NULL)
    {
      // if EOF builtincmd of quit
      //active jobs is last job?

      // if not a command at all ?
      bool is_built_in_cmd = builtin_cmd(j, (j->first_process)->argc, (j->first_process)->argv);
      if(!is_built_in_cmd) 
      {

        if(j->bg == true)
        {
          spawn_job(j, false);
        }
        else
        {
          spawn_job(j, true);
        }

        if(active_jobs_head == NULL) 
        {
        active_jobs_head = j;
        
        }

        else 
        {
        job_t *lastJob = find_last_job(active_jobs_head);
        lastJob->next = j;
        j->next = NULL;

        }
      }

      j = j->next;
    }
  }
}