Access to process dictionary

classic Classic list List threaded Threaded
18 messages Options
Reply | Threaded
Open this post in threaded view
|

Access to process dictionary

Donald Steven
Do two function calls with the same name but one parameter different
(see below) share access to the same process dictionary or does each
have a distinct instance?


Example:


funA(param1, param2) -> do something.

funA(param1, differentparam2) -> do something different.


===


I could just use an if statement within one function call, but I'd like
to know if they share the same process dictionary. Thanks.

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Access to process dictionary

PAILLEAU Eric

Hi,

Process dictionary is a dictionary for a process.

If you call each functions from different processes, they are independents.

From a single process two functions cannot be called at same time.


Envoyé depuis mon mobile



---- Donald Steven a écrit ----

Do two function calls with the same name but one parameter different

(see below) share access to the same process dictionary or does each

have a distinct instance?





Example:





funA(param1, param2) -> do something.



funA(param1, differentparam2) -> do something different.





===





I could just use an if statement within one function call, but I'd like

to know if they share the same process dictionary. Thanks.



_______________________________________________

erlang-questions mailing list

[hidden email]

http://erlang.org/mailman/listinfo/erlang-questions


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Access to process dictionary

Donald Steven
Merci Eric!

On 12/22/2018 8.56 AM, Eric Pailleau wrote:

Hi,

Process dictionary is a dictionary for a process.

If you call each functions from different processes, they are independents.

From a single process two functions cannot be called at same time.


Envoyé depuis mon mobile



---- Donald Steven a écrit ----

Do two function calls with the same name but one parameter different

(see below) share access to the same process dictionary or does each

have a distinct instance?





Example:





funA(param1, param2) -> do something.



funA(param1, differentparam2) -> do something different.





===





I could just use an if statement within one function call, but I'd like

to know if they share the same process dictionary. Thanks.



_______________________________________________

erlang-questions mailing list

[hidden email]

http://erlang.org/mailman/listinfo/erlang-questions



_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Access to process dictionary

PAILLEAU Eric

You should however avoid to use process dictionaries if possible.

It is handy to store things, but generally a sign that your code is missing something.


For instance if you face some processes crashes, your process dictionary will be lost, while using states in OTP libs will be shown in crash report. Easier for debugging.


Regards


Envoyé depuis mon mobile



---- Donald Steven a écrit ----

Merci Eric!

On 12/22/2018 8.56 AM, Eric Pailleau wrote:

Hi,

Process dictionary is a dictionary for a process.

If you call each functions from different processes, they are independents.

From a single process two functions cannot be called at same time.


Envoyé depuis mon mobile



---- Donald Steven a écrit ----

Do two function calls with the same name but one parameter different

(see below) share access to the same process dictionary or does each

have a distinct instance?





Example:





funA(param1, param2) -> do something.



funA(param1, differentparam2) -> do something different.





===





I could just use an if statement within one function call, but I'd like

to know if they share the same process dictionary. Thanks.



_______________________________________________

erlang-questions mailing list

[hidden email]

http://erlang.org/mailman/listinfo/erlang-questions



_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Access to process dictionary

Pierre Fenoll-2
Hi list. 
Am I the only one not seeing the first email of most threads? Or is it my gmail mobile client that does its thing?
Thanks & be well

On Sun 23 Dec 2018 at 10:13, Eric Pailleau <[hidden email]> wrote:

You should however avoid to use process dictionaries if possible.

It is handy to store things, but generally a sign that your code is missing something.


For instance if you face some processes crashes, your process dictionary will be lost, while using states in OTP libs will be shown in crash report. Easier for debugging.


Regards


Envoyé depuis mon mobile



---- Donald Steven a écrit ----

Merci Eric!

On 12/22/2018 8.56 AM, Eric Pailleau wrote:

Hi,

Process dictionary is a dictionary for a process.

If you call each functions from different processes, they are independents.

From a single process two functions cannot be called at same time.


Envoyé depuis mon mobile



---- Donald Steven a écrit ----

Do two function calls with the same name but one parameter different

(see below) share access to the same process dictionary or does each

have a distinct instance?





Example:





funA(param1, param2) -> do something.



funA(param1, differentparam2) -> do something different.





===





I could just use an if statement within one function call, but I'd like

