![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| getting the return code of forked child process (ftp) | KittyJ | High Level Programming | 7 | 08-19-2007 03:44 AM |
| display in a child process a command called in the parent one | remid1985 | High Level Programming | 7 | 01-19-2007 06:40 PM |
| how to find the chid process id from given parent process id | guhas | Shell Programming and Scripting | 3 | 10-13-2005 08:13 AM |
| Implementing 2 pipes between a parent and child process | bwgoudey | High Level Programming | 2 | 09-24-2005 11:14 PM |
| parent and child process question? | tosa | High Level Programming | 0 | 02-16-2005 03:04 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
Can a child process return a specific value to a parent process ?
Hello everybody !
I have two C source file: child.c and parent.c. child.c looks like that: Code:
int main()
{
How can I get in parent the return code from child (here value is 15647) without using interprocess communication (pipe, shared memory, ...) ? Is there any way to do that without IPC ? Thank you ! |
|
||||
|
Thank you for answers !
To era: Maybe I was not clear. Supposed I want to calculate a product of two integers in child process. So, child.c looks like that: ... some #include int main(int argc, char *argv[]) { int a; int b; a=atoi(argv[1]); b=atoi(argv[2]); return a*b;} Under Windows, if I use this code, in parent.c: int main() { int result; char *args[4]; // prepare arguments for child process args[0] = "child.exe"; args[1] = argv[1]; args[2] = argv[2]; args[3] = NULL; // launch the child process result=_spawnvp (_P_WAIT, args[0], args); printf("Product: %d, result); return 0;} I'm able to catch the return product from child (a*b) as return from _spawnvp() function in result variable. Under Linux, posix_spawnp() or wait() return only EXIT_FAILURE or EXIT_SUCCES. I didn't find a way to catch in the parent the product value calculate in the child (value which is returned in child). To ramen_noodle: I don't understand you startChild() function. I don't know what is funion. Is a function or a data type ? I didn't find reference to funion(). |
|
||||
|
You do get slightly more with WEXITSTATUS(status) after you've done a waitpid(pid, &status, ...) but this was never meant as a facility for anything more than very small integers.
I I understand ramen_noodle's hint correctly, you would have a function pointer "funion" which you run in the child, and which returns its result in the memory location pointed to by anarg. What are you trying to achieve; why is regular IPC out of the question? |
|
||||
|
Thanks for the assist Era.
All that can be returned from a child is it's pid without ipc. That's all my example does. Remember that at fork the parents address space is copied into the child. Afterwards no assignments are shared. The exception is vfork. There are some ugly hacks that can accomplish what you need using this. You may want to consider threads instead. It sounds like what you have in mind. Traditionally in a process (ipc) paradigm: Pipes are simple and clean. Everything else is going to take time to get used to. |
|
||||
|
Thank you very much !
Despite popular believe, there are many things that are simpler to do under Windows than under Unix. I am sorry that I'm not so skilled in C to understand ramen_noodle's code. So, finally, there is no simple way to return a value from child into parent without IPC. That's I want to know. Thank you again for your answers. |
| Sponsored Links | ||
|
|