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 8976663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:11:57+00:00 2026-06-15T19:11:57+00:00

A process in Erlang will either call link/1 or spawn_link to create a link

  • 0

A process in Erlang will either call link/1 or spawn_link to create a link with another process. In a recent application i am working on i got curious on whether its possible for a process to know at a given instance, the number of other processes its linked to. is this possible ? is their a BIF ?

Then, also, when a linked process dies, i guess that if it were possible to know the number of linked processes, this number would be decremented automatically by the run-time system. Such a mechanism would be ideal in dealing with Parent-Child relationships in Erlang concurrent programs, even in simple ones which do not involve supervisors.

Well, is it possible for an Erlang process to know out-of-the-box perhaps via a BIF, the number of processes linked to it, such that whenever a linked process dies, this value is decremented automatically under-the-hood :)?


To expand on this question a little bit, consider a gen_server, which will handle thousands of messages via handle_info. In this part, its job is to dispatch child processes to handle the task as soon as it comes in. The aim of this is to make sure the server loop returns immediately to take up the next request. Now, the child process handles the task asynchronously and sends the reply back to the requestor before it dies. Please refer to this question and its answer before you continue.

Now, what if, for every child process spawned off by the gen_server, a link is created, and i would like to use this link as a counter. I know, i know, everyone is going to be like ” why not use the gen_server State, to carry say, a counter, and then increment or decrement it accordingly ? ” 🙂 Somewhere in the gen_server, i have:

handle_info({Sender,Task},State)->
    spawn_link(?MODULE,child,[Sender,Task]),
    %%  At this point, the number of links to the gen_server is incremented
    %%  by the run-time system
    {noreply,State};
handle_info( _ ,State) -> {noreply,State}.

The child goes on to do this:

child(Sender,Task)->
    Result = (catch execute_task(Task)),
    Sender ! Result,
    ok. %% At this point the child process exits, 
        %% and i expect the link value to be decremented

Then finally, the gen_server has an exposed call like this:

get_no_of_links()-> gen_server:call(?MODULE,links).
handle_call(links, _ ,State)->
    %% BIF to get number of instantaneous links expected here
    Links = erlang:get_links(), %% This is fake, do not do it at home :)
    {reply,Links,State};
handle_call(_ , _ ,State)-> {reply,ok,State}.

Now, some one may ask them selves, really, Why would anyone want to do this ?

Usually, its possible to create an integer in the gen_server State and then we do it ourselves, or at least make the gen_server handle_info of type {'EXIT',ChildPid,_Reason} and then the server would act accordingly. My thinking is that if it were possible to know the number of links, i would use this to know ( at a given moment in time), how many child processes are still busy working, this in turn may actually assist in anticipating server load.

  • 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-15T19:11:58+00:00Added an answer on June 15, 2026 at 7:11 pm

    From manual for process_info:

    {links, Pids}:
    Pids is a list of pids, with processes to which the process
    has a link

    3> process_info(self(), links).
    {links,[<0.26.0>]}
    4> spawn_link(fun() -> timer:sleep(100000) end).
    <0.38.0>
    5> process_info(self(), links).                 
    {links,[<0.26.0>,<0.38.0>]}
    

    I guess it could be used to count number of linked processes

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

Sidebar

Related Questions

My Erlang app will create up to 1'000'000 processes. Each process will be a
I am in the process of designing an Erlang/OTP application which will expose its
It seems that an erlang process will stay alive until the 5 sec default
Playing with Erlang, I've got a process-looping function like: process_loop(...A long list of parameters
How can I tell a specific process (let's call it the generator) to create
My working environment is Erlang. Can a process having 2 different functions have two
I have erlang application. In this application i run process with spawn(?MODULE, my_foo, [my_param1,
I am trying to create a process in Erlang that can monitor itself and
Hi I am implementing an IM-server-like application in Erlang. I used one agent process
Normally if I'd like to have an Erlang process timeout I would use the

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.