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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:35:36+00:00 2026-05-16T05:35:36+00:00

How does Linux determine the next PID it will use for a process? The

  • 0

How does Linux determine the next PID it will use for a process? The purpose of this question is to better understand the Linux kernel. Don’t be afraid to post kernel source code. If PIDs are allocated sequentially how does Linux fill in the gaps? What happens when it hits the end?

For example if I run a PHP script from Apache that does a <?php print(getmypid());?> the same PID will be printed out for a few minutes while hit refresh. This period of time is a function of how many requests apache is receiving. Even if there is only one client the PID will eventually change.

When the PID changes, it will be a close number, but how close? The number does not appear to be entirely sequential. If I do a ps aux | grep apache I get a fair number of processes:

enter image description here

How does Linux choose this next number? The previous few PID’s are still running, as well as the most recent PID that was printed. How does apache choose to reuse these PIDs?

  • 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-05-16T05:35:37+00:00Added an answer on May 16, 2026 at 5:35 am

    The kernel allocates PIDs in the range of (RESERVED_PIDS, PID_MAX_DEFAULT). It does so sequentially in each namespace (tasks in different namespaces can have the same IDs). In case the range is exhausted, pid assignment wraps around.

    Some relevant code:

    Inside alloc_pid(…)

    for (i = ns->level; i >= 0; i--) {
        nr = alloc_pidmap(tmp);
        if (nr < 0)
            goto out_free;
        pid->numbers[i].nr = nr;
        pid->numbers[i].ns = tmp;
        tmp = tmp->parent;
    }
    

    alloc_pidmap()

    static int alloc_pidmap(struct pid_namespace *pid_ns)
    {
            int i, offset, max_scan, pid, last = pid_ns->last_pid;
            struct pidmap *map;
    
            pid = last + 1;
            if (pid >= pid_max)
                    pid = RESERVED_PIDS;
            /* and later on... */
            pid_ns->last_pid = pid;
            return pid;
    }
    

    Do note that PIDs in the context of the kernel are more than just int identifiers; the relevant structure can be found in /include/linux/pid.h. Besides the id, it contains a list of tasks with that id, a reference counter and a hashed list node for fast access.

    The reason for PIDs not appearing sequential in user space is because kernel scheduling might fork a process in between your process’ fork() calls. It’s very common, in fact.

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

Sidebar

Related Questions

How to use linux kernel's find_module() function? The documentation says must hold module_mutex. Does
This question is inspired by Does Linux provide a monotonically increasing clock to applications
How does one determine the current stack size of a program in linux? it
I am working with linux kernel 2.6.38 and have a question about two fields
In linux, how can I determine the default text editor, terminal, etc? does it
I'm trying to understand how does Linux encrypt our password on the etc/shadow file,
I'm doing some Linux kernel development, and I'm trying to use Netbeans. Despite declared
Does linux have commands allow me to push my regularly used commands into a
Does Linux automatically re-claim all memory used by an applications immediately? If so then
I want to trace the modification history of a specific file. Does linux have

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.