Thread: [R-gregmisc-users] SF.net SVN: r-gregmisc: [1043] trunk/fork
Brought to you by:
warnes
From: <wa...@us...> - 2007-02-05 23:10:39
|
Revision: 1043 http://svn.sourceforge.net/r-gregmisc/?rev=1043&view=rev Author: warnes Date: 2007-02-05 15:08:43 -0800 (Mon, 05 Feb 2007) Log Message: ----------- Add signal() command to allow ignoring signals Modified Paths: -------------- trunk/fork/DESCRIPTION trunk/fork/R/signame.R trunk/fork/man/fork.Rd trunk/fork/man/signame.Rd trunk/fork/src/fork.c Added Paths: ----------- trunk/fork/R/signal.R trunk/fork/man/signal.Rd Modified: trunk/fork/DESCRIPTION =================================================================== --- trunk/fork/DESCRIPTION 2007-01-28 10:42:24 UTC (rev 1042) +++ trunk/fork/DESCRIPTION 2007-02-05 23:08:43 UTC (rev 1043) @@ -1,6 +1,6 @@ Package: fork Title: R functions for handling multiple processes. -Version: 1.0.2 +Version: 1.0.3 Author: Gregory R. Warnes <wa...@bs...> Description: These functions provides simple wrappers around the Unix process management API calls: fork, wait, waitpid, kill, and _exit. This Added: trunk/fork/R/signal.R =================================================================== --- trunk/fork/R/signal.R (rev 0) +++ trunk/fork/R/signal.R 2007-02-05 23:08:43 UTC (rev 1043) @@ -0,0 +1,30 @@ +# $Id: kill.R 340 2004-05-25 19:12:32Z warnes $ + +signal <- function(signal, action=c("ignore","restore") ) + { + action=match.args(action) + + if(is.character(signal)) + sig=sigval(signal)$val + else (is.numeric(signal) && !is.na(signal)) + sig=signal + else + stop("Illegal value for signal") + + act <- switch( + action, + "ignore"=0, + "restore"=1 + ) + + msg1 = + + .C( + "Rfork_signal", + as.integer(signal), + as.integer(act), + PACKAGE="fork" + ) + invisible() + } + Modified: trunk/fork/R/signame.R =================================================================== --- trunk/fork/R/signame.R 2007-01-28 10:42:24 UTC (rev 1042) +++ trunk/fork/R/signame.R 2007-02-05 23:08:43 UTC (rev 1043) @@ -20,7 +20,8 @@ val=integer(1), desc=character(1), PACKAGE="fork") - unlist(retval) + #retval <- unlist(retval) + retval } siglist <- function() Modified: trunk/fork/man/fork.Rd =================================================================== --- trunk/fork/man/fork.Rd 2007-01-28 10:42:24 UTC (rev 1042) +++ trunk/fork/man/fork.Rd 2007-02-05 23:08:43 UTC (rev 1043) @@ -162,8 +162,15 @@ close(con2) } +# Important: if we aren't going to explicitly wait() for the child +# process to exit, we need to tell the OS that we are ignoring child +# process signals so it will let us die cleanly. +signal("SIGCHLD","ignore") +# fork off the process pid <- fork(recieve) + +# start sending... send() } } Added: trunk/fork/man/signal.Rd =================================================================== --- trunk/fork/man/signal.Rd (rev 0) +++ trunk/fork/man/signal.Rd 2007-02-05 23:08:43 UTC (rev 1043) @@ -0,0 +1,49 @@ +\name{signal} +\alias{signal} +\title{Enable or disable handling of signals} +\description{ + This function allows enabling or disabling handling of the specified signal. +} +\usage{ +signal(signal, action = c("ignore", "default")) +} +\arguments{ + \item{signal}{Signal handler to manipulate, either as a numeric id or + character mnemonic} + \item{action}{Either "ignore" or "default"} +} +\details{ + It is occasionally necessary to instruct the R process to ignore + certain signals. This function allows changing the status of a signal + to either igore the signal (SIG_IGN="ignore") or to the OS's default + handler (SIG_DFL="default") +} +\value{ + Nothing of interest. +} +\references{ See the unix man page for "signal" for more information} +\author{Gregory R. Warnes \email{gre...@ro...} } +\note{ + Be very careful with this function. It can be used to totally + confuse the R process. +} +\seealso{ \code{\link{sigval}}, \code{\link{fork}} } +\examples{ + +# Ignore child termination signals for forked processes +signal("SIGCHLD","ignore") + +# Fork off a child process to say "Hi!". + { + pid = fork(slave=NULL) + if(pid==0) { + # only runs in the child process + cat("Hi from the child process!\n"); + exit() # force process to die + } + } + +} +% Add one or more standard keywords, see file 'KEYWORDS' in the +% R documentation directory. +\keyword{programming} Modified: trunk/fork/man/signame.Rd =================================================================== --- trunk/fork/man/signame.Rd 2007-01-28 10:42:24 UTC (rev 1042) +++ trunk/fork/man/signame.Rd 2007-02-05 23:08:43 UTC (rev 1043) @@ -22,7 +22,7 @@ compile time. } \value{ - A vector (\code{signame}, \code{sigval}) or data frame + A list (\code{signame}, \code{sigval}) or data frame (\code{siglist}) containing the components: \item{name}{Symbolic name} \item{val}{Integer value} Modified: trunk/fork/src/fork.c =================================================================== --- trunk/fork/src/fork.c 2007-01-28 10:42:24 UTC (rev 1042) +++ trunk/fork/src/fork.c 2007-02-05 23:08:43 UTC (rev 1043) @@ -266,3 +266,15 @@ return list; } +void Rfork_signal(int *sig, int *action) +{ + sig_t func; + + if(*action==0) + func = SIG_IGN; + else + func = SIG_DFL; + + signal( *sig, func ); +} + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <wa...@us...> - 2007-02-14 22:49:26
|
Revision: 1053 http://svn.sourceforge.net/r-gregmisc/?rev=1053&view=rev Author: warnes Date: 2007-02-14 14:48:47 -0800 (Wed, 14 Feb 2007) Log Message: ----------- Add code to install SIGCHLD handler to avoid forked children from becoming zombies Added Paths: ----------- trunk/fork/src/sigchld_handler.c trunk/fork/tests/ trunk/fork/tests/test_many_fork.R Added: trunk/fork/src/sigchld_handler.c =================================================================== --- trunk/fork/src/sigchld_handler.c (rev 0) +++ trunk/fork/src/sigchld_handler.c 2007-02-14 22:48:47 UTC (rev 1053) @@ -0,0 +1,73 @@ +#include <R.h> +#include <Rdefines.h> + +/* + Code taken from a posting to the Gimp-developer mailing list by + Raphael Quinet quinet at gamers.org accessible at: + http://lists.xcf.berkeley.edu/lists/gimp-developer/2000-November/013572.html +*/ + +#include <signal.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <unistd.h> + +int installed=0; // Has our sigcld signal hander already been installed? + +struct sigaction sa ; // our sigcld signal handler +struct sigaction osa; // original (R) signal hander + +void sigchld_hander(int dummy) +{ + int st; + while (wait3(&st, WNOHANG, NULL) > 0); +} + + +void R_install_sigcld_handler() +{ + int ret; + + if(installed==0) + { + Rprintf ("Installing SIGCHLD signal handler..."); + //sa.sa_handler = sigchld_handler; + sa.sa_handler = waiter; + //sigfillset (&sa.sa_mask); + //sa.sa_flags = SA_RESTART; + ret = sigaction (SIGCHLD, &sa, &osa); + if (ret < 0) + { + error("Cannot set signal handler"); + } + installed=-1; + Rprintf("Done.\n"); + } + else + { + warning("SIGCLD signal handler already installed"); + } + +} + +void R_restore_sigcld_handler() +{ + int ret; + + if(installed==-1) + { + Rprintf ("Restoring original SIGCHLD signal handler..."); + ret = sigaction (SIGCHLD, &osa, &sa); + if (ret < 0) + { + error("Cannot reset signal handler"); + } + installed=0; + Rprintf("Done.\n"); + } + else + { + warning("SIGCLD signal handler not installed: cannot reset."); + } + +} Added: trunk/fork/tests/test_many_fork.R =================================================================== --- trunk/fork/tests/test_many_fork.R (rev 0) +++ trunk/fork/tests/test_many_fork.R 2007-02-14 22:48:47 UTC (rev 1053) @@ -0,0 +1,20 @@ +library(fork) + +# ignore sigchld signals so child processes will die cleanly +#signal("SIGCHLD","ignore") + +# start signal handler +.C("R_install_sigcld_handler") + +for(i in 1:100) + { + pid = fork(slave=NULL) + if(pid==0) { + cat("Hi from the child process\n"); exit() + } else { + cat("Hi from the parent process\n"); + } + } + +# remove signal handler +.C("R_restore_sigcld_handler") This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <wa...@us...> - 2007-03-24 21:30:28
|
Revision: 1085 http://svn.sourceforge.net/r-gregmisc/?rev=1085&view=rev Author: warnes Date: 2007-03-24 14:30:26 -0700 (Sat, 24 Mar 2007) Log Message: ----------- Update my email address Modified Paths: -------------- trunk/fork/DESCRIPTION trunk/fork/man/exit.Rd trunk/fork/man/fork.Rd trunk/fork/man/getpid.Rd trunk/fork/man/kill.Rd trunk/fork/man/signame.Rd Modified: trunk/fork/DESCRIPTION =================================================================== --- trunk/fork/DESCRIPTION 2007-03-24 21:27:27 UTC (rev 1084) +++ trunk/fork/DESCRIPTION 2007-03-24 21:30:26 UTC (rev 1085) @@ -2,10 +2,13 @@ Title: R functions for handling multiple processes. Version: 1.2.0 Date: 2007-03-22 -Author: Gregory R. Warnes <wa...@bs...> +Author: Gregory R. Warnes <gr...@ra...>. Financial + support for some aspects of this package provided by Metrum Research Group, + LLC <http://www.metrumrg.com>. Description: These functions provides simple wrappers around the Unix process management API calls: fork, signal, wait, waitpid, kill, and _exit. This enables construction of programs that utilize and mange multiple concurrent - processes. -Maintainer: Gregory R. Warnes <wa...@bs...> + processes. Commercial support for this package available from + Random Technologies, LLC <http://www.random-technologies-llc.com> +Maintainer: Gregory R. Warnes <gr...@ra...> License: GPL Modified: trunk/fork/man/exit.Rd =================================================================== --- trunk/fork/man/exit.Rd 2007-03-24 21:27:27 UTC (rev 1084) +++ trunk/fork/man/exit.Rd 2007-03-24 21:30:26 UTC (rev 1085) @@ -25,7 +25,7 @@ argument, so it is usually not necessary to directly call exit(). } \references{"\_exit" man page} -\author{ Gregory R. Warnes \email{wa...@bs...}} +\author{ Gregory R. Warnes \email{gr...@ra...}} \seealso{\code{\link{fork}}, \code{\link{getpid}}, \code{\link{wait}}, \code{\link{kill}}, \code{\link{killall}} } \examples{ Modified: trunk/fork/man/fork.Rd =================================================================== --- trunk/fork/man/fork.Rd 2007-03-24 21:27:27 UTC (rev 1084) +++ trunk/fork/man/fork.Rd 2007-03-24 21:30:26 UTC (rev 1085) @@ -88,7 +88,7 @@ function returns 0 to the child process. } \references{'fork' man page} -\author{ Gregory R. Warnes \email{wa...@bs...}} +\author{ Gregory R. Warnes \email{gr...@ra...}} \seealso{\code{\link{getpid}}, \code{\link{exit}}, \code{\link{wait}}, \code{\link{kill}}, \code{\link{killall}} } \examples{ Modified: trunk/fork/man/getpid.Rd =================================================================== --- trunk/fork/man/getpid.Rd 2007-03-24 21:27:27 UTC (rev 1084) +++ trunk/fork/man/getpid.Rd 2007-03-24 21:30:26 UTC (rev 1085) @@ -15,7 +15,7 @@ Integer process id. } \references{Unix "getpid" man page} -\author{ Gregory R. Warnes \email{wa...@bs...}} +\author{ Gregory R. Warnes \email{gr...@ra...}} \seealso{\code{\link{fork}}, \code{\link{exit}}, \code{\link{wait}}, \code{\link{kill}}, \code{\link{killall}} } \examples{ Modified: trunk/fork/man/kill.Rd =================================================================== --- trunk/fork/man/kill.Rd 2007-03-24 21:27:27 UTC (rev 1084) +++ trunk/fork/man/kill.Rd 2007-03-24 21:30:26 UTC (rev 1085) @@ -28,7 +28,7 @@ \code{killall} does not return a value. } \references{"kill" and "waitpid" man pages} -\author{ Gregory R. Warnes \email{wa...@bs...}} +\author{ Gregory R. Warnes \email{gr...@ra...}} \seealso{\code{\link{getpid}}, \code{\link{exit}}, \code{\link{wait}}, \code{\link{kill}}, \code{\link{killall}} } \examples{ Modified: trunk/fork/man/signame.Rd =================================================================== --- trunk/fork/man/signame.Rd 2007-03-24 21:27:27 UTC (rev 1084) +++ trunk/fork/man/signame.Rd 2007-03-24 21:30:26 UTC (rev 1085) @@ -29,7 +29,7 @@ \item{desc}{Description} } \references{Unix "signal" man page.} -\author{ Gregory R. Warnes \email{wa...@bs...}} +\author{ Gregory R. Warnes \email{gr...@ra...}} \seealso{\code{\link{getpid}}, \code{\link{exit}}, \code{\link{wait}}, \code{\link{kill}}, \code{\link{killall}} } \examples{ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |