How can I make my program return to the original loop after calling exec?

Montao

I have a program that is similar to a basic bash shell. I can run programs in the /bin directory from the shell. I have a builtin command checkEnv that uses a pager to display system variables exactly like printenv | sort | less but when less has completed, the program exits. I want to program to return to the shell. Can you tell my how I can do it? The way I create the pipeline is

cmd[0].argv= printenv;
cmd[1].argv= sort;
cmd[2].argv= pager_cmd;
fork_pipes(3, cmd);

where cmd is declared:

struct command
{
    char * const *argv;
};
struct command cmd[3];

I have helper functions to fork:

/* Helper function that forks pipes */
static void fork_pipes(int n, struct command *cmd)
{
    int i;
    int in = 0;
    int fd[2];
    int take_return;
    for (i = 0; i < n - 1; ++i)
    {
        take_return = pipe(fd);
        ++take_return; /* Please the -O4 switch to gcc */
        spawn_proc(in, fd[1], cmd + i);
        close(fd[1]);
        in = fd[0];
    }
    if (dup2(in, 0) < 0)    {
        err_syserr("dup2() failed on stdin for %s: ", cmd[i].argv[0]);
    }
    fprintf(stderr, "%d: executing %s\n", (int)getpid(), cmd[i].argv[0]);
    execvp(cmd[i].argv[0], cmd[i].argv);
    err_syserr("failed to execute %s: ", cmd[i].argv[0]);
}

/* Helper function that spawns processes */
static int spawn_proc(int in, int out, struct command *cmd)
{
    pid_t pid;
    if ((pid = fork()) == 0)
    {
        if (in != 0)
        {
            if (dup2(in, 0) < 0)
                err_syserr("dup2() failed on stdin for %s: ", cmd->argv[0]);
            close(in);
        }
        if (out != 1)
        {
            if (dup2(out, 1) < 0)
                err_syserr("dup2() failed on stdout for %s: ", cmd->argv[0]);
            close(out);
        }
        fprintf(stderr, "%d: executing %s\n", (int)getpid(), cmd->argv[0]);
        execvp(cmd->argv[0], cmd->argv);
        err_syserr("failed to execute %s: ", cmd->argv[0]);
    }
    else if (pid < 0)   {
        err_syserr("fork failed: ");
    }
    return pid;
}

My problem is that the program exits when using the builtin command checkEnv. I want the program to return to the main loop that is the command prompt for my shell:

 while(1) {

        printf("miniShell>> ");
        memset(line, 0, sizeof line); /*Reset*/
        if(!fgets(line, BUFFER_LEN, stdin)) {
            break;
        }

Can you help me?

Oleg Andriyanov

My guess is that you forgot to call fork for the third process. You use just exec for it which means your main process (the miniShell) is substituted by pager_cmd. You should write something like that:

static void fork_pipes(int n, struct command *cmd)
{
    ... //calling spawn_proc two times
    pid = fork();
    if(pid == 0) { //child process (pager_cmd)
        execvp(cmd[i].argv[0], cmd[i].argv);
        err_syserr("failed to execute %s: ", cmd[i].argv[0]);
    } else if(pid > 0) {
        //call wait() for three pids we obtained by forking
    } else {
        //error handling
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I make my java program not create multiple instances when calling the constructor with parameters?

From Dev

How can I make my program faster?

From Dev

How can I return to the beginning of my program if something goes wrong?

From Dev

How can I make my recursive C program more efficient?

From Dev

How can I make a QTextEdit widget scroll from my program

From Dev

How can I make my Python program's executable file?

From Dev

How can I make the LLVM IR of a function available to my program?

From Dev

How can I make the LLVM IR of a function available to my program?

From Dev

How can i make my program reactive in swift with RxSwift

From Dev

How can I make my program run on startup by adding it to the registry?

From Dev

How do I keep the this context of my original function after calling .apply()?

From Dev

How can i make this method return to that variable in that loop

From Dev

How can I return to a line inside my While loop

From Dev

how can i return value after loop in C

From Dev

How can I make the reactionTime work in my while loop

From Dev

How can I make my while loop less stressful on the browser?

From Dev

How can i make a jQuery animation loop on infinite, after it finish?

From Dev

How do I get my logic in this Java program to make my loop work?

From Dev

AVAudioPlayer continues to play after segue and relaunches on return to original view controller - how can I stop it?

From Dev

How can I make TextToSpeech to speak a text with max volume and restore original volume after speak end?

From Java

How can I make the object move back to his original position after he reached the target?

From Dev

How can I return to a production build of window 10 without losing my program

From Dev

How can I exit the loop in this program

From Dev

How can i use a while loop on this program?

From Dev

How can i make this program work?

From Dev

How can i make this program faster and simple?

From Dev

How frequently should I be calling sql.Open in my program?

From Dev

How can I program a while loop that waits for my input in Android? (SOLVED)

From Dev

How can I update data in a for loop when my data is returned with a defer after the loop completes

Related Related

  1. 1

    How can I make my java program not create multiple instances when calling the constructor with parameters?

  2. 2

    How can I make my program faster?

  3. 3

    How can I return to the beginning of my program if something goes wrong?

  4. 4

    How can I make my recursive C program more efficient?

  5. 5

    How can I make a QTextEdit widget scroll from my program

  6. 6

    How can I make my Python program's executable file?

  7. 7

    How can I make the LLVM IR of a function available to my program?

  8. 8

    How can I make the LLVM IR of a function available to my program?

  9. 9

    How can i make my program reactive in swift with RxSwift

  10. 10

    How can I make my program run on startup by adding it to the registry?

  11. 11

    How do I keep the this context of my original function after calling .apply()?

  12. 12

    How can i make this method return to that variable in that loop

  13. 13

    How can I return to a line inside my While loop

  14. 14

    how can i return value after loop in C

  15. 15

    How can I make the reactionTime work in my while loop

  16. 16

    How can I make my while loop less stressful on the browser?

  17. 17

    How can i make a jQuery animation loop on infinite, after it finish?

  18. 18

    How do I get my logic in this Java program to make my loop work?

  19. 19

    AVAudioPlayer continues to play after segue and relaunches on return to original view controller - how can I stop it?

  20. 20

    How can I make TextToSpeech to speak a text with max volume and restore original volume after speak end?

  21. 21

    How can I make the object move back to his original position after he reached the target?

  22. 22

    How can I return to a production build of window 10 without losing my program

  23. 23

    How can I exit the loop in this program

  24. 24

    How can i use a while loop on this program?

  25. 25

    How can i make this program work?

  26. 26

    How can i make this program faster and simple?

  27. 27

    How frequently should I be calling sql.Open in my program?

  28. 28

    How can I program a while loop that waits for my input in Android? (SOLVED)

  29. 29

    How can I update data in a for loop when my data is returned with a defer after the loop completes

HotTag

Archive