473,386 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Collecting value returned by main()

vpawizard
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
7 1730
r035198x
13,262 8TB
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
horace1
1,510 Expert 1GB
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
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
horace1
1,510 Expert 1GB
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
/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
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
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

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

Similar topics

25
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default...
11
by: Sweety | last post by:
hello to all members, i have strange question in C. main() { printf("%d",main) ; } here o/p is same for all m/c in TC++ version 3.0 i.e 657. I think this is not garbage.
5
by: Robert Oschler | last post by:
I am converting a Perl script over to "C" for a potential open source project. I need some open source "C" code that will give me the same functionality of a Perl Style associative array: ...
1
by: Dan | last post by:
hi ng, i want to give the invoker of my application information whether application has passed successfully. it shall get scheduled, and if the exeution did not pass, it should be repeated some...
20
by: lovecreatesbeauty | last post by:
Hello experts, Is the following code snippet legal? If it is, how can exit() do the keyword return a favor and give a return value to the main function? Can a function call (or only this...
17
by: romixnews | last post by:
Hi, I'm facing the problem of analyzing a memory allocation dynamic and object creation dynamics of a very big C++ application with a goal of optimizing its performance and eventually also...
9
by: DanielJohnson | last post by:
I am wondering where does the value returned by main goes. Suppoes main returns some number say 42, where is it stored. Supposed a shell script call a C program and the program returns the value...
4
by: kinaxx | last post by:
Hello, now I'm learning progamming language in university. but i have some question. in textbook. says there are four passing Mechanism 1) pass by value (inother words : call by value) 2)...
11
by: kinaxx | last post by:
Hello, now I'm learning progamming language in university. but i have some question. in textbook. says there are four passing Mechanism 1) pass by value (inother words : call by value) 2)...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.