Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8245765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:17:34+00:00 2026-06-07T22:17:34+00:00

Round 2 After reading some of the answers, my revised code is: int pid

  • 0

Round 2

After reading some of the answers, my revised code is:

int pid = fork();

if (pid == -1) {
    perror("fork");
} else if (pid == 0) {   

    if (in) { //if '<' char was found in string inputted by user
        int fd0 = open(input, O_RDONLY, 0);
        dup2(fd0, STDIN_FILENO);
        close(fd0);
        in = 0;
    }

    if (out) { //if '>' was found in string inputted by user
        int fd1 = creat(output, 0644);
        dup2(fd1, STDOUT_FILENO);
        close(fd1);
        out = 0;
    }   

    execvp(res[0], res);
    perror("execvp");
    _exit(1);
} else {
    waitpid(pid, 0, 0);
    free(res);
}

It works, but seems the standard output isn’t being reconnected or something to that effect. Here is execution:

SHELL$ cat > file
hello, world
this is a test
SHELL$ cat < file //no output
SHELL$ ls //no output

‘<‘ and ‘>’ both work, but after they are executed there is no output.


Round 1

I have been working on a relatively simple shell in C for a while now, but I am having trouble implementing input (<) and output (>) redirection. Help me find the issues in the following code:

int fd;
int pid = fork();
int current_out;

if (in) { //if '<' char was found in string inputted by user
    fd = open(input, O_RDONLY, 0);
    dup2(fd, STDIN_FILENO);
    in = 0;
    current_out = dup(0);
}

if (out) { //if '>' was found in string inputted by user
    fd = creat(output, 0644);
    dup2(fd, STDOUT_FILENO);
    out = 0;
    current_out = dup(1);
}

if (pid == -1) {
    perror("fork");
} else if (pid == 0) {       
    execvp(res[0], res);
    perror("execvp");
    _exit(1);
} else {
    waitpid(pid, 0, 0);
    dup2(current_out, 1);
    free(res);
}

I may have some unnecessary material in there because I have been trying different things to get it to work. I am not sure what is going wrong.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T22:17:36+00:00Added an answer on June 7, 2026 at 10:17 pm

    You have way too many file descriptors open after your redirection. Let’s dissect the two paragraphs:

    if (in) { //if '<' char was found in string inputted by user
        fd = open(input, O_RDONLY, 0);
        dup2(fd, STDIN_FILENO);
        in = 0;
        current_in = dup(0);  // Fix for symmetry with second paragraph
    }
    
    if (out) { //if '>' was found in string inputted by user
        fd = creat(output, 0644);
        dup2(fd, STDOUT_FILENO);
        out = 0;
        current_out = dup(1);
    }
    

    I’m going to be charitable and ignore the fact that you are ignoring errors. However, you will need to error check your system calls.

    In the first paragraph, you open a file and capture the file descriptor (it might well be 3) in the variable fd. You then duplicate the file descriptor over standard input (STDIN_FILENO). Note, though, that file descriptor 3 is still open. Then you do a dup(0) (which, for consistency, should be STDIN_FILENO), getting another file descriptor, perhaps 4. So you have file descriptors 0, 3 and 4 pointing at the same file (and, indeed, the same open file description — noting that an open file description is different from an open file descriptor). If your intention with current_in was to preserve the (parent) shell’s standard input, you have to do that dup() before you do the dup2() that overwrites the output. However, you would be better off not altering the parent shell’s file descriptors; it is less overhead than re-duplicating the file descriptors.

    Then you more or less repeat the process in the second paragraph, first overwriting the only record of file descriptor 3 being open with the fd = creat(...) call but obtaining a new descriptor, perhaps 5, then duplicating that over standard output. You then do a dup(1), yielding another file descriptor, perhaps 6.

    So, you have stdin and stdout of the main shell redirected to the files (and no way of reinstating those to the original values). Your first problem, therefore, is that you are doing the redirection before you fork(); you should be doing it after the fork() — though when you get to piping between processes, you will need to create pipes before forking.

    Your second problem is that you need to close a plethora of file descriptors, one of which you no longer have a reference for.

    So, you might need:

    if ((pid = fork()) < 0)
        ...error...
    else if (pid == 0)
    {
        /* Be childish */
        if (in)
        {
            int fd0 = open(input, O_RDONLY);
            dup2(fd0, STDIN_FILENO);
            close(fd0);
        }
    
        if (out)
        {
            int fd1 = creat(output , 0644) ;
            dup2(fd1, STDOUT_FILENO);
            close(fd1);
        }
        ...now the child has stdin coming from the input file, 
        ...stdout going to the output file, and no extra files open.
        ...it is safe to execute the command to be executed.
        execve(cmd[0], cmd, env);   // Or your preferred alternative
        fprintf(stderr, "Failed to exec %s\n", cmd[0]);
        exit(1);
    }
    else
    {
        /* Be parental */
        ...wait for child to die, etc...
    }
    

    Before you do any of this, you should ensure that you’ve already flushed the shell’s standard I/O channels, probably by using fflush(0), so that if the forked child writes to standard error because of a problem, there is no extraneous duplicated output.

    Also note that the various open() calls should be error-checked.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Round a double to 2 significant figures after decimal point The code
After reading some threads on misuses of exceptions (basically saying you don't want to
I am after a all round installation and introduction to Glassfish. (ie Your boss
In our office, we regularly enjoy some rounds of foosball / table football after
To round a number I use the following code: public static roundBd(BigDecimal bd){ BigDecimal
I'm trying to write a very simple number guessing game (code is below). After
I am developing a simple game. After every round the results will be displayed.
Possible Duplicate: Round a double to 2 significant figures after decimal point I know
I want to round down column values after division aplied: DataColumn maxSpend = new
After searching and reading a little bit I came up with the following SQL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.