to know if they share the same process dictionary. Thanks.



_______________________________________________

erlang-questions mailing list

[hidden email]

http://erlang.org/mailman/listinfo/erlang-questions


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
--

Cheers,
-- 
Pierre Fenoll


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Access to process dictionary

Loïc Hoguin-3
It's you. :-)

On 23/12/2018 11:15, Pierre Fenoll wrote:

> Hi list.
> Am I the only one not seeing the first email of most threads? Or is it
> my gmail mobile client that does its thing?
> Thanks & be well
>
> On Sun 23 Dec 2018 at 10:13, Eric Pailleau <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     You should however avoid to use process dictionaries if possible.
>
>     It is handy to store things, but generally a sign that your code is
>     missing something.
>
>
>     For instance if you face some processes crashes, your process
>     dictionary will be lost, while using states in OTP libs will be
>     shown in crash report. Easier for debugging.
>
>
>     Regards
>
>
>     Envoyé depuis mon mobile
>
>
>
>     ---- Donald Steven a écrit ----
>
>     Merci Eric!
>
>     On 12/22/2018 8.56 AM, Eric Pailleau wrote:
>>
>>     Hi,
>>
>>     Process dictionary is a dictionary for a process.
>>
>>     If you call each functions from different processes, they are
>>     independents.
>>
>>     From a single process two functions cannot be called at same time.
>>
>>
>>     Envoyé depuis mon mobile
>>
>>
>>
>>     ---- Donald Steven a écrit ----
>>
>>     Do two function calls with the same name but one parameter different
>>
>>     (see below) share access to the same process dictionary or does each
>>
>>     have a distinct instance?
>>
>>
>>
>>
>>
>>     Example:
>>
>>
>>
>>
>>
>>     funA(param1, param2) -> do something.
>>
>>
>>
>>     funA(param1, differentparam2) -> do something different.
>>
>>
>>
>>
>>
>>     ===
>>
>>
>>
>>
>>
>>     I could just use an if statement within one function call, but I'd
>>     like
>>
>>     to know if they share the same process dictionary. Thanks.
>>
>>
>>
>>     _______________________________________________
>>
>>     erlang-questions mailing list
>>
>>     [hidden email] <mailto:[hidden email]>
>>
>>     http://erlang.org/mailman/listinfo/erlang-questions
>>
>
>     _______________________________________________
>     erlang-questions mailing list
>     [hidden email] <mailto:[hidden email]>
>     http://erlang.org/mailman/listinfo/erlang-questions
>
> --
>
> Cheers,
> --
> Pierre Fenoll
>
>
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions
>

--
Loïc Hoguin
https://ninenines.eu
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Access to process dictionary

Igor Clark
I frequently miss emails from some particular people, whether at the start or in the middle of a thread, making it look like threads are missing messages, because my receiving gmail account puts them in spam as it doesn’t like  their email provider’s DKIM headers, or something. Especially when they come from yahoo.com. I go to the spam folder and remove the spam tag and they show up again. Pierre, as you’re on gmail, you may be getting something similar?

