By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,003 Members | 1,163 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,003 IT Pros & Developers. It's quick & easy.

Instead of using fork() how to releasing child process in windows fasion?

P: 5
For sending the message in linux environment-
the perl script however uses fork() to release the client process as soon as possible and then in the child process handles the message send.
For sending the message in windows environment-
instead of using fork how to release the child process in the Window fasion? since fork() is not available in windows.
Jun 11 '07 #1
Share this Question
Share on Google+
3 Replies


miller
Expert 100+
P: 1,089
since fork() is not available in windows.
Greetings raghav82,

fork is available on windows, but only for perl 5.8 and after. I suggust that you upgrade. The following works fine on my Windows2000 box ActivePerl 5.8.8:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2.  
  3. local $| = 1; # Unbuffer STDOUT
  4.  
  5. my $pid = fork();
  6. die "cannot fork: $!" unless defined $pid;
  7.  
  8. # Parent Process
  9. if ($pid) {
  10.     print "A$_\n" for (1..1000);
  11.  
  12. # Child Process
  13. } else {
  14.     print "       B$_\n" for (1..1000);
  15.     exit(0);
  16. }
  17.  
Alternatively, there is always the threads pragma as well, which is even better for a lot of purposes:

Expand|Select|Wrap|Line Numbers
  1. use threads;
  2. use threads::shared;
  3.  
  4. use strict;
  5.  
  6. local $| = 1; # Unbuffer STDOUT
  7.  
  8. my $thr1 = threads->new (sub {
  9.     print "A$_\n" for (1..1000);
  10. });
  11. my $thr2 = threads->new (sub {
  12.     print "       B$_\n" for (1..1000);
  13. });
  14.  
  15. # rejoin the threads when they finish
  16. $thr1->join;
  17. $thr2->join;
  18.  
  19. exit;
  20.  
Typical Output:
Expand|Select|Wrap|Line Numbers
  1. ...
  2.        B966
  3.        B967
  4.        B968
  5.        B969
  6.        B970
  7.        B971
  8.        B972
  9.        B973
  10.        B974
  11.        B975
  12. A982
  13. A983
  14. A984
  15. A985
  16. A986
  17. A987
  18. A988
  19. A989
  20. A990
  21. A991
  22. A992
  23. A993
  24. A994
  25. A995
  26. A996
  27. A997
  28. A998
  29.        B976
  30.        B977
  31.        B978
  32.        B979
  33.        B980
  34.        B981
  35.        B982
  36.        B983
  37.        B984
  38.        B985
  39.        B986
  40.        B987
  41.        B988
  42.        B989
  43.        B990
  44.        B991
  45.        B992
  46. A999
  47. A1000
  48.        B993
  49.        B994
  50.        B995
  51.        B996
  52.        B997
  53.        B998
  54.        B999
  55.        B1000
  56.  
- Miller
Jun 11 '07 #2

P: 5
Thank U miller for your quick reply. can i know is their any way i can do this in the perl vesions which does not support fork()?
Jun 12 '07 #3

miller
Expert 100+
P: 1,089
First of all, I completely don't understand what you're trying to achieve. Your original description did not really make sense.

Nevertheless, as I already said in my first post, the threads pragma was used as an alternative to forking in windows until support was added with version 5.8. Read the link that I already provided to you for details.

- Miller
Jun 12 '07 #4

Post your reply

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