From: Daniel Berger Date: 2004-03-10T06:18:49+09:00 Subject: process.c changes All, I stumbled across Curt Tilmes' Proc::Wait3 Perl module and it made me think that Ruby (and probably Perl) should have this built-in. I whipped this up quick and it seems to work alright. I felt a struct was nicer than returning a huge array (as Chris does), but that can be changed easily enough. These two functions could also be reduced to one function (with a few more "if" statements) if you really wanted. If this patch is accepted, then I would also suggest deprecating wait2/waitpid2, since they don't provide any information that wait3/waitpid3 don't provide (and the latter provide much more). Regards, Daniel Berger = SAMPLE SCRIPTS = # test_wait3.rb fork{ sleep 5 exit 1 } #p Process.wait3(Process::WNOHANG) p Process.wait3 # test_waitpid3.rb pid = fork{ sleep 3 } p Time.now p Process.waitpid3(pid,Process::WNOHANG) p Time.now p Process.waitpid3(pid) p Time.now = DIFF FILE = --- process.c Tue Mar 9 14:00:09 2004 +++ ../ruby-1.8.1/process.c Wed Dec 10 00:57:06 2003 @@ -456,105 +456,6 @@ return rb_assoc_new(pid, rb_last_status); } -static VALUE proc_wait3(int argc, VALUE *argv){ - int status; - int flags = 0; - struct rusage r; - pid_t pid; - VALUE rbFlags = Qnil; - VALUE rbStruct = rb_struct_define("ProcStruct","pid","status","utime","stime", - "maxrss","minflt","majflt","nswap","inblock","oublock","msgsnd", - "msgrcv","nsignals","nvcsw","nivcsw",0 - ); - - rb_scan_args(argc,argv,"01",&rbFlags); - - if(Qnil != rbFlags){ - flags = NUM2INT(rbFlags); - } - - pid = wait3(&status, flags, &r); - if(pid < 0){ - rb_sys_fail(0); - } - else if(pid > 0){ - return rb_struct_new(rbStruct, - INT2NUM(pid), - INT2NUM(status), - INT2NUM(r.ru_utime.tv_sec + (r.ru_utime.tv_usec/1000.0)), - INT2NUM(r.ru_stime.tv_sec + (r.ru_stime.tv_usec/1000.0)), - INT2NUM(r.ru_maxrss), - INT2NUM(r.ru_ixrss), - INT2NUM(r.ru_idrss), - INT2NUM(r.ru_isrss), - INT2NUM(r.ru_minflt), - INT2NUM(r.ru_majflt), - INT2NUM(r.ru_nswap), - INT2NUM(r.ru_inblock), - INT2NUM(r.ru_oublock), - INT2NUM(r.ru_msgsnd), - INT2NUM(r.ru_msgrcv), - INT2NUM(r.ru_nsignals), - INT2NUM(r.ru_nvcsw), - INT2NUM(r.ru_nivcsw) - ); - } - else{ - return rb_last_status = Qnil; - } -} - -/* wait4 is actually waitpid3 - who comes up with this? */ -static VALUE proc_waitpid3(int argc, VALUE *argv){ - int status; - int flags = 0; - struct rusage r; - pid_t pid; - VALUE rbPid; - VALUE rbFlags = Qnil; - VALUE rbStruct = rb_struct_define("ProcStruct","pid","status","utime","stime", - "maxrss","minflt","majflt","nswap","inblock","oublock","msgsnd", - "msgrcv","nsignals","nvcsw","nivcsw",0 - ); - - rb_scan_args(argc,argv,"11",&rbPid,&rbFlags); - pid = NUM2INT(rbPid); - - if(Qnil != rbFlags){ - flags = NUM2INT(rbFlags); - } - - pid = wait4(pid, &status, flags, &r); - if(pid < 0){ - rb_sys_fail(0); - } - else if(pid > 0){ - return rb_struct_new(rbStruct, - INT2NUM(pid), - INT2NUM(status), - INT2NUM(r.ru_utime.tv_sec + (r.ru_utime.tv_usec/1000.0)), - INT2NUM(r.ru_stime.tv_sec + (r.ru_stime.tv_usec/1000.0)), - INT2NUM(r.ru_maxrss), - INT2NUM(r.ru_ixrss), - INT2NUM(r.ru_idrss), - INT2NUM(r.ru_isrss), - INT2NUM(r.ru_minflt), - INT2NUM(r.ru_majflt), - INT2NUM(r.ru_nswap), - INT2NUM(r.ru_inblock), - INT2NUM(r.ru_oublock), - INT2NUM(r.ru_msgsnd), - INT2NUM(r.ru_msgrcv), - INT2NUM(r.ru_nsignals), - INT2NUM(r.ru_nvcsw), - INT2NUM(r.ru_nivcsw) - ); - } - else{ - return rb_last_status = Qnil; - } -} - static VALUE proc_waitall() { @@ -2436,10 +2337,8 @@ rb_define_module_function(rb_mProcess, "kill", rb_f_kill, -1); rb_define_module_function(rb_mProcess, "wait", proc_wait, -1); rb_define_module_function(rb_mProcess, "wait2", proc_wait2, -1); - rb_define_module_function(rb_mProcess, "wait3", proc_wait3, -1); rb_define_module_function(rb_mProcess, "waitpid", proc_wait, -1); rb_define_module_function(rb_mProcess, "waitpid2", proc_wait2, -1); - rb_define_module_function(rb_mProcess, "waitpid3", proc_waitpid3, -1); rb_define_module_function(rb_mProcess, "waitall", proc_waitall, 0); rb_define_module_function(rb_mProcess, "detach", proc_detach, 1);