Concurrent computing: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 40: | Line 40: | ||
[[Category:Perl]] |
[[Category:Perl]] |
||
'''Interpreter:''' [[Perl]] 5.x |
'''Interpreter:''' [[Perl]] 5.x |
||
In the child code, you may have to re-open database handles and such. |
|||
FORK: |
|||
if ($pid = fork()) { |
|||
# parent code |
|||
} elsif (defined($pid)) { |
|||
setsid; # tells apache to let go of this process and let it run solo |
|||
# disconnect ourselves from input, output, and errors |
|||
close(STDOUT); |
|||
close(STDIN); |
|||
close(STDERR); |
|||
# re-open to /dev/null to prevent irrelevant warn messages. |
|||
open(STDOUT, '>/dev/null'); |
|||
open(STDIN, '>/dev/null'); |
|||
open(STDERR, '>>/home/virtual/logs/err.log'); |
|||
# child code |
|||
exit; # important to exit |
|||
} elsif($! =~ /emporar/){ |
|||
warn '[' . localtime() . "] Failed to Fork - Will try again in 10 seconds.\n"; |
|||
sleep(10); |
|||
goto FORK; |
|||
} else { |
|||
warn '[' . localtime() . "] Unable to fork - $!"; |
|||
exit(0); |
|||
} |
Revision as of 17:26, 6 February 2007
Concurrent computing
You are encouraged to solve this task according to the task description, using any language you may know.
You are encouraged to solve this task according to the task description, using any language you may know.
Using either native language concurrency syntax or freely available libraries write a program to display the strings "Enjoy" "Rosetta" "Code", one string per line, in random order. Concurrency syntax must use threads, tasks, co-routines, or whatever concurrency is called in your language.
Ada
with Ada.Text_Io; use Ada.Text_Io; with Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random; with Ada.strings.Unbounded; use Ada.Strings.Unbounded; procedure Concurrent_Hello is task type Writer is entry Start(Message : String); end Writer; task body Writer is Seed : Generator; Sleep_Time : Float; Words : Unbounded_String; begin Reset(Seed); Sleep_Time := Random(Seed); accept Start(Message : String) do Words := To_Unbounded_String(Message); end Start; delay Duration(Sleep_Time); Put_Line(To_String(Words)); end Writer; T1 : Writer; T2 : Writer; T3 : Writer; begin T1.Start("Enjoy"); T2.Start("Rosetta"); T3.Start("Code"); end Concurrent_Hello;
Perl
Interpreter: Perl 5.x
In the child code, you may have to re-open database handles and such.
FORK: if ($pid = fork()) { # parent code } elsif (defined($pid)) { setsid; # tells apache to let go of this process and let it run solo # disconnect ourselves from input, output, and errors close(STDOUT); close(STDIN); close(STDERR); # re-open to /dev/null to prevent irrelevant warn messages. open(STDOUT, '>/dev/null'); open(STDIN, '>/dev/null'); open(STDERR, '>>/home/virtual/logs/err.log'); # child code exit; # important to exit } elsif($! =~ /emporar/){ warn '[' . localtime() . "] Failed to Fork - Will try again in 10 seconds.\n"; sleep(10); goto FORK; } else { warn '[' . localtime() . "] Unable to fork - $!"; exit(0); }