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

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

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
3 4203
miller
1,089 Expert 1GB
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
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
1,089 Expert 1GB
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

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

Similar topics

2
by: CwK | last post by:
How to use fork() system function to fork multi child process at the same time ? For example: Run a program to fork 5 child process at the same time and the parent must wait until all child...
6
by: shellcode | last post by:
the code: ------fork.c------ #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> int main() {
11
by: ramu | last post by:
Hi All, We know that a c function never returns more than one value. Then how come the fork() function returns two values? How it is implemented? Regards
0
by: spacelabstudio | last post by:
Hi, I'm observing some weird behavior and have written the following test program to demonstrate. This works under cygwin/WinXP but not Gentoo(kernel 2.6): huh.py...
1
by: chad.vice | last post by:
I have a CGIC program that generates reporting output on-the-fly. At times this generation can take a long time, and session timeouts occur while users wait for the page results to be displayed. ...
3
by: thrillseekersforever | last post by:
The questions(A&B) are to fine no# of process running from the below codes. However, I couldn't decipher the solution. Will someone please throw some light on this? Thanks a lot!! A] void...
5
by: vsowmya | last post by:
Hi I have to develop a c program to create a parent process that spawn three child process. i have written a code but that was not working out.Please help me in getting the desired output of my...
5
by: JoeW | last post by:
Now before I go into detail I just want to say that this is purely for my own benefit and has no real world usage. I remember way back when the tool for *nix systems called forkbomb was created. I...
1
by: ohaqqi | last post by:
Hi guys, I'm still working on my shell. I'm trying to implement a function typefile that will take a command line input as follows: > type <file1> This command will implement a catenation of...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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.