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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:02:28+00:00 2026-06-07T19:02:28+00:00

I have an assignment that is to take in a string from the command

  • 0

I have an assignment that is to take in a string from the command line, reverse it and output each character in a separate child process using fork(). I’m just not getting the correct output from the fork() calls. The problem is that indexes get jumbled up on output, like 3, 1, 2, 0 when it should just be 3,2,1,0 … etc… What makes it more confusing is it randomly is successful when the word is 3 characters long (but not always), but routinely is incorrect for words with 4+ characters. The loop works correctly without the fork() call.

Here is my main function, and the problem exists within the for loop.

int main(int argc, char **argv){
    pid_t childpid = 0;
    int i;
    char* invert = new char[strlen(argv[1])+1];
    int invert_length = strlen(argv[1]);
    strcpy(invert, argv[1]);
    for(i=invert_length-1; i>=0; i--){
        childpid = fork();
        if(childpid==(pid_t) 0){
            //I am the child            
            cout<<"Child ["<< i <<"] = "  << invert[i] <<"."<<endl;
            break;
        }
    }   
    return 0;
}
  • 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-07T19:02:30+00:00Added an answer on June 7, 2026 at 7:02 pm

    A simple modification of your program makes it work. Let the child do the next fork. Each process waits for the one it spawned.

    int main(int argc, char **argv){
        pid_t childpid = 0;
        int i;
        char* invert = new char[strlen(argv[1])+1];
        int invert_length = strlen(argv[1]);
        strcpy(invert, argv[1]);
        for(i=invert_length-1; i>=0; i--){
            childpid = fork();
            if(childpid==(pid_t) 0){
                //I am the child
                cout<<"Child ["<< i <<"] = "  << invert[i] <<"."<<endl;
                continue;
            }
            break;
        }
        wait(0);
        return 0;
    }
    

    Edit: Skizz objected that each process executed its work before launching the next one. There was no requirement in the question that all the processes be launched first, but the version below does that.

    int main(int argc, char **argv){
        pid_t childpid = 0;
        int i;
        char* invert = new char[strlen(argv[1])+1];
        int invert_length = strlen(argv[1]);
        strcpy(invert, argv[1]);
        for(i=0; i<invert_length; ++i){
            childpid = fork();
            if(childpid!=(pid_t) 0){
                wait(0);
                break;
            }
        }
        if (i-->0) cout<<"Child ["<< i <<"] = "  << invert[i] <<"." <<endl;
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

for an assignment I have to write a program that will take in an
I have an assignment that wants me to write an ternary search algorithm and
I have an assignment that simulates a dice game. As part of the program,
I have quite a large XSL document for an assignment that does a number
My assignment requires that I have an ER diagram that shows the dependencies between
For a school assignment I have to create a C++ program that will create
For an assignment in college, we have to make a script in Perl that
In my template smart pointer class, I want to have separate constructors and assignment
I have an assignment in my java class that i need to recursively print
The program needs to take in a simple string from the user and display

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.