Fork: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|Erlang}}: changing fork to new style (with module:fun and arg))
(added ruby; not tested)
Line 4: Line 4:
{{works with|gcc}}
{{works with|gcc}}
{{libheader|POSIX}}
{{libheader|POSIX}}
<c>#include<stdio.h>
<pre>
#include<stdio.h>
#include<unistd.h>
#include<unistd.h>


Line 21: Line 20:


return 0;
return 0;
}</c>
}
</pre>


=={{header|Erlang}}==
=={{header|Erlang}}==
Line 42: Line 40:
=={{header|OCaml}}==
=={{header|OCaml}}==


#load "unix.cma";;
<ocaml>#load "unix.cma";;
let pid = Unix.fork ();;
let pid = Unix.fork ();;
if pid > 0 then
if pid > 0 then
print_endline "This is the original process"
print_endline "This is the original process"
else
else
print_endline "This is the new process";;
print_endline "This is the new process";;</ocaml>


=={{header|Perl}}==
=={{header|Perl}}==
Line 53: Line 51:
In the child code, you may have to re-open database handles and such.
In the child code, you may have to re-open database handles and such.


FORK:
<perl>FORK:
if ($pid = fork()) {
if ($pid = fork()) {
# parent code
# parent code
} elsif (defined($pid)) {
} elsif (defined($pid)) {
setsid; # tells apache to let go of this process and let it run solo
setsid; # tells apache to let go of this process and let it run solo
# disconnect ourselves from input, output, and errors
# disconnect ourselves from input, output, and errors
close(STDOUT);
close(STDOUT);
close(STDIN);
close(STDIN);
close(STDERR);
close(STDERR);
# re-open to /dev/null to prevent irrelevant warn messages.
# re-open to /dev/null to prevent irrelevant warn messages.
open(STDOUT, '>/dev/null');
open(STDOUT, '>/dev/null');
open(STDIN, '>/dev/null');
open(STDIN, '>/dev/null');
open(STDERR, '>>/home/virtual/logs/err.log');
open(STDERR, '>>/home/virtual/logs/err.log');
# child code
# child code
exit; # important to exit
exit; # important to exit
} elsif($! =~ /emporar/){
} elsif($! =~ /emporar/){
warn '[' . localtime() . "] Failed to Fork - Will try again in 10 seconds.\n";
warn '[' . localtime() . "] Failed to Fork - Will try again in 10 seconds.\n";
sleep(10);
sleep(10);
goto FORK;
goto FORK;
} else {
} else {
warn '[' . localtime() . "] Unable to fork - $!";
warn '[' . localtime() . "] Unable to fork - $!";
exit(0);
exit(0);
}</perl>
}


Obviously you could do a Fork in a lot less lines, but this code covers all the bases
Obviously you could do a Fork in a lot less lines, but this code covers all the bases
Line 92: Line 90:
=={{header|Python}}==
=={{header|Python}}==
{{works with|Python|2.5}}
{{works with|Python|2.5}}
import os
<python>import os

pid = os.fork()
pid = os.fork()
if pid > 0:
if pid > 0:
# parent code
# parent code
else:
else:
# child code</python>

=={{header|Ruby}}==
<ruby>pid = fork
if pid > 0
# parent code
else
# child code
end</ruby>
or
<ruby>fork do
# child code
# child code
end
# parent code</ruby>


=={{header|Toka}}==
=={{header|Toka}}==

Revision as of 01:30, 22 December 2008

Task
Fork
You are encouraged to solve this task according to the task description, using any language you may know.

In this task, the goal is to spawn a new process which can run simultaneously with, and independently of, the original parent process.

C

Works with: gcc
Library: POSIX

<c>#include<stdio.h>

  1. include<unistd.h>

int main(void) {

 pid_t pid;
 if((pid=fork())==0) {
   printf("This is new process\n");
 } else if(pid>0) {
   printf("This is the original process\n");
 } else {
   printf("ERROR: Something went wrong\n");
 }
 return 0;

}</c>

Erlang

-module(fork).
-export([start/0]).

start() ->
    spawn(fork,child,[]),
    io:format("This is the original process~n").

child() ->
    io:format("This is the new process~n").

Then you can compile your code and execute it:

c(fork).
fork:start().

OCaml

<ocaml>#load "unix.cma";; let pid = Unix.fork ();; if pid > 0 then

 print_endline "This is the original process"

else

 print_endline "This is the new process";;</ocaml>

Perl

Works with: Perl version 5.x

In the child code, you may have to re-open database handles and such.

<perl>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);

}</perl>

Obviously you could do a Fork in a lot less lines, but this code covers all the bases

Pop11

lvars ress;
if sys_fork(false) ->> ress then
   ;;; parent
   printf(ress, 'Child pid = %p\n');
else
   printf('In child\n');
endif;

Python

Works with: Python version 2.5

<python>import os

pid = os.fork() if pid > 0:

# parent code

else:

# child code</python>

Ruby

pid = fork if pid > 0

# parent code

else

# child code

end or fork do

 # child code

end

  1. parent code

Toka

needs shell
getpid is-data PID
[ fork getpid PID = [ ." Child PID: " . cr ] [ ." In child\n" ] ifTrueFalse ] invoke

UnixPipes

Demonstrating a subshell getting forked, and running concurrently with the original process

(echo "Process 1" >&2 ;sleep 5; echo "1 done" ) | (echo "Process 2";cat;echo "2 done")