Problem with fork() and execlp process


 
Thread Tools Search this Thread
Top Forums Programming Problem with fork() and execlp process
# 1  
Old 02-09-2013
Error Problem with fork() and execlp process

Hello everyone, 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 program

Code:
#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

Code:
#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.


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


Thanks,

John

Last edited by jim mcnamara; 02-10-2013 at 12:51 PM..
# 2  
Old 02-10-2013
I don't really know much about C++, but three things stood out as I skimmed your code:
1) execlp's list of args isn't null pointer terminated.
2) The int in status is not the exit status of the child; it's a bitmask which includes other information. To retrieve the exit status requires masking out all but the 8 bits which encode it. For that, macros are provided. See your wait/waitpid man page for specifics.
3) exit(-1) won't do want you expect. Only the least significant 8 bits of -1 will be used. The resulting exit status will be 255.

Also, your code would be easier to digest if it were enclosed within code tags instead of icode.

Regards and welcome,
Alister

Last edited by alister; 02-10-2013 at 11:24 AM..
# 3  
Old 02-10-2013
with waitpid, use the WIFEXITED macro and WIFEXITSTATUS
Code:
if (waitpid(pid, &status, 0) > 0)  // you have a valid return
{
    if(WIFEXITED(status) )
    {
       int retcode=WIFEXITSTATUS(status);
       printf("ret code is %d\n", retcode);
     }
}

valid retcode is always 0 - 255. If WIFEXITED fails you have to use WIFSIGNALED the way you used WIFEXITED. And then some other implementation defined macros like WIFTERMSIG WIFCOREDUMP. There are other choices, too.
# 4  
Old 02-10-2013
The arguments for execlp() are char *:

Code:
int execlp(const char *file, const char *arg0, ...
          /* const char *argn, (char *)0 */);

Looks like you're passing binary int's.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sleep 600 or fork/spawn a process or daemon?

Hi, I have a script that run every 10 minutes, from a specific timeframe of the day, for example 0500 - 1900. The script is some sort of checker script for an application log file and check for errors and email us if there is error/s reported in the log. At the moment, I schedule it... (1 Reply)
Discussion started by: newbie_01
1 Replies

2. Programming

Generating Random Number in Child Process using Fork

Hello All, I am stuck up in a program where the rand functions ends up giving all the same integers. Tried sleep, but the numbers turned out to be same... Can anyone help me out how to fix this issue ? I have called the srand once in the program, but I feel like when I call fork the child process... (5 Replies)
Discussion started by: manisum
5 Replies

3. Programming

fork n process

Making a C program (process PP) that satisfies the following requirements: 1. Pp receives 3 ≤ N ≤ 10 arguments on the command line invocation, to interpret as a whole and thus should be 1 ≤ Ni ≤ 4; 2. Pp initially will have to create N processes Pi children facing a range of life to ... (1 Reply)
Discussion started by: loweherz
1 Replies

4. Programming

Timed action after fork() in parent process

Assume you have such a piece of (more or less pseudo-)code: if(fork() == 0) {// childprocess chmod(someProgram, 00777); exec(someProgram); } else { // assume it never fails and this is the parent chmod(someProgram, 00000); // should be executed as soon as possible after the... (5 Replies)
Discussion started by: disaster
5 Replies

5. Red Hat

Fork wait in background process - large delay

hi all, We are trying to run a process in the background and in the process we call fork ;and wait for the child process to finish .We find that the died = wait(&status); happens after 10 seconds randomly and sometimes completes in time (within 1 sec) This behavior is seen only when the... (1 Reply)
Discussion started by: vishnu.priya
1 Replies

6. UNIX for Dummies Questions & Answers

cannot fork process on IBM - AIX

Hi, Currently, I'm getting the foll error on an IBM AIX /etc/profile: 0403-030 The fork function failed. Too many processes already exist. How can i check the current no. of processes which can run simultaneously on an IBM AIX machine for oracle user and how can i change ? Thanks Vin (1 Reply)
Discussion started by: win_vin
1 Replies

7. Programming

Multiple process using fork()

I have a code which has four different process, each printing different message. I have provided it with user input (implemented using thread), depending on which the corresponding message from that process has to be printed.The code is shown below.when I run the pgm, it takes input such as... (1 Reply)
Discussion started by: shashi
1 Replies

8. HP-UX

error : can not fork new process

hi today we came across error "can not fork new process" when i checked there were 400 ksh processes were running for that particular user ( due to kernel parameter setting no of processes were restricted to 400 ) and the reason for this was somebody executed shell script which had "*" ( only *... (3 Replies)
Discussion started by: zedex
3 Replies

9. UNIX for Dummies Questions & Answers

Cannot fork , too many process - SCO Unix 5.05

I need a help... I have a HP Netserver LH 6000 U with Hardware Raid . Sco 5.0.5 installed with Oracle database. Total number of users, normally logged in are around 60 nos. The system is very slow ( takes 6 hours for processing one Lakh bills) during the Billing process. Also it is... (1 Reply)
Discussion started by: saleeshpl
1 Replies

10. Programming

Reference Variables To A Child Process Created With Fork

Hi! IN THE FOLLOWING PROGRAM THE VALUE OF j REMAINS UNCHANGED . WHY ? IF I WANT A VARIABLE VALUE TO CHANGE LIKE THIS , IS THERE ANY WAY TO DO IT ? Or do we have to use shared memory variables? main() { int return_pid, i, total; int j=1; total = TOTALRECS+1; for... (2 Replies)
Discussion started by: AJAY BHATIA
2 Replies
Login or Register to Ask a Question