By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,913 Members | 1,263 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,913 IT Pros & Developers. It's quick & easy.

Collecting value returned by main()

vpawizard
P: 66
Hello,
Is it possible to store the value returned by main() into a variable? I have an executable which takes two values as arguments and returns the sum. I invoke the executable from another program which subsequently uses the returned values(sum of two numbers). I am programming in Unix environment.

Thanks in advance.
Dec 29 '06 #1
Share this Question
Share on Google+
7 Replies


10K+
P: 13,264
Hello,
Is it possible to store the value returned by main() into a variable? I have an executable which takes two values as arguments and returns the sum. I invoke the executable from another program which subsequently uses the returned values(sum of two numbers). I am programming in Unix environment.

Thanks in advance.
Hi vp,

How are you invoking the other program? (If you could post the codes you are using).
Dec 29 '06 #2

Expert 100+
P: 1,510
Hello,
Is it possible to store the value returned by main() into a variable? I have an executable which takes two values as arguments and returns the sum. I invoke the executable from another program which subsequently uses the returned values(sum of two numbers). I am programming in Unix environment.

Thanks in advance.
if you are using swawnlp() to run the child process
http://www.qnx.com/developers/docs/momentics621_docs/neutrino/lib_ref/s/spawnl.html

with mode P_WAIT (parent waits for child to terminate) spawnlp() returns the exit status of the child process
Dec 29 '06 #3

vpawizard
P: 66
I tried the following but still getting PID of child:

Code for mainprg.c:
Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <process.h>
  5.  
  6. int main()
  7. {
  8.         char num1[]="20";
  9.         char num2[]="30";
  10.         int wait_status;
  11.  
  12.         wait_status=spawnl(1,"./arithmetic.exe",num1,num2,(char*)NULL);
  13.         printf("%d\n",wait_status);
  14.  
  15.         return 0;
  16. }
  17.  
  18.  
Code for arithmetic.c:
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6.  
  7. int main(int argc,char* argv[])
  8. {
  9.         exit(atoi(argv[0]) + atoi(argv[1]));
  10. }
  11.  
  12.  
Dec 29 '06 #4

Expert 100+
P: 1,510
I tried the following but still getting PID of child:

Code for mainprg.c:
Expand|Select|Wrap|Line Numbers
  1.         wait_status=spawnl(1,"./arithmetic.exe",num1,num2,(char*)NULL);
  2.  
  3.  
the first parameter of spawnlp() is 1 which is probably P_NOWAIT - the value returned by P_NOWAIT is the PID of the child process. To get the exit status for a P_NOWAIT process, you must use the waitpid() function, giving it this process ID.

Do you need the child to run concurrently with the parent? - if not use P_WAIT which waits for the child to complete then returns the exit status of the child process.
Dec 29 '06 #5

vpawizard
P: 66
/usr/include/process.h :

#define _P_WAIT 1
#define _P_NOWAIT 2
#define _P_OVERLAY 3
#define _P_NOWAITO 4
#define _P_DETACH 5
Dec 30 '06 #6

vpawizard
P: 66
Here are some sample runs of my program:

$ ./mainprg
Enter first number:123
Enter second number:127
In arithmetic.c,main():250
In mainprg.c,wait_status:64000

$ ./mainprg
Enter first number:452
Enter second number:123
In arithmetic.c,main():575
In mainprg.c,wait_status:16128


$ ./mainprg
Enter first number:123
Enter second number:127
In arithmetic.c,main():250
In mainprg.c,wait_status:64000

$ ./mainprg
Enter first number:10
Enter second number:20
In arithmetic.c,main():30
In mainprg.c,wait_status:7680

$ ./mainprg
Enter first number:452
Enter second number:123
In arithmetic.c,main():575
In mainprg.c,wait_status:16128

Please note the output for the set of input !!!
Dec 30 '06 #7

vpawizard
P: 66
Hello,
Thanks for help. I found out the problem:

If the parent process of the calling process is running a wait call, it is notified of the termination of the calling process and the low-order 8 bits (that is, bits 0377 or 0xFF) of the Status parameter are made available to it.
Dec 30 '06 #8

Post your reply

Sign in to post your reply or Sign up for a free account.