From: John Carter Date: 2008-03-27T10:50:21+09:00 Subject: Re: "loop do IO.select([io], nil, nil)" eats 95% of CPU,any other way? --Boundary_(ID_7jYW9YVuvyxVA4b4l+OBuQ) Content-type: TEXT/PLAIN; format=flowed; charset=X-UNKNOWN Content-transfer-encoding: QUOTED-PRINTABLE On Thu, 27 Mar 2008, I=C3=B1aki Baz Castillo wrote: > ... > sigprocmask(SIG_BLOCK, NULL, []) =3D 0 > sigprocmask(SIG_BLOCK, NULL, []) =3D 0 > sigprocmask(SIG_BLOCK, NULL, []) =3D 0 > sigprocmask(SIG_BLOCK, NULL, []) =3D 0 > sigprocmask(SIG_BLOCK, NULL, []) =3D 0 > select(4, [3], [], [], {0, 0}) =3D 0 (Timeout) <--- > sigprocmask(SIG_BLOCK, NULL, []) =3D 0 > sigprocmask(SIG_BLOCK, NULL, []) =3D 0 > sigprocmask(SIG_BLOCK, NULL, []) =3D 0 > sigprocmask(SIG_BLOCK, NULL, []) =3D 0 > sigprocmask(SIG_BLOCK, NULL, []) =3D 0 My guess is if you include the read statements you get something like= this... read(3, "", 1000) =3D 0 Which means (according to "man 2 read") On success, the number of bytes read is returned (zero indicates e= nd of file) If you read "man select_tut" 9. If the functions read(2), recv(2), write(2), and send(= 2) fail with errors other than those listed in 7., or one of the input functions returns 0= , indicating end of file, then you should not pass that descriptor to select() again. In the ab= ove example, I close the descriptor imme=E2=80=90 diately, and then set it to -1 to prevent it being inc= luded in a set. Thus... mkfifo foofi ruby -w test.rb& echo bah > foofi =3D=3Dtest.rb=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D loop do begin open( "foofi") do |f| loop do p select([f],nil,nil,500000) p f.sysread(1000) end end rescue EOFError =3D> details p details end end =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D Works as expected... But... =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D open( "foofi") do |f| loop do p select([f],nil,nil,500000) p f.sysread(1000) end end =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D Bombs out with EOFError but curiously =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D open( "foofi") do |f| loop do p select([f],nil,nil,500000) p f.read(1000) end end =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D Exhibits exactly the behaviour you describe. Looking at the strace.... open("foofi", O_RDONLY|O_LARGEFILE) =3D 9 select(10, [9], NULL, NULL, {500000, 0}) =3D 1 (in [9], left {500000,= 0}) fstat64(1, {st_dev=3Dmakedev(0, 11), st_ino=3D37, st_mode=3DS_IFCHR|0= 620, st_nlink=3D1, st_uid=3D1001, st_gid=3D5, st_blksize=3D1024, st_b= locks=3D0, st_rdev=3Dmakedev(136, 35), st_atime=3D2008/03/27-14:45:52= , st_mtime=3D2008/03/27-14:45:52, st_ctime=3D2008/03/27-14:45:52}) = =3D 0 mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1= , 0) =3D 0xb7db2000 write(1, "[[#], [], []]\n", 26) =3D 26 fstat64(9, {st_dev=3Dmakedev(8, 3), st_ino=3D2375703, st_mode=3DS_IFI= FO|0644, st_nlink=3D1, st_uid=3D1001, st_gid=3D65534, st_blksize=3D40= 96, st_blocks=3D0, st_size=3D0, st_atime=3D2008/03/27-14:43:56, st_mt= ime=3D2008/03/27-14:45:54, st_ctime=3D2008/03/27-14:45:54}) =3D 0 mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1= , 0) =3D 0xb7db1000 read(9, "bah\n", 4096) =3D 4 read(9, "", 4096) =3D 0 write(1, "\"bah\\n\"\n", 8) =3D 8 select(10, [9], NULL, NULL, {500000, 0}) =3D 1 (in [9], left {500000,= 0}) write(1, "[[#], [], []]\n", 26) =3D 26 write(1, "nil\n", 4) =3D 4 select(10, [9], NULL, NULL, {500000, 0}) =3D 1 (in [9], left {500000,= 0}) write(1, "[[#], [], []]\n", 26) =3D 26 write(1, "nil\n", 4) =3D 4 The curiosity is why doesn't the f.read throw an EOFError According to ri IO#read... At end of file, it returns nil or "" depend on length. ios.read() and ios.read(nil) returns "". ios.read(positive-integer) returns nil. So it all behaves according to plan... just not your plan... :-)) John Carter Phone : (64)(3) 358 6639 Tait Electronics Fax : (64)(3) 359 4632 PO Box 1645 Christchurch Email : john.carter@tait.co.n= z New Zealand --Boundary_(ID_7jYW9YVuvyxVA4b4l+OBuQ)--