wait: Wait for all children processes to finish

Description Usage See Also Examples

View source: R/bfork.R

Description

Halts execution until all child processes terminate.

Usage

1
    wait()

See Also

fork.
waitpid.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
    ## destination for plots
    dest <- "/path/to/dest/"

    ## example function to be forked
    fn <- function() {
        Sys.sleep(1) # simulate some work
        img_name <- paste("child", i, sep="-")
        png(paste(dest, img_name, ".png", sep=""))
        plot(0,0, xlim=c(0,2), ylim=c(0,2), type="n")
        text(1,1, paste("Child [", i, "]: Hello, World!", sep=""))
        dev.off()
    }
    
    starttime <- Sys.time()

    ## fork the child processes
    for(i in 1:10) {
        fork(fn)
    }
    
    ## wait for all processes to finish
    wait()

    endtime <- Sys.time()

    print(paste("Total time taken to run 10 1s processes: ",
                round(endtime - starttime, 2), "s", sep=""))
    ## Output: (Time taken may vary)
    ##   Total time taken to run 10 1s processes: 1.68s

Example output

Error in plot.new() : could not open file '/path/to/dest/child-1.png'
Calls: fork ... .Call -> <Anonymous> -> plot -> plot.default -> plot.new
Execution halted
Warning message:
system call failed: Resource temporarily unavailable 
Error in plot.new() : could not open file '/path/to/dest/child-5.png'
Calls: fork ... .Call -> <Anonymous> -> plot -> plot.default -> plot.new
Execution halted
Error in plot.new() : could not open file '/path/to/dest/child-4.png'
Calls: fork ... .Call -> <Anonymous> -> plot -> plot.default -> plot.new
Execution halted
Warning message:
system call failed: Resource temporarily unavailable 
Warning message:
system call failed: Resource temporarily unavailable 
Error in plot.new() : could not open file '/path/to/dest/child-10.png'
Calls: fork ... .Call -> <Anonymous> -> plot -> plot.default -> plot.new
Execution halted
sh: 1: rm: Permission denied
sh: 1: rm: Permission denied
sh: 1: rm: Permission denied
sh: 1: rm: Permission denied
NULL
[1] "Total time taken to run 10 1s processes: 1.06s"

bfork documentation built on May 29, 2017, 10:01 p.m.