I'm new to Erlang - can anyone help me with concurrency and message passing?

26 views
Skip to first unread message

Jason Schvach

unread,
Apr 2, 2017, 12:29:36 PM4/2/17
to Erlang Questions
Hi all! I'm brand new to erlang! 2 weeks to be exact (and also new to programming). I'm curious if anyone can help me out. I'm currently working on some coding while I'm learning about concurrency and message passing? I'm assuming its basic concepts. So I'm trying to convert a sequential code into concurrent. I takes a txt file and splits it into "chunks", counts how many of each characters are in those "chunks", then joins them.

I've made it concurrent by having each "chunk" spawned to a new process to be counted, which works fine. But now I'm having trouble trying to get the results of each of those processes, sent to a single process, and then tho be joined. If anyone can help, please message me.

7stud

unread,
May 20, 2017, 7:44:02 AM5/20/17
to Erlang Questions
Have all the workers send a message to the parent process that spawned them.  In order for the worker to send a message back to the parent process, the worker needs the parent PId.  So when the parent spawns the worker, the parent should specify self() as an additional argument for the spawned process.  self() returns the Pid of the process in which self() is called.

The problem is: messages sent by the worker processes to the parent process can arrive in any order, so you won't know how to join the pieces of the file back together. You could label each chunk with a number, and when you received all the chunks from the workers and gathered them into a list of tuples, [{Id, Chunk}], you could sort the list on the first element.  Then you could run lists:map() over that list to get a list of chunks [Chunk1, Chunk2, ...ChunkN], then string:join() will take care of the rest.  lists:foldlr() could do the same thing as lists:map() and string:join() in a single traversal of the list, but that is likely too advanced for you at this poin.


> I'm brand new to erlang! 2 weeks to be exact (and also new to programming). 

Well, you certainly talk the talk, so you should be congratulated for that.  However, I don't believe Erlang is appropriate for a beginner.  I think you would have much, much more fun learning Python.  Really.  No question in my mind.  I would spend a year with Python before attempting a functional language like Erlang.     Concurrency is not a beginning topic in the field of computer programming.  And, Erlang is said to make easy things hard and hard things easy.   As a beginner, you want easy things to be easy.


Reply all
Reply to author
Forward
0 new messages