[erlang-questions] who owns an open file

Mats Cronqvist mats.cronqvist@REDACTED
Thu Mar 22 09:50:27 CET 2007


Ulf Wiger (TN/EAB) wrote:
> I had reason to debug a system that ran out of file descriptors. It was
> easy enough to verify that the list of ports kept growing, and to
> extract the new ports in each poll iteration. More difficult -
> surprisingly - to figure out who owned the open files.
> 
> erlang:port_info(P) will give the pid of the controlling process. In
> this case, it's a file_io_server. It will be linked to the process which
> spawned it - this is always file_server. But file_server has no info of
> who ordered the opening of the file. The file_io_server does, but keeps
> it in a state variable, and doesn't respond to e.g. sys:get_status(Pid).
> 
> Finally, I wrote the following hacks in the shell:
> 
> 1> Owner = fun(P) -> {backtrace,Bin} = process_info(P,backtrace), Str =
> binary_to_list(Bin), {match,Start,Len} =
> regexp:match(Str,"#Port<.*>,.*}},"), [PidS|_] =
> string:tokens(string:sub_string(Str,Start+Len),"<>"),
> list_to_pid("<"++PidS++">") end.
> 
> #Fun<erl_eval.6.56006484>
> 2> {ok,Fd} = file:open("foo.txt",[write]) <file:open(> . 
> {ok,<0.80.0>}
> 3> Owner(Fd).
> <0.70.0>                                                             
> 4> self().
> <0.70.0>
> 5> Filename = ets:lookup_element(file_io_servers,Fd,2).     
> "foo.txt" 
> 
> 
> There should be a better way.
> 

   trying to outdo me in the Horrible One-Liner Competition? Hah!

 
[{process_info(Pid,monitors),ets:lookup(file_io_servers,Pid)}||[{name,"efile"},{links,[Pid]}|_]<-[erlang:port_info(P) 
|| P <- erlang:ports()]].

   mats



More information about the erlang-questions mailing list