The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM
Home Forums Register Rules & FAQ Members List Arcade Search Today's Posts Mark Forums Read


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.


Other UNIX.COM Threads 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 12:44 AM
display in a child process a command called in the parent one remid1985 High Level Programming 7 01-19-2007 02:40 PM
how to find the chid process id from given parent process id guhas Shell Programming and Scripting 3 10-13-2005 05:13 AM
Implementing 2 pipes between a parent and child process bwgoudey High Level Programming 2 09-24-2005 08:14 PM
parent and child process question? tosa High Level Programming 0 02-16-2005 11:04 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-07-2008
Registered User
 

Join Date: Mar 2008
Posts: 4
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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 !
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-07-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,203
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
wait () returns the exit code from a child.

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

Join Date: Dec 2007
Location: Virginia, USA.
Posts: 202
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Code:
pid_t startChild(void (*funion)(void *arg), void *anarg) {
pid_t chld;
                       if ( (chld = fork()) == 0) {
                           funion(anarg);
                       }
                       return chld;
}
Reply With Quote
  #4 (permalink)  
Old 04-07-2008
Registered User
 

Join Date: Mar 2008
Posts: 4
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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().
Reply With Quote
  #5 (permalink)  
Old 04-07-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,203
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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?
Reply With Quote
Google UNIX.COM
Reply



Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB 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 -7. The time now is 06:19 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger

Search Engine Optimization by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102