R : Copyright 2005, The R Foundation for Statistical Computing Version 2.1.1 (2005-06-20), ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for a HTML browser interface to help. Type 'q()' to quit R. > ### *
> ### > attach(NULL, name = "CheckExEnv") > assign(".CheckExEnv", as.environment(2), pos = length(search())) # base > ## add some hooks to label plot pages for base and grid graphics > setHook("plot.new", ".newplot.hook") > setHook("persp", ".newplot.hook") > setHook("grid.newpage", ".gridplot.hook") > > assign("cleanEx", + function(env = .GlobalEnv) { + rm(list = ls(envir = env, all.names = TRUE), envir = env) + RNGkind("default", "default") + set.seed(1) + options(warn = 1) + delayedAssign("T", stop("T used instead of TRUE"), + assign.env = .CheckExEnv) + delayedAssign("F", stop("F used instead of FALSE"), + assign.env = .CheckExEnv) + sch <- search() + newitems <- sch[! sch %in% .oldSearch] + for(item in rev(newitems)) + eval(substitute(detach(item), list(item=item))) + missitems <- .oldSearch[! .oldSearch %in% sch] + if(length(missitems)) + warning("items ", paste(missitems, collapse=", "), + " have been removed from the search path") + }, + env = .CheckExEnv) > assign("..nameEx", "__{must remake R-ex/*.R}__", env = .CheckExEnv) # for now > assign("ptime", proc.time(), env = .CheckExEnv) > grDevices::postscript("fork-Examples.ps") > assign("par.postscript", graphics::par(no.readonly = TRUE), env = .CheckExEnv) > options(contrasts = c(unordered = "contr.treatment", ordered = "contr.poly")) > options(warn = 1) > library('fork') > > assign(".oldSearch", search(), env = .CheckExEnv) > assign(".oldNS", loadedNamespaces(), env = .CheckExEnv) > cleanEx(); ..nameEx <- "exit" > > ### * exit > > flush(stderr()); flush(stdout()) > > ### Name: exit > ### Title: Exit from a child process > ### Aliases: exit > ### Keywords: programming > > ### ** Examples > > waittest <- function() + { + pid = fork(NULL) + if(pid==0) + { + cat("Hi, I'm the child process and I need to explicitly call exit().") + cat("\n\n") + exit() + } + else + { + wait(pid) + cat("Hi, I'm the main process, and I wait()ed until the child process\n") + cat("finished before introducing myself.\n") + } + } > > waittest() Hi, I'm the child process and I need to explicitly call exit(). > > ### Name: exit > ### Title: Exit from a child process > ### Aliases: exit > ### Keywords: programming > > ### ** Examples > > waittest <- function() + { + pid = fork(NULL) + if(pid==0) + { + cat("Hi, I'm the child process and I need to explicitly call exit().") + cat("\n\n") + exit() + } + else + { + wait(pid) + cat("Hi, I'm the main process, and I wait()ed until the child process\n") + cat("finished before introducing myself.\n") + } + } > > waittest() Hi, I'm the main process, and I wait()ed until the child process finished before introducing myself. > > > > cleanEx(); ..nameEx <- "fork" > > ### * fork > > flush(stderr()); flush(stdout()) > > ### Name: fork > ### Title: Create a new R process using the Unix 'fork' system call > ### Aliases: fork > ### Keywords: programming > > ### ** Examples > > > ### > # Count from 1 to 10 in a separate process > ### > > # define the function to do the work > testfun <- function() + { + cat("Counting in process", getpid(), "\n") + for(i in 1:10) + { + i <<- i+1 # assign into Global environment + cat("i=",i,"\n") + } + cat("Done counting in process", getpid(), "\n") + } > > # run normally, the function will change our value of i > i <- 0 > testfun() Counting in process 1581 i= 1 i= 2 i= 3 i= 4 i= 5 i= 6 i= 7 i= 8 i= 9 i= 10 Done counting in process 1581 > i [1] 11 > > # Run in a separate process, our value of i remains unchanged > i <- 0 > { + pid <- fork(testfun) + wait(pid) # wait until the child finishes, then display its exit status + } Counting in process 1586 i= 1 i= 2 i= 3 i= 4 i= 5 i= 6 i= 7 i= 8 i= 9 i= 10 Done counting in process 1586 > > # Run in a separate process, our value of i remains unchanged > i <- 0 > { + pid <- fork(testfun) + wait(pid) # wait until the child finishes, then display its exit status + } pid status 1586 0 > > > ### > # Use a socket to communicate between two processes. Information > # typed on the console, which is read by the initial process, will be send > # to the child process for display. > ### > ## Not run: > ##D send <- function() > ##D { > ##D pid <- getpid() > ##D con1 <- socketConnection(Sys.info()["nodename"], port = 6011) > ##D i <- 1 > ##D while(TRUE) > ##D { > ##D cat("[",pid,"] ",i,": ",sep="") > ##D data <- readLines(stdin(), n=1) > ##D writeLines(data, con=con1) > ##D if( length(grep("quit", data))>0 ) > ##D break; > ##D i <- i+1 > ##D } > ##D close(con1) > ##D } > ##D > ##D recieve <- function() > ##D { > ##D pid <- getpid() > ##D con2 <- socketConnection(port = 6011, block=TRUE, server=TRUE) > ##D i <- 1 > ##D while(TRUE) > ##D { > ##D data <- readLines(con2, n=1) > ##D cat("[",pid,"] ",i,": ",sep="") > ##D writeLines(data, stdout()) > ##D if( length(grep("quit", data))>0 ) > ##D break; > ##D i <- i+1 > ##D } > ##D close(con2) > ##D } > ##D > ##D pid <- fork(recieve) > ##D send() > ## End(Not run) > > > > cleanEx(); ..nameEx <- "getpid" > > ### * getpid > > flush(stderr()); flush(stdout()) > > ### Name: getpid > ### Title: Obtain the process id for the current process. > ### Aliases: getpid > ### Keywords: programming > > ### ** Examples > > > getpid() [1] 1581 > > ## Not run: > ##D for(i in 1:10) > ##D fork( function() { cat("PID:", getpid(), "\n"); exit()} ) > ## End(Not run) > > > > cleanEx(); ..nameEx <- "kill" > > ### * kill > > flush(stderr()); flush(stdout()) > > ### Name: kill > ### Title: Send a signal to one or more processes. > ### Aliases: kill killall > ### Keywords: programming > > ### ** Examples > > > # start a process that just sleeps for 10 seconds > sleepy <- function() + { + cat("Going to sleep..") + Sys.sleep(10) + cat("Woke up!") + } > pid <- fork( sleepy ) > > # kill the sleeping process > kill(pid) [1] 0 > > > > > cleanEx(); ..nameEx <- "signame" > > ### * signame > > flush(stderr()); flush(stdout()) > > ### Name: signame > ### Title: Obtain information about signals > ### Aliases: signame sigval siglist > ### Keywords: programming > > ### ** Examples > > > # look up the numeric value for SIGABT: > sigval("SIGKILL") name val desc "SIGKILL" "9" "Killed" > > # the 'SIG' component can be ommitted: > sigval("HUP") name val desc "SIGHUP" "1" "Hangup" > > # now look up based on value > signame(9) name val desc "SIGKILL" "9" "Killed" > > # and get a complete table of signal information > siglist() name val desc 1 1 SIGHUP Hangup 2 2 SIGINT Interrupt 3 3 SIGQUIT Quit 4 4 SIGILL Illegal Instruction 5 5 SIGTRAP Trace or Breakpoint Trap 6 6 SIGABRT Abort 7 7 SIGEMT Emulation Trap 8 8 SIGFPE Arithmetic Exception 9 9 SIGKILL Killed 10 10 SIGBUS Bus Error 11 11 SIGSEGV Segmentation Fault 12 12 SIGSYS Bad System Call 13 13 SIGPIPE Broken Pipe 14 14 SIGALRM Alarm Clock 15 15 SIGTERM Terminated 16 30 SIGUSR1 User Signal 1 17 31 SIGUSR2 User Signal 2 18 20 SIGCHLD Child Status Changed 19 28 SIGWINCH Window Size Change 20 16 SIGURG Urgent Socket Condition 21 17 SIGSTOP Stopped (Signal) 22 18 SIGTSTP Stopped (User) 23 19 SIGCONT Continued 24 21 SIGTTIN Stopped (tty input) 25 22 SIGTTOU Stopped (tty output) 26 26 SIGVTALRM Virtual Timer Expired 27 27 SIGPROF Profiling Timer Expired 28 24 SIGXCPU CPU Time limit Exceeded 29 25 SIGXFSZ File Size Limit Exceeded 30 6 SIGIOT IOT trap. (Synonym for SIGABRT) 31 23 SIGIO I/O Now Possible (4.2 BSD} 32 29 SIGINFO Power Fail or Restart (Synonym for SIGPWR} > > > > cleanEx(); ..nameEx <- "wait" > > ### * wait > > flush(stderr()); flush(stdout()) > > ### Name: wait > ### Title: Wait for child process(es) to stop or terminate. > ### Aliases: wait > ### Keywords: programming > > ### ** Examples > > waittest <- function() + { + pid = fork(NULL) + if(pid==0) + { + cat("Hi, I'm the child process and I need to explicitly call exit().") + cat("\n\n") + exit() + } + else + { + wait(pid) + cat("Hi, I'm the main process, and I wait()ed until the child process\n") + cat("finished before introducing myself.\n") + } + } > > waittest() Hi, I'm the child process and I need to explicitly call exit(). > > ### Name: wait > ### Title: Wait for child process(es) to stop or terminate. > ### Aliases: wait > ### Keywords: programming > > ### ** Examples > > waittest <- function() + { + pid = fork(NULL) + if(pid==0) + { + cat("Hi, I'm the child process and I need to explicitly call exit().") + cat("\n\n") + exit() + } + else + { + wait(pid) + cat("Hi, I'm the main process, and I wait()ed until the child process\n") + cat("finished before introducing myself.\n") + } + } > > waittest() Hi, I'm the main process, and I wait()ed until the child process finished before introducing myself. > > > > ### *