> On 23 Dec 2018, at 10:51, Loïc Hoguin <[hidden email]> wrote:
>
> It's you. :-)
>
>> On 23/12/2018 11:15, Pierre Fenoll wrote:
>> Hi list.
>> Am I the only one not seeing the first email of most threads? Or is it my gmail mobile client that does its thing?
>> Thanks & be well
>> On Sun 23 Dec 2018 at 10:13, Eric Pailleau <[hidden email] <mailto:[hidden email]>> wrote:
>>    You should however avoid to use process dictionaries if possible.
>>    It is handy to store things, but generally a sign that your code is
>>    missing something.
>>    For instance if you face some processes crashes, your process
>>    dictionary will be lost, while using states in OTP libs will be
>>    shown in crash report. Easier for debugging.
>>    Regards
>>    Envoyé depuis mon mobile
>>    ---- Donald Steven a écrit ----
>>    Merci Eric!
>>>    On 12/22/2018 8.56 AM, Eric Pailleau wrote:
>>>
>>>    Hi,
>>>
>>>    Process dictionary is a dictionary for a process.
>>>
>>>    If you call each functions from different processes, they are
>>>    independents.
>>>
>>>    From a single process two functions cannot be called at same time.
>>>
>>>
>>>    Envoyé depuis mon mobile
>>>
>>>
>>>
>>>    ---- Donald Steven a écrit ----
>>>
>>>    Do two function calls with the same name but one parameter different
>>>
>>>    (see below) share access to the same process dictionary or does each
>>>
>>>    have a distinct instance?
>>>
>>>
>>>
>>>
>>>
>>>    Example:
>>>
>>>
>>>
>>>
>>>
>>>    funA(param1, param2) -> do something.
>>>
>>>
>>>
>>>    funA(param1, differentparam2) -> do something different.
>>>
>>>
>>>
>>>
>>>
>>>    ===
>>>
>>>
>>>
>>>
>>>
>>>    I could just use an if statement within one function call, but I'd
>>>    like
>>>
>>>    to know if they share the same process dictionary. Thanks.
>>>
>>>
>>>
>>>    _______________________________________________
>>>
>>>    erlang-questions mailing list
>>>
>>>    [hidden email] <mailto:[hidden email]>
>>>
>>>    http://erlang.org/mailman/listinfo/erlang-questions
>>>
>>    _______________________________________________
>>    erlang-questions mailing list
>>    [hidden email] <mailto:[hidden email]>
>>    http://erlang.org/mailman/listinfo/erlang-questions
>> --
>> Cheers,
>> --
>> Pierre Fenoll
>> _______________________________________________
>> erlang-questions mailing list
>> [hidden email]
>> http://erlang.org/mailman/listinfo/erlang-questions
>
> --
> Loïc Hoguin
> https://ninenines.eu
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Access to process dictionary

Vance Shipley
In reply to this post by PAILLEAU Eric
On Sun, Dec 23, 2018 at 2:43 PM Eric Pailleau <[hidden email]> wrote:
> You should however avoid to use process dictionaries if possible.

Yeah, don't do that. If you are new to functional programming and
think the process dictionary is the answer to the question :"where are
the global variables?" you are on the wrong track.
The process dictionary is for OTP and other tools, it's not something
you should be using for general work.

--
     -Vance
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Access to process dictionary

Donald Steven
Vance,


Thanks for your note.  I've heard that injunction before and have been
successful until recently in not using it.  However, I was unable to
solve the following exercise without it.


Just for fun, I wanted to try and develop an Erlang version of getc (the
old Kernighan and Ritchie "Software Tools") so that I could not only
read a character from a binary buffer but 'put back' a character (or
more than one) if I needed to.  Further, instances of this getc function
had to be able to be called from time to time, not
successively-recursively as in while getc /= eof putc, etc.


So I needed to know where in the input buffer I was; that is, to save
the buffer index.  In a procedural language, I'd just have a global
index and increment it after a getc and decrement it after a put back
character.  With a process dictionary I was able to do this.  Without
it, I was stumped.


If you have a non-process-dictionary solution, I'm all ears.


Best,


Don


On 12/23/2018 6:57 AM, Vance Shipley wrote:
> On Sun, Dec 23, 2018 at 2:43 PM Eric Pailleau <[hidden email]> wrote:
>> You should however avoid to use process dictionaries if possible.
> Yeah, don't do that. If you are new to functional programming and
> think the process dictionary is the answer to the question :"where are
> the global variables?" you are on the wrong track.
> The process dictionary is for OTP and other tools, it's not something
> you should be using for general work.
>
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Access to process dictionary

Vance Shipley
On Sun, Dec 23, 2018 at 6:32 PM Donald Steven <[hidden email]> wrote:
> So I needed to know where in the input buffer I was; that is, to save the buffer index.

Sure, we very often have state we need to maintain. In functional
programming you pass in all the information needed for a function to
perform it's work. Here you would pass the buffer index as an argument
to your function.

> If you have a non-process-dictionary solution, I'm all ears.

Look at the OTP kernel application's disk_log: chunk/2 function:
http://erlang.org/doc/man/disk_log.html#chunk-2

It takes 'Log' and  'Continuation' arguments. The first time it's
called you provide the value 'start' for 'Continuation' and the result
is a tuple of '{Contnuation, Terms}' where Continuation is an opaque
value which you use in the next call to chunk/2. The function is
reentrant because the caller is keeping the state.

