473,405 Members | 2,210 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,405 software developers and data experts.

how to return values from several forks

I'm working on a program that simulates a certain model with several paramters and returns one result value. I want to check how changing each parameter affects the result. What I did before was running the simulation with the base set of paramters to get a baseline and then change one parameter and run it again. Now I found out about fork() and can just run fork() for each set of parameters. This runs a lot faster, so it's preferrable.
I want to return not the absolute result values of each parameter set but the difference to the base. I have learned how to let two processes communicate from http://docstore.mik.ua/orelly/perl/cookbook/ch16_11.htm, but it fails when I try to spawn several forks and get their result back.

in pseudocode:
Expand|Select|Wrap|Line Numbers
  1. for parameter = each parameter_set
  2.   fork()
  3. if parent
  4.   base = sim(base_parameters)
  5.   wait for return values
  6.   print return1 - base, return2 - base etc
  7. else
  8.   result = sim(parameter)
  9.   return result
Oct 19 '10 #1
2 4639
chorny
80 Expert
Try using module like Child.
Oct 25 '10 #2
Thanks for the answer chorny, but I need the script to be able to run on other machines without them installing extra modules, so that won't work.

I have however found a solution that works if I know the number of children I need to spawn before runtime. It's far from elegant, but it works ;)

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. # multifork test
  3.  
  4. use IO::Handle;
  5.  
  6. #prepare pipes, one per child
  7. pipe(READER0, WRITER0);
  8. WRITER0->autoflush(1);
  9. pipe(READER1, WRITER1);
  10. WRITER1->autoflush(1);
  11. pipe(READER2, WRITER2);
  12. WRITER2->autoflush(1);
  13. pipe(READER3, WRITER3);
  14. WRITER3->autoflush(1);
  15.  
  16. if ($pid[0] = fork) { # i'm the parent
  17.     close WRITER0;
  18.     if ($pid[1] = fork) { # i'm still the parent
  19.         close WRITER1;
  20.         if($pid[2] = fork) { # i'm still the parent
  21.             close WRITER2;
  22.             if($pid[3] = fork) { # i'm still the parent
  23.                 close WRITER3;
  24.             } else { # i'm child 3
  25.                 die "cannot fork: $!" unless defined $pid[3];
  26.                 close READER3;
  27.                 # do childish stuff here
  28.                 print WRITER3 "Child Pid $$ is sending this\n";
  29.                 close WRITER3;
  30.                 exit;
  31.             }
  32.         } else { # i'm child 2
  33.             die "cannot fork: $!" unless defined $pid[2];
  34.             close READER2;
  35.             # do childish stuff here
  36.             print WRITER2 "Child Pid $$ is sending this\n";
  37.             close WRITER2;
  38.             exit;
  39.         }
  40.     } else { # i'm child 1
  41.         die "cannot fork: $!" unless defined $pid[1];
  42.         close READER1;
  43.         # do childish stuff here
  44.         print WRITER1 "Child Pid $$ is sending this\n";
  45.         close WRITER1;
  46.         exit;
  47.     }
  48. } else { # i'm child 0
  49.     die "cannot fork: $!" unless defined $pid[0];
  50.     close READER0;
  51.     # do childish stuff here
  52.     print WRITER0 "Child Pid $$ is sending this\n";
  53.     close WRITER0;
  54.     exit;
  55. }
  56.  
  57. # listen to your children:
  58. chomp($line[0] = <READER0>);
  59. chomp($line[1] = <READER1>);
  60. chomp($line[2] = <READER2>);
  61. chomp($line[3] = <READER3>);
  62. # print out what the children said
  63. print "Parent Pid $$ just read this: $line[0]\n";
  64. print "Parent Pid $$ just read this: $line[1]\n";
  65. print "Parent Pid $$ just read this: $line[2]\n";
  66. print "Parent Pid $$ just read this: $line[3]\n";
  67. #wait for children to finish
  68. for ($n=0;$n<=3;$n++) {
  69.     waitpid($pid[$n], 0);
  70. }
Oct 26 '10 #3

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

Similar topics

66
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
1
by: Jack Addington | last post by:
I have a 3rd party object that fires an itemchanged event when someone edits some data on a form. This event has a custom eventArgs that has a field called ActionCode. In the code of the event,...
5
by: Edward Diener | last post by:
I am gathering from the documentation that return values from __events are not illegal but are frowned upon in .NET. If this is the case, does one pass back values from an event handler via...
18
by: skishorev | last post by:
Hi, Here I am taking two functions. void f(int,int) and another one is float f(int,int). Is it possible to overload with return values. Thx, kishore
2
by: gbanister | last post by:
I'd like to repost a message that I found in this group almost one year ago, because it's the exact problem I'm in and there was no solution offered to this post last year. (note: I was not the...
43
by: Tim Chase | last post by:
Just as a pedantic exercise to try and understand Python a bit better, I decided to try to make a generator or class that would allow me to unpack an arbitrary number of calculatible values. In...
17
by: binjobster | last post by:
Hello everyone, I'm updating some documents that describes so many C/C++ functions/method(about 2500). But I'm not familiar with these codes. So I want to find such a utility can generate...
8
by: aleksandar.ristovski | last post by:
Hello all, I have been thinking about a possible extension to C/C++ syntax. The current syntax allows declaring a function that returns a value: int foo(); however, if I were to return...
4
by: barcaroller | last post by:
I am trying to adopt a model for calling functions and checking their return values. I'm following Scott Meyer's recommendation of not over-using exceptions because of their potential overhead. ...
14
by: =?Utf-8?B?QmVu?= | last post by:
Hi all, I'm trying to understand the concept of returning functions from the enclosing functions. This idea is new to me and I don't understand when and why I would need to use it. Can someone...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.