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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:32:16+00:00 2026-06-18T14:32:16+00:00

this is my first post. I have a task to use a fork to

  • 0

this is my first post.
I have a task to use a fork to create multiple processes and then use execlp to run another program to add 2 numbers.

The problem I am having is we are supposed to use the exit() call in the execlp to return the small integer. This is a bad way to communicate but it’s what we are supposed to do for the sake of this program.

Here is my “coordinator” program

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h> 
#include <sys/wait.h> 
#include <stdlib.h>

using namespace std;

int main (int argc,char* argv[])

{
const int size = argc-1;

int sizeArray = 0;

int numofProc =0;

int arrayofNum[size];

int status;

int value;



for(int y=1; y<argc; y++)
{

arrayofNum[y-1] = atoi(argv[y]);

sizeArray++;

}


if(sizeArray % 2 !=0)

{

arrayofNum[sizeArray]  = 0;

sizeArray++;



}



numofProc = sizeArray/2;


//declaration of a process id variable

pid_t pid;


//fork a child process is assigned 

//to the process id

pid=fork();



//code to show that the fork failed

//if the process id is less than 0

if(pid<0)

{

    cout<<"Fork Failed";// error occurred

    exit(-1); //exit

}


//code that runs if the process id equals 0

//(a successful for was assigned

else 

if(pid==0)

{

    //this statement creates a specified child process

     execlp("./worker", "worker", arrayofNum[0], arrayofNum[1]);//child process



}

//code that exits only once a child 

//process has been completed

else

{

waitpid(pid, &status, 0);

cout<<status;


}

//main

}  

and here is the execlp process

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>


using namespace std;



int main(int argc, char* argv[])

{



int arrayofNum[argc-1];



arrayofNum[0] = atoi(argv[1]);

arrayofNum[1] = atoi(argv[2]);



int sum = arrayofNum[0] + arrayofNum[1];

exit(sum);


}  

My problem is NO MATTER WHAT I DO, the status is ALWAYS printing a 0, I do not know how to retrieve the sum that is returned from the worker process.

My professor told me “”Only the higher byte of status will have the value returned by the worker. you need to extract it. It can be done by many ways. “”

In a nut shell, my question is, How do I retrieve that “sum” that is being sent from my worker process.

Please, I am so confused and have been up for 2 nights wondering about this

Thanks,

John

  • 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-18T14:32:17+00:00Added an answer on June 18, 2026 at 2:32 pm

    First up, you need to pass strings to your program, but you say:

    execlp("./worker", "worker", arrayofNum[0], arrayofNum[1]);
    

    And arrayofNum is an array of integers. Also, with execlp you also need to pass a NULL as the last argument. The standard says:

    The arguments represented by arg0,… are pointers to null-terminated
    character strings
    . These strings shall constitute the argument list
    available to the new process image. The list is terminated by a null
    pointer
    .

    Second, after you call to waitpid(2) you need to do:

    if (WIFEXITED(status))
        code = WEXITSTATUS(status);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first post and I have to admit, I am terrible at
This is my first post here. I have a problem. I need to take
This is my first post and I my first experience with jquery. I have
be kind this is my first post. I have a Crop intent retuning and
Hello everyone (this is my first post) I have some simple AD code that
This is my first post in this forum, so please, be patient with me.
This is my first post on stackoverflow, so please excuse me if my question
This is my first post, thx, you've been very helpful. But I'm stuck. I
This is my first post here so go easy. I am trying to build
This is my first post on Stack Overflow and I'm just wondering on the

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.