Fork

From Rosetta Code
Revision as of 00:07, 14 April 2009 by rosettacode>Spoon! (added standard ml)
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.

ALGOL 68

Translation of: C
Works with: ALGOL 68G version Any - tested with release mk15-0.8b.fc9 - "fork" is not part of the standard's preclude.

<lang> main: (

 INT pid;
 IF (pid:=fork)=0 THEN
   print("This is new process")
 ELIF pid>0 THEN
   print("This is the original process")
 ELSE
   print("ERROR: Something went wrong")
 FI

) </lang> Output:

This is new process
This is the original process

C

Works with: gcc
Library: POSIX

<lang 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;

}</lang>

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

<lang 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";;</lang>

Perl

Works with: Perl version 5.x

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

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

}</lang>

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

<lang python>import os

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

# parent code

else:

# child code</lang>

Ruby

<lang ruby>pid = fork if pid

# parent code

else

# child code

end</lang> or <lang ruby>fork do

 # child code

end

  1. parent code</lang>

Smalltalk

<lang smalltalk>'Here I am' displayNl. |a| a := [

 (Delay forSeconds: 2) wait . 
 1 to: 100 do: [ :i | i displayNl ]

] fork. 'Child will start after 2 seconds' displayNl. "wait to avoid terminating first the parent;

a better way should use semaphores"

(Delay forSeconds: 10) wait.</lang>

Standard ML

<lang sml>case Posix.Process.fork () of

  SOME pid => print "This is the original process\n"
| NONE     => print "This is the new process\n";

</lang>

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")