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 8018893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:14:13+00:00 2026-06-04T21:14:13+00:00

I’m real lost here. Building a Linux shell, working on processing external commands. I’m

  • 0

I’m real lost here. Building a Linux shell, working on processing external commands. I’m trying to create a new process then execute the commands. Very new to exec(), fork(), pipe() and dup(), but I’m sure I need them somewhere.

Basically my question is: what’s the best way to pass arguments into exec? I know there’s many options, but if I have a string vector that contains “ls -l” how do I pass that into execute? Should I re-parse it into “ls” and “-l”?

I already have fork() creating a child process, but I don’t know how to run exec().

On a somewhat related note, what should I put in the waitpid part of fork()

                pid_t pid;
                int status;
                pid = fork();
                if (pid < 0)
                {
                    cout << "Fork failed." << endl;
                }
                else if (pid == 0)
                {

                    execv("/bin/sh", (VECTOR OF COMMANDS?));
                    _exit (EXIT_FAILURE);
                }
                else
                {
                    if (waitpid (pid, &status, 0) == pid)
                    {
                        cout << "huh?" << endl;
                    }
                    else
                    {
                        cout << "Error." << endl;
                    }
                }

The next hurdle is piping, but I’ll cross that bridge when I get there.

EDIT:

For what it’s worth, here’s the parsing and calling that I’m having trouble with. The lines with “**” after seem to be the ones giving me problems

    const char *args [1024];

string::iterator it5;
size_t pos5;

for (it5=origCstr.begin(); it5 < origCstr.end(); it5++)
{
    string::iterator it2;
    pos5 = origCstr.find(' ');
    if (pos5 == string::npos)
    {
        tmpChar = origCstr.c_str();
        args[argCount] = tmpChar;
        argCount++;
        break;
    }
    it2 = it5 + pos5;
        tmpCstr = origCstr.substr(0, pos5);
    tmpChar = tmpCstr.c_str();
    args[argCount] = tmpChar;
    origCstr.erase(it5, it2+1);
    argCount++;
}
    tmpChar = origCstr.c_str();
args[argCount] = tmpChar;
argCount++;

    pid_t pid;
int status;
pid = fork();
if (pid < 0)
{
    cout << "Fork failed." << endl;
}
else if (pid == 0)
{

        execv("/bin/", args); ****
        _exit (EXIT_FAILURE);
}
else
{
    if (waitpid (pid, &status, 0) == pid)
    {
        cout << "huh?" << endl;
    }
    else
    {
        cout << "Error." << endl;
    }
}
  • 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-04T21:14:14+00:00Added an answer on June 4, 2026 at 9:14 pm

    You will want to call ‘execv’ so that you can make a char*[] containing the options. “ls” and “-l” each get their own slot in the array.

    You’ll have to cast away const, or use a char `char const*[]’ array and then cast away the const on that to pass it to execv. In general, the declarations for these system calls are mildly unfriendly to C++.

    See a stack overflow question on this subject.

    There’s a reasonable tutorial at http://www.yolinux.com/TUTORIALS/ForkExecProcesses.html.

    Roughly speaking:

    char * exec_args[1024];
    int arg_count = 0;
    std::vector<std::string> theArgs;
    
    exec_args[arg_count++] = "/bin/whatever"; // leave command in argv[0]
    for (int x = 0; x < theArgs.size(); x++) {
       exec_args[arg_count++] = strdup(theArgs[x].c_str());
    }
    exec_args[arg_count++] = 0; // tell it when to stop!
    
    execv("/bin/whatever", exec_args);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.