ets weirdness when using -run

Raimo Niskanen <>
Wed Jun 19 10:46:06 CEST 2002


Hi Chris.

The -run argument spawns a new process that calls eg:start([Key]). When
that function returns, the process dies, and with it the ets table. Try
typing process_info(pid(0,1,0)) in the shell after your test has failed,
to verify that the table owner has died. 

When you run it from the erlang shell, the shell process becomes the
owner, and stays alive after eg:start("Key").

/ Raimo Niskanen, Erlang/OTP, Ericsson AB



Chris Pressey wrote:
> 
> I have a program that uses a public, named ets table like so:
> 
> -module(eg).
> -export([start/1, new/1]).
> 
> start(Key) ->
>   ets:new(my_ets, [public, named_table]),
>   io:fwrite("~p~n", [ets:info(my_ets)]),
>   start_new(lists:flatten(Key)).
> 
> start_new(Key) ->
>   spawn(?MODULE, new, [Key]).
> 
> new(Key) ->
>   io:fwrite("~p~n", [ets:info(my_ets)]),
>   case ets:lookup(my_ets, Key) of
>     [_] ->
>        io:fwrite("Already in database~n");
>     _ ->
>        io:fwrite("Not yet in database~n")
>   end.
> 
> Here is what happens when I run it from the Erlang shell:
> 
> kallisti# erl
> Erlang (BEAM) emulator version 5.1.1 [source]
> 
> Eshell V5.1.1  (abort with ^G)
> 1> eg:start("Key").
> {{memory,276},
>  {owner,<0.23.0>},
>  {name,my_ets},
>  {size,0},
>  {},
>  {named_table,true},
>  {type,set},
>  {keypos,1},
>  {protection,public}}
> {{memory,276},
>  {owner,<0.23.0>},
>  {name,my_ets},
>  {size,0},
>  {},
>  {named_table,true},
>  {type,set},
>  {keypos,1},
>  {protection,public}}
> <0.30.0>
> Not yet in database
> 2>
> 
> But here is what happens when I run it from the command line:
> 
> kallisti# erl -run eg start Key
> Erlang (BEAM) emulator version 5.1.1 [source]
> 
> Eshell V5.1.1  (abort with ^G)
> 1> {{memory,276},
>  {owner,<0.1.0>},
>  {name,my_ets},
>  {size,0},
>  {},
>  {named_table,true},
>  {type,set},
>  {keypos,1},
>  {protection,public}}
> undefined
> 
> =ERROR REPORT==== 18-Jun-2002::16:22:00 ===
> Error in process <0.30.0> with exit value:
> {badarg,[{ets,lookup,[my_ets,"Key"]},{eg,new,1}]}
> 
> Any thoughts on what I might be doing wrong to make my_ets just vanish
> like that?
> 
> -Chris



More information about the erlang-questions mailing list