Esempio n. 1
0
File: fg.c Progetto: catuss-a/42sh
int		builtins_fg(int ac, char **av, t_config *conf, int *ret)
{
  change_signal(0);
  *ret = 0;
  if (ac == 1 && put_in_foreground(conf->head_job, conf) == -1)
    {
      *ret = -1;
      my_puterr("fg: No current job.\n");
    }
  else if (ac == 2)
    {
      if (check_job_and_put_in_foreground(conf->head_job, av[1], conf) == -1)
	{
	  *ret = -1;
	  my_puterr("fg: No such job.\n");
	}
    }
  else if (ac != 1)
    {
      *ret = -1;
      my_puterr("fg: No such job.\n");
    }
  change_signal(1);
  return (MYSH_SIGNAL_NOPE);
}
// move a job into the foreground
int cmd_fg(tok_t arg[]){
  process* p;
  if( arg[0] != NULL )
    p = get_process( atoi( arg[0] ) );
  else if( first_process != NULL )
    p = first_process->prev;
  if( p != NULL ){
    put_in_foreground( p, true );
  }
  return 1;
}
Esempio n. 3
0
File: shell.c Progetto: dnery/plush
void sh_continue(job_t *job, int foreground)
{
        /* Mark suspended job as running again */
        process_t *p = job->first_process;
        for (; p != NULL; p = p->next)
                p->suspended = 0;
        job->notified = 0;

        /* Put in fg or bg, sending non-zero cont */
        if (foreground)
                put_in_foreground(job, 1);
        else
                put_in_background(job, 1);
}
int
shell( int argc, char *argv[] ) {

  init_shell(); // initialize the shell
  
  pid_t pid = getpid();	       // get current processe's PID
  pid_t ppid = getppid();      // get parent's PID 
  pid_t cpid, tcpid, cpgid;

  print_welcome( argv[0], pid, ppid );
  print_prompt();
  char *s = malloc(INPUT_STRING_SIZE + 1); // user input string
  
  while( (s = freadln(stdin)) ){ // read in the input string
    process *p = create_process( s );
    if( p != NULL ){ // do we need to run a newly created process?
      cpid = fork(); // fork off a child
      if( cpid == 0 ) { // child
	p->pid = getpid();
	launch_process( p );
      } // if( cpid == 0 )
      else if( cpid > 0 ){ // parent
	p->pid = cpid;
	if( shell_is_interactive ){ // are we running shell from a terminal?
	  // put 'p' into its own process group
	  // since our shell is not supporting pipes, we treat
	  // each process by itself as a 'job' to be done
	  if( setpgid( cpid, cpid ) < 0 ) 
	    printf( "Failed to put the child into its own process group.\n" );
	  if( p->background )
	    put_in_background( p, false );
	  else
	    put_in_foreground( p, false );
	} // if( shell_is_interactive )
	else
	  wait_for_process( p );
      } // else if( cpid > 0 )
      else // fork() failed
	printf( "fork() failed\n" );
    } // if( p != NULL )
    print_prompt();
  } // while

  return EXIT_SUCCESS;
} // shell
Esempio n. 5
0
File: shell.c Progetto: dnery/plush
void sh_launch_job(job_t *job, int foreground)
{
        process_t *p;                                   /* Process iteration */
        pid_t pid;                                      /* PID for the newly spawned process */
        int output;                                     /* Output file descriptor */
        int input = job->stdin;                         /* Input file descriptor */
        int iopipe[2] = {STDIN_FILENO, STDOUT_FILENO};  /* Inter-process comms pipe */

        for (p = job->first_process; p != NULL; p = p->next) {

                /* Set up pipes if applicable, set output */
                if (p->next != NULL) {
                        check(pipe(iopipe) >= 0);
                        output = iopipe[1];
                } else {
                        output = job->stdout;
                }

                /* Fork child process and launch */
                check((pid = fork()) >= 0);

                if (pid == 0) {
                        /* I am the fork */
                        sh_launch_process(p, job->pgid, input, output,
                                        job->stderr, foreground);
                } else {
                        /* I am parent */
                        p->pid = pid;

                        if (shell_interactive) {

                                /*
                                 * if (job->pgid == 0)
                                 *      job->pgid = pid;
                                 *
                                 * MAN specifies that if pgid is 0, pid of
                                 * indicated process (pid) shall be used.
                                 */
                                setpgid(pid, job->pgid);
                        }
                }

                /* Clean up the pipes, set input */
                if (input != job->stdin)
                        close(input);
                if (output != job->stdout)
                        close(output);

                input = iopipe[0];
        }

        format_job_info(job, "launched");

        /* Either wait until job is finished, or engage new */
        if (!shell_interactive)
                job_wait_blocked(job);
        else if (foreground)
              put_in_foreground(job, 0);
        else
              put_in_background(job, 0);
}