The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-07-2008
Ametis1970 Ametis1970 is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 4
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()
{
...
return 15647
}
parent.c calls child using fork()-exec() or posix_spawn().
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 !
  #2 (permalink)  
Old 04-07-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
wait () returns the exit code from a child.

By definition, there are two processes communicating here, so even this is a form of IPC.
  #3 (permalink)  
Old 04-07-2008
ramen_noodle ramen_noodle is offline Forum Advisor  
Registered User
  
 

Join Date: Dec 2007
Location: Virginia, USA.
Posts: 251
Code:

pid_t startChild(void (*funion)(void *arg), void *anarg) {
pid_t chld;
                       if ( (chld = fork()) == 0) {
                           funion(anarg);
                       }
                       return chld;
}
  #4 (permalink)  
Old 04-07-2008
Ametis1970 Ametis1970 is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 4
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().
  #5 (permalink)  
Old 04-07-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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?
  #6 (permalink)  
Old 04-07-2008
ramen_noodle ramen_noodle is offline Forum Advisor  
Registered User
  
 

Join Date: Dec 2007
Location: Virginia, USA.
Posts: 251
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.
  #7 (permalink)  
Old 04-07-2008
Ametis1970 Ametis1970 is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 4
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
Closed Thread

Bookmarks

Tags
linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 12:20 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0