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.
7 1730
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).
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
I tried the following but still getting PID of child:
Code for mainprg.c: -
-
#include <unistd.h>
-
#include <stdio.h>
-
#include <process.h>
-
-
int main()
-
{
-
char num1[]="20";
-
char num2[]="30";
-
int wait_status;
-
-
wait_status=spawnl(1,"./arithmetic.exe",num1,num2,(char*)NULL);
-
printf("%d\n",wait_status);
-
-
return 0;
-
}
-
-
Code for arithmetic.c: -
-
-
#include <unistd.h>
-
#include <stdlib.h>
-
#include <math.h>
-
-
int main(int argc,char* argv[])
-
{
-
exit(atoi(argv[0]) + atoi(argv[1]));
-
}
-
-
I tried the following but still getting PID of child:
Code for mainprg.c: -
wait_status=spawnl(1,"./arithmetic.exe",num1,num2,(char*)NULL);
-
-
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.
/usr/include/process.h :
#define _P_WAIT 1
#define _P_NOWAIT 2
#define _P_OVERLAY 3
#define _P_NOWAITO 4
#define _P_DETACH 5
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 !!!
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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.
|
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:
...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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)...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
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...
|
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...
|
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,...
|
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...
| |