Thread: execl not executing?

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    69

    execl not executing?

    i'm having a little trouble getting my program going.

    it takes a line of input from cin into a string, parses it on any spaces found, and puts the result into an array of char*'s. however, when it gets to the fork and execl section, the anomalies begin. it seems to loop somewhere, first generating a childID of some number, and the second time generating a 0 for childID. This triggers both the if and else statements in the forking function.

    i would think it has to do with the strange way i had the assignment and casting of the string into the char* type. i have no reason to believe that it is perfect, and it was just born as a way to get rid of the compiling errors.


    here's the code:

    Code:
    #include <sys/types.h>  // For pid_t
    #include <sys/wait.h>	// For wait
    #include <cstdlib>	
    #include <cstdio>		// For perror and errno
    #include <iostream>	
    #include <unistd.h>		// For exec and fork calls
    #include <cerrno>		// For errno and perror
    #include <string>
    #include <sstream>
    #include <vector>
    using namespace std;
    
    string input(vector<string>& history);
    void parsing(string command, char* args[]);
    void forking(char* args[]);
    
    int main ( int argCount, char *argValues[])
    {
      int argument;
      char *args[255];
      int count=0;
      vector<string> history;
    
      string command;
      string temp;
      string temp2;
      
    // call input here:
      command=input(history);
    
    // call parsing here:
      parsing(command, args);
    
      cout<<"Here are the arguments as detected by the function:" <<endl;
      while (args[count]!='\0')
      {
    	  cout<<"args[" <<count <<"]=" <<args[count] <<endl;
    	  count++;
      }
    // call forking here
      forking(args);
    
      return EXIT_SUCCESS;
    }
    
    string input(vector<string>& history)
    {
    	string command;
    
    	cout<<"Enter a command" <<endl;
    	getline(cin, command);
    	history.push_back(command);
    	return command;
    }
    
    void parsing(string command, char* args[])
    {
      string temp2;
      string temp;
      int count=0;
      if (command.find(" ", 0)!=std::string::npos)
      {
    	  temp2=command.substr(0, command.find(" ", 0));
    	  args[0]=const_cast <char*>(temp2.c_str());
    	  command.erase(0, command.find(" ", 0));
    	  count++;
      }
      istringstream parse(command);
      while (parse>>temp)
      {
    	  args[count]=const_cast <char*>(temp.c_str());
    	  count++;
      }
    }
    
    void forking(char* args[])
    {
      pid_t childID;
      int status;
    
      childID = fork();
      cout<<"childID=" <<childID <<endl;
      cout<<"getting to the if statement" <<endl;
      if (childID == 0)
      {		/* It's the child process */
    	cout<<"inside the if statement" <<endl;
        status = execl (args[0], *args);
    	cout<<"after execl" <<endl;
        if (status == -1) 
          {
    		cout << "Error Doing execv! errno = " << errno << endl;
    		perror("exec");
          }
        
      }
      else
      {
        childID = wait(&status);
        cout << "Output status of child process " << childID
             << " is " << (status >> 8 & 0xff) << endl;
      }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > status = execl (args[0], *args);
    You've just written
    status = execl (args[0], args[0]);

    Try
    status = execl (args[0], args);

    > args[count]=const_cast <char*>(temp.c_str());
    I kinda doubt this works - it seems to me you're just taking a copy of a pointer to a local variable
    A variable which will disappear when the function exits
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    69
    when i change the execl(args[0], *args) to execl(args[0], args) it gives me this error:

    Code:
    $ g++ exec2.cpp -o exec2
    exec2.cpp: In function `void forking(char**)':
    exec2.cpp:87: error: cannot convert `char**' to `const char*' for argument `2'
       to `int execl(const char*, const char*, ...)'
    also, any suggestions for the assigning of string type to char* type?

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    > also, any suggestions for the assigning of string type to char* type?
    Assuming the memory allocated in args[0] is sufficient to hold the entire string, then:
    Code:
    strcpy(args[count], temp.c_str());
    should work. I don't see in your code, though where you allocate the memory for that. I'm not sure why you need to use a char array in this instance, though. Just use the std::string class, and then when you want to pass it to execl() or any other function taking const char* arguments use c_str().

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >status = execl (args[0], args);

    Maybe:
    status = execv( args[0], args );

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    69
    Quote Originally Posted by swoopy
    >status = execl (args[0], args);

    Maybe:
    status = execv( args[0], args );
    thank the almighty lord it worked!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. execl and command line arguments
    By imtiaz3 in forum C Programming
    Replies: 13
    Last Post: 09-30-2008, 12:18 PM
  2. Giving instruction to an executing C code
    By darkwalk in forum C Programming
    Replies: 2
    Last Post: 10-24-2006, 12:01 PM
  3. Replies: 40
    Last Post: 09-01-2006, 12:09 AM
  4. Can we use select() to capture output from execl()?
    By Nessarose in forum Networking/Device Communication
    Replies: 5
    Last Post: 07-05-2005, 12:53 AM
  5. execl failing
    By carrythe0 in forum C Programming
    Replies: 1
    Last Post: 10-01-2001, 12:25 PM