Where to keep it? Basically you do the same thing, you keep passing it
in an argument to the functions being executed by your process.  Look
at the OTP stdlib application's gen_server behaviour:
http://erlang.org/doc/man/gen_server.html#Module:init-1

Every function includes a 'State' argument which carries around all
your stateful stuff. This is where your "global variable" type stuff
lives.


--
     -Vance
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Access to process dictionary

Attila Rajmund Nohl
In reply to this post by PAILLEAU Eric
Eric Pailleau <[hidden email]> ezt írta (időpont: 2018. dec.
23., V, 10:13):
>
> You should however avoid to use process dictionaries if possible.
>
> It is handy to store things, but generally a sign that your code is missing something.
>
>
> For instance if you face some processes crashes, your process dictionary will be lost, while using states in OTP libs will be shown in crash report. Easier for debugging.

I do see the process dictionary in crash reports (I'm not sure if
lager puts it there or it is there by default). Process dictionary is
useful for read-only data (i.e. not state): put stuff there at process
startup, then don't have to worry about passing yet an other value
around, all function can access that piece of data.
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Access to process dictionary

PAILLEAU Eric

Hi,

Yes you are right. I forgot this.

Anyway process dictionary should be avoid because same dictionary  entry name could be used in different library code called in same process.

Let say you create an entry 'foo' in your code, but you use external code having same idea. Your dictionary can be overwritten. It is not safe.


Envoyé depuis mon mobile



---- Attila Rajmund Nohl a écrit ----

Eric Pailleau <[hidden email]> ezt írta (időpont: 2018. dec.
23., V, 10:13):
>
> You should however avoid to use process dictionaries if possible.
>
> It is handy to store things, but generally a sign that your code is missing something.
>
>
> For instance if you face some processes crashes, your process dictionary will be lost, while using states in OTP libs will be shown in crash report. Easier for debugging.

I do see the process dictionary in crash reports (I'm not sure if
lager puts it there or it is there by default). Process dictionary is
useful for read-only data (i.e. not state): put stuff there at process
startup, then don't have to worry about passing yet an other value
around, all function can access that piece of data.
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Access to process dictionary

zxq9-2
In reply to this post by Donald Steven
On 2018年12月23日日曜日 8時02分56秒 JST Donald Steven wrote:
> So I needed to know where in the input buffer I was; that is, to save
> the buffer index.  In a procedural language, I'd just have a global
> index and increment it after a getc and decrement it after a put back
> character.  With a process dictionary I was able to do this.  Without
> it, I was stumped.
>
>
> If you have a non-process-dictionary solution, I'm all ears.


Consider this simple state-keeping service loop:


-module(simple).
-export([start/1, stop/1]).
-export([add/2, sub/2]).

start(X) ->
  spawn(fun() -> loop(X) end).

add(PID, Amount) ->
    PID ! {add, Amount},
    ok.

sub(PID, Amount) ->
    PID ! {sub, Amount},
    ok.

stop(PID) ->
    PID ! stop,
    ok.

loop(X) ->
    ok = io:format("~p: X is ~p~n", [self(), X]),
    receive
        {add, Y} ->
            NewX = X + Y,
            loop(NewX);
        {sub, Y} ->
            NewX = X - Y,
            loop(NewX);
        stop ->
            ok = io:format("Bye!~n"),
            exit(normal);
        Unexpected ->
            ok = io:format("I don't understand ~tp~n", [Unexpected]),
            loop(X)
    end.


This process spawns with whatever value we give the start/1 function and keeps it in state as a state variable, not in the process dictionary. The "life" of this process is the main service loop, loop/1, and the add/2, sub/2, and stop/1 functions are all just functionally defined interfaces to make interacting with the process sane (blasting naked messages from random bits of code all over a project quickly becomes an exploding complexity problem).

This program just keeps looping, retaining the current value throughout its life until you send it a `stop` message.

Every process is a different little guy, all his own and has no visibility or concern about whatever is going on with other processes. In the use example below I spawn two processes from the simple.erl module pasted above and send them different messages.


