Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8938933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:44:13+00:00 2026-06-15T10:44:13+00:00

How can I tell a specific process (let’s call it the generator) to create

  • 0

How can I tell a specific process (let’s call it the generator) to create a new process in Erlang?

Also, how can I keep track of all the processes that the generator has created?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:44:14+00:00Added an answer on June 15, 2026 at 10:44 am

    Okay. Now, for purposes of learning, lets do a very basic example. Others will talk of OTP Supervisors, Process dictionaries e.t.c. but i want to keep it easy for learning purposes. With tis example, the generator can know how many processes it has created, their Pids, knows when they die e.t.c.

    -module(generator).
    -compile(export_all).
    -define(ERROR(X),error_logger:error_report(X)).
    start()-> register(generator,spawn(fun() -> generator() end)), ok.
    generator()-> ProcessBuffer = [], process_flag(trap_exit,true), loop(ProcessBuffer).
    create(Module,Function,Args)-> generator ! {create_new_proc,{Module,Function,Args}}, ok.
    send_to_kids(Message)-> generator ! {tell_kids,Message}, ok.
    loop(Buffer)-> receive {create_new_proc,{M,F,A}} -> Pid = spawn(M,F,A), link(Pid), loop([Pid|Buffer]); {tell_kids,Message} -> [ Kid ! {broadcast,Message} || Kid <- Buffer], loop(Buffer); {'EXIT',SomePid,Reason} -> ?ERROR(["Child has crashed",{pid,SomePid},{reason,Reason}]), loop(lists:delete(SomePid,Buffer)); _ -> loop(Buffer) end.
    child_loop()-> receive {broadcast,Message} -> io:format("\n\tChild: ~p got: ~p~n",[self(),Message]), child_loop(); _ -> child_loop() end.

    Testing in shell

    E:\Applications>erl
    Eshell V5.9  (abort with ^G)
    1> c(generator).
    {ok,generator}
    2> generator:start().
    ok
    3> generator:create(generator,child_loop,[]).
    ok
    4> [generator:create(generator,child_loop,[]) || _ <- lists:seq(1,5)],ok.
    ok
    5> generator:send_to_kids("Erlang is good !").
            Child: <0.45.0> got: "Erlang is good !"
            Child: <0.44.0> got: "Erlang is good !"
            Child: <0.43.0> got: "Erlang is good !"
            Child: <0.42.0> got: "Erlang is good !"
            Child: <0.41.0> got: "Erlang is good !"
            Child: <0.39.0> got: "Erlang is good !"
    ok
    6> generator:send_to_kids("1+1 = 2").
            Child: <0.45.0> got: "1+1 = 2"
            Child: <0.44.0> got: "1+1 = 2"
            Child: <0.43.0> got: "1+1 = 2"
            Child: <0.42.0> got: "1+1 = 2"
            Child: <0.41.0> got: "1+1 = 2"
            Child: <0.39.0> got: "1+1 = 2"
    ok
    7>
    

    A process registered as generator is started and may be told to start any number of children processes. The child process is generically created from the {M, F, A} combination sent to the generator. The generator loop keeps a track record of the processes created. It can then be told to broadcast messages to all processes.

    However, in practice/ production, you might not to do this, because the generator loop may consume lots of memory as the list Buffer grows since thats where we keep all the Pids of our processes. You can also use ETS Tables, or Mnesia e.t.c but i havenot used them in the example because they will seem a little bit more advanced. Look at this question and its answer to understand more.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone please tell me how to show all privileges/rules from a specific user
Please tell me how can I get array of all Those DIV of specific
Under Linux, how can I tell what specific process owns / is using a
Duplicate: How can I tell if another process is 64bit? Can I detect the
I have an OSGi bundle that can also run in plain Java process. I
The Clipboard object has a ContainsData() method that can tell if a specific format
Anyone can tell me which one is better (JAXB or Apache XMLBeans) taking in
Anyone can tell me what I'm doing wrong? I am creating a simple system
Which API can tell me the information about if LED is supported by a
so from what i can tell, you have to specify artifacts and working directory

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.