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

  • Home
  • SEARCH
  • 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 8643157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:57:48+00:00 2026-06-12T11:57:48+00:00

So for a class assignment we’re making our own basic shell. Among other functions,

  • 0

So for a class assignment we’re making our own basic shell. Among other functions, it has to be able to process absolute paths and execute external applications using fork() and execv.

The idea is the program forks a child process, calls execv which replaces said child process, and terminates it when the external execution is done.

Unfortunately I missed the class on this, and I’ve tried several examples I found online, none of them have worked. The problem is the parent process duplicating the execv command and terminating itself. Right now I’m just trying to get it to execute “/bin/ls” and return to the main loop of the shell.

Here’s the offending code. I imagine I’m missing something simple. And yes, this would be a good question for the professor, but he doesn’t keep Friday office hours and I don’t want to wait hours or days for an email response. 😛 Likewise I know I should implement better error checking. Right now I’m going for basic functionality, I’ll add the bells and whistles later.

I’ve confirmed that all variables below have their proper values. The question is how do I only cause the child process to close after execv and prevent the parent from doing the same?

TLDR: Teaching myself how to use fork/execv and finding that I’m absolutely clueless.

161 pid_t child = fork();
162 
163 if(child != -1) 
164   if(token[0] == '.' || token[0] == '/') 
165     if(access(token, X_OK) == 0) {
166       char** charArgs = toCharMatrix(args);
167       execv(token, charArgs);
169     } 
  • 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-12T11:57:49+00:00Added an answer on June 12, 2026 at 11:57 am

    parent must wait for child process to finish… And in order to identify child and parent, use process id returned by fork.. see an example below:

    int runcmd(char *cmd)
    {
      char* argv[MAX_ARGS];
      pid_t child_pid;
      int child_status;
    
      parsecmd(cmd,argv);
      child_pid = fork();
      if(child_pid == 0) {
        /* This is done by the child process. */
    
        execv(argv[0], argv);
    
        /* If execv returns, it must have failed. */
    
        printf("Unknown command\n");
        exit(0);
      }
      else {
         /* This is run by the parent.  Wait for the child
            to terminate. */
    
         do {
           pid_t tpid = wait(&child_status);
           if(tpid != child_pid) process_terminated(tpid);
         } while(tpid != child_pid);
    
         return child_status;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For this assignment I had to create my own string class. I initially wrote
I have these models: class Assignment(db.Model): title = db.StringPeoperty(required=True) ...other stuff... class TeamScore(db.Model): assignment
For a class assignment, I'm required to write an object to a file. Our
My class assignment is to write a program that has the user input a
our assignment is to implement a linked list class to do the following with
I need to run some basic SQL code for a class assignment, but I'm
For a class assignment I need to implement a destructor for the objects of
Can I optimize the class assignment here at all? I have to do this
For my second programming assignment in my Java class, we have to create a
I am doing a homework assignment for my summer OO class and we need

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.