1> c(simple).
{ok,simple}
2> P1 = simple:start(0).
<0.72.0>: X is 0
<0.72.0>
3> P2 = simple:start(100).
<0.74.0>: X is 100
<0.74.0>
4> simple:sub(P1, 10).
<0.72.0>: X is -10
ok
5> simple:sub(P2, 10).
<0.74.0>: X is 90
ok
6> simple:add(P1, 1000).
<0.72.0>: X is 990
ok
7> simple:add(P1, 50).
<0.72.0>: X is 1040
ok
8> simple:sub(P2, 1000).
<0.74.0>: X is -910
ok


Think carefully about the above code and why it works the way it does.

For a more fully fleshed-out (but still minimal) OTP example consider the Example Server code.
https://gitlab.com/zxq9/example-server/tree/master

es_client.erl
https://gitlab.com/zxq9/example-server/blob/master/src/es_client.erl
Very similar to the above code -- it is a OTP compliant process that handles a socket and receives messages from other chatters.

es_client_man.erl
https://gitlab.com/zxq9/example-server/blob/master/src/es_client_man.erl
Also similar to the above code, but is written as a gen_server for convenience. You don't see the loop/3 function -- it is hidden higher up in gen_server code -- what you do see are the callback functions that handle incoming messages: handle_call/3, handle_cast/2, and handle_info/2.

I recommend playing around with the simple.erl a bit and rewriting it to do whatever you want first.
Then clone the Example Server repository (it can be launched with the ./start script, open a telnet port with es:listen(PortNumber) is how you make it listen) and edit that to make it do whatever you want on the network.

With Erlang you just have to write a bunch of little programs and mess around editing existing programs until it all suddenly clicks!


-Craig
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Access to process dictionary

Attila Rajmund Nohl
In reply to this post by PAILLEAU Eric
The same is true for e.g. public named ETS tables or even for modules. If a different library uses the same name, it's a problem.

2018. dec. 24., Hét 0:10 dátummal Eric Pailleau <[hidden email]> ezt írta:

Hi,

Yes you are right. I forgot this.

Anyway process dictionary should be avoid because same dictionary  entry name could be used in different library code called in same process.

Let say you create an entry 'foo' in your code, but you use external code having same idea. Your dictionary can be overwritten. It is not safe.


Envoyé depuis mon mobile



---- Attila Rajmund Nohl a écrit ----

Eric Pailleau <[hidden email]> ezt írta (időpont: 2018. dec.
23., V, 10:13):
>
> You should however avoid to use process dictionaries if possible.
>
> It is handy to store things, but generally a sign that your code is missing something.
>
>
> For instance if you face some processes crashes, your process dictionary will be lost, while using states in OTP libs will be shown in crash report. Easier for debugging.

I do see the process dictionary in crash reports (I'm not sure if
lager puts it there or it is there by default). Process dictionary is
useful for read-only data (i.e. not state): put stuff there at process
startup, then don't have to worry about passing yet an other value
around, all function can access that piece of data.
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Access to process dictionary

PAILLEAU Eric

Yeah,

A common workaround is to prefix all your entries with the name of your module, but it is not a full guarantee.


'Use the state, Luke... '


Envoyé depuis mon mobile



---- Attila Rajmund Nohl a écrit ----

The same is true for e.g. public named ETS tables or even for modules. If a different library uses the same name, it's a problem.

2018. dec. 24., Hét 0:10 dátummal Eric Pailleau <[hidden email]> ezt írta:

Hi,

Yes you are right. I forgot this.

Anyway process dictionary should be avoid because same dictionary  entry name could be used in different library code called in same process.

Let say you create an entry 'foo' in your code, but you use external code having same idea. Your dictionary can be overwritten. It is not safe.


Envoyé depuis mon mobile



---- Attila Rajmund Nohl a écrit ----

Eric Pailleau <[hidden email]> ezt írta (időpont: 2018. dec.
23., V, 10:13):
>
> You should however avoid to use process dictionaries if possible.
>
> It is handy to store things, but generally a sign that your code is missing something.
>
>
> For instance if you face some processes crashes, your process dictionary will be lost, while using states in OTP libs will be shown in crash report. Easier for debugging.

I do see the process dictionary in crash reports (I'm not sure if
lager puts it there or it is there by default). Process dictionary is
useful for read-only data (i.e. not state): put stuff there at process
startup, then don't have to worry about passing yet an other value
around, all function can access that piece of data.
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Access to process dictionary

