Thread: Help managing child processes (I think)

  1. #1
    Registered User
    Join Date
    Jul 2014
    Posts
    15

    Help managing child processes (I think)

    Like a few others on here, I'm writing a basic Unix shell. Most of my commands work, including redirection, such as "ls > testfile". The problem is, after one of these redirection commands, my prompt icon disappears. If I enter some text it says "write error: Bad file descriptor", and after that is unresponsive.

    After spending some time debugging, something is definitely going wrong when I attempt to close the open file descriptors. Still trying to narrow it down further but I can't get any response from close().

    Code:
    // File redirection example
    if (strcmp(args[1], ">") == 0) {
        fd = open(args[2], O_WRONLY|O_CREAT|O_TRUNC, 0644); 
    
        if (fd == -1) {
            exitStatus = 1;
        }
    
        fd2 = dup2(fd, 1);
    
        if (fd == -1) {
            exitStatus = 1;
        }
    
        args[1] = NULL;
        exitStatus = launch(args); 
        close(fd);
        close(fd2);
    
        // It never makes it to this point
    }
    
    // Job launch example
    int launch(char **args) {
        pid_t pid, wpid;
        int status, exitStatus = 0;
    
    
        pid = fork();
        if (pid == 0) { 
            //printf("This is the child\n");
            if (execvp(args[0], args) == -1) { 
                printf("Command not recognized\n");
                exit(1); 
            }
        } else if (pid < 0) { 
            perror("smallsh");
        } else { 
            do {
                wpid = waitpid(pid, &status, WUNTRACED); 
            } while (!WIFEXITED(status) && !WIFSIGNALED(status)); 
        }
    
    
        if (status != 0 || WIFSIGNALED(status)) exitStatus = 1;
    
    
        return exitStatus;
    }
    Last edited by hinesro; 02-23-2015 at 04:36 AM.

  2. #2
    Registered User
    Join Date
    Jul 2014
    Posts
    15
    I'm still stumped, it seems as though close(fd) is breaking the program. Can anyone see what I'm doing wrong?

  3. #3
    Registered User
    Join Date
    Jul 2014
    Posts
    15
    As a workaround, I had to manually restore stdout. It works for now but I'm still confused about why that didn't happen when close() was called.

    Code:
    if (numArgs == 3) {
        int saved_stdout; // Save standard output to be restored after redirection
        saved_stdout = dup(1);
    
    
        if (strcmp(args[1], ">") == 0) {
            fd = open(args[2], O_WRONLY|O_CREAT|O_TRUNC, 0644); 
    
    
            dup2(fd, 1); // Point stdout to this file
    
    
            close(fd); // Close unused file descriptor
    
    
            exitStatus = launch(args); // Launch the command
        }
    
        /* Restore stdout */
        dup2(saved_stdout, 1);
        close(saved_stdout);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 12-01-2013, 02:00 PM
  2. Child processes
    By jsx-27 in forum C Programming
    Replies: 3
    Last Post: 08-24-2012, 02:27 AM
  3. forks and child processes
    By kpax in forum C Programming
    Replies: 1
    Last Post: 05-28-2008, 04:47 AM
  4. managing father and son processes
    By Andreak in forum C Programming
    Replies: 4
    Last Post: 06-03-2005, 09:50 AM
  5. Child processes in Linux
    By Music_Man in forum Linux Programming
    Replies: 0
    Last Post: 03-20-2003, 09:04 AM