Problems with execvp and fork....

This is a discussion on Problems with execvp and fork.... within the C Programming forums, part of the General Programming Boards category; Code: Bool execute_command(char *input) { pid_t child_pid; /* fork() == 0 for child process */ if(child_pid == 0) { char ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    41

    Problems with execvp and fork....

    Code:
    Bool execute_command(char *input)
    {
       pid_t child_pid;
    
       /* fork() == 0 for child process */
    
       if(child_pid == 0)
       {
          char *argv[]={input, 0};
          execvp(argv[0], argv);
          perror("execvp() failure!\n");
    
          printf("This print is after execl() and should not get executed\n");
    
          _exit(EXIT_FAILURE);
       }
    
       else if(child_pid > 0)
       {
          wait(0);
          return(TRUE);
       }
    
       else
       {
          perror("Fork Error. Quitting the Programing.\n");
          exit(EXIT_FAILURE);
       }
    }
    When the input is something like "ls" the program just hangs. I thought that is was the wait statement that made this so, so I took it out, im still getting the same results.

  2. #2
    Registered User
    Join Date
    Sep 2005
    Posts
    41
    here's some more code that exemplifies what i'm trying to do. Still having the same problem.

    Code:
    #define _GNU_SOURCE 1
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <getopt.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    
    typedef enum
    {
       FALSE = 0,
       TRUE = 1
    } Bool;
    
    int main()
    {
       pid_t child_pid;
       Bool stillGoing = TRUE;
       ssize_t readReturn = 0;
       size_t bufLen = 0;
       char *input = NULL;
       int x = 0;
    
       while(stillGoing)
       {
          printf("Enter some stuff ese: ");
          /* The buffer will contain the new line character and the null char */
          readReturn = getline(&input, &bufLen, stdin);
          
          /* Get rid of any new line that may be there. */
          for(x = 0; x < strlen(input) + 1; ++x)
          {
             if(input[x] == '\n')
             {
                input[x] = '\0';
             }
          } 
          
          if(readReturn == 5 && strncmp(input, "exit", 4) == 0)
          {
             stillGoing = FALSE;
             break;
          }
    
          if(child_pid == 0)
          {  
             char *argv[]={input, 0};
             execvp(argv[0], argv);
             perror("execvp() failure!\n");
    
             printf("This print is after execl() and should not get executed\n");
          
             _exit(EXIT_FAILURE);
          }
       
          else if(child_pid > 0)
          {
             wait(0);
          }
    
          else
          {
             perror("Fork Error. Quitting the Programing.\n");
             exit(EXIT_FAILURE);
          }
          input = NULL;
       }     
    }

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Whereabouts do you think the problem is? Put some printf()s in there to help you debug it and find out exactly where the hang is occuring.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    41
    the problem was that i never forked a new process. I dont know how i left that out lol.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trying to understand fork, dup2, and execvp
    By Unclejunebug in forum C Programming
    Replies: 0
    Last Post: 04-29-2009, 07:04 PM
  2. fork, execv, spawn, or something else?
    By DavidP in forum C++ Programming
    Replies: 8
    Last Post: 01-26-2009, 03:25 PM
  3. Storing the result of execvp to a char array in C
    By kponenation in forum C Programming
    Replies: 1
    Last Post: 12-14-2005, 10:43 PM
  4. execvp and ampersand?
    By slevytam in forum C Programming
    Replies: 16
    Last Post: 02-02-2005, 07:36 PM
  5. Procesees, fork(), wait() and exec()
    By Stewdent in forum Linux Programming
    Replies: 1
    Last Post: 02-20-2003, 10:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21