Benoit Chesneau-2
In reply to this post by Pierre Fenoll-2
nope i got that too... and recently received a probe test as well. Not sure what's the issue.

Benoit

On Sun, Dec 23, 2018 at 11:15 AM Pierre Fenoll <[hidden email]> wrote:
Hi list. 
Am I the only one not seeing the first email of most threads? Or is it my gmail mobile client that does its thing?
Thanks & be well

On Sun 23 Dec 2018 at 10:13, Eric Pailleau <[hidden email]> wrote:

You should however avoid to use process dictionaries if possible.

It is handy to store things, but generally a sign that your code is missing something.


For instance if you face some processes crashes, your process dictionary will be lost, while using states in OTP libs will be shown in crash report. Easier for debugging.


Regards


Envoyé depuis mon mobile



---- Donald Steven a écrit ----

Merci Eric!

On 12/22/2018 8.56 AM, Eric Pailleau wrote:

Hi,

Process dictionary is a dictionary for a process.

If you call each functions from different processes, they are independents.

From a single process two functions cannot be called at same time.


Envoyé depuis mon mobile



---- Donald Steven a écrit ----

Do two function calls with the same name but one parameter different

(see below) share access to the same process dictionary or does each

have a distinct instance?





Example:





funA(param1, param2) -> do something.



funA(param1, differentparam2) -> do something different.





===





I could just use an if statement within one function call, but I'd like

to know if they share the same process dictionary. Thanks.



_______________________________________________

erlang-questions mailing list

[hidden email]

http://erlang.org/mailman/listinfo/erlang-questions


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
--

Cheers,
-- 
Pierre Fenoll

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Access to process dictionary

Donald Steven
In reply to this post by zxq9-2
Hi Craig,

Thanks so much!!

I usually work to compile programs and when I tried this based on your code (see source below), it almost works.  I must be making a simple newbie mistake.

The output I get is:

<0.58.0>: X is 1000
I don't understand {<0.5.0>,{add,1}}
<0.58.0>: X is 1000

============

Here's the code:

-module(simple).
-export([main/0]).
-export([start/1, stop/1]).
-export([add/2, sub/2]).
main() ->
Pid = start(1000),
Pid ! {self(), {add, 1}}.
%-----------------------------------------------------------------------------------
start(X) ->
Pid = spawn(fun() -> loop(X) end),
Pid.
%-----------------------------------------------------------------------------------
loop(X) ->
ok = io:format("~p: X is ~p~n", [self(), X]),
receive
{add, Y} ->
NewX = X + Y,
loop(NewX);
{sub, Y} ->
NewX = X - Y,
loop(NewX);
stop ->
ok = io:format("Bye!~n"),
exit(normal);
Unexpected ->
ok = io:format("I don't understand ~tp~n", [Unexpected]),
loop(X)
end.
%-----------------------------------------------------------------------------------
add(PID, Amount) ->
PID ! {add, Amount},
ok.
sub(PID, Amount) ->
PID ! {sub, Amount},
ok.
stop(PID) ->
PID ! stop,
ok.
%-----------------------------------------------------------------------------------


On 12/23/2018 9.24 PM, [hidden email] wrote:
-module(simple).
-export([start/1, stop/1]).
-export([add/2, sub/2]).

start(X) ->
  spawn(fun() -> loop(X) end).

add(PID, Amount) ->
    PID ! {add, Amount},
    ok.

sub(PID, Amount) ->
    PID ! {sub, Amount},
    ok.

stop(PID) ->
    PID ! stop,
    ok.

loop(X) ->
    ok = io:format("~p: X is ~p~n", [self(), X]),
    receive
        {add, Y} ->
            NewX = X + Y,
            loop(NewX);
        {sub, Y} ->
            NewX = X - Y,
            loop(NewX);
        stop ->
            ok = io:format("Bye!~n"),
            exit(normal);
        Unexpected ->
            ok = io:format("I don't understand ~tp~n", [Unexpected]),
            loop(X)
    end.


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Access to process dictionary

Donald Steven
Yes!!  Thanks Taavi. I'll have to study why the first version didn't fly.

On 12/30/2018 1.48 PM, [hidden email] wrote:
>    Pid = start(1000),
>    Pid ! {add, 1}.

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions