PID reuse possibility in Linux

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












In a Linux operating system, is there any chance that a PID can be reused?



For example, A PID is named 2252. This PID is dead and erased from kernel process table. Is there any chance that the process table can re-use a same PID for a new process, or it will not be used in any of the upcoming processes?







share|improve this question






















  • duplicate? stackoverflow.com/questions/18122592/…
    – Michael D.
    Jan 5 at 11:42










  • No. Not a duplicate. the process is dead and removed from server. But is there any possibility that the same PID kernel can assign to future processes.
    – Athiri
    Jan 5 at 11:44










  • I can only guess - if cat /proc/sys/kernel/pid_max is reached, it might start from 2 looking for the next available, unused pid. I'd say yes.
    – Michael D.
    Jan 5 at 11:50










  • Ok. So in my case PID-2252 will be reused. Thanks Mike.
    – Athiri
    Jan 5 at 11:58







  • 1




    relating: unix.stackexchange.com/q/16883/117549
    – Jeff Schaller
    Jan 5 at 12:06














up vote
2
down vote

favorite












In a Linux operating system, is there any chance that a PID can be reused?



For example, A PID is named 2252. This PID is dead and erased from kernel process table. Is there any chance that the process table can re-use a same PID for a new process, or it will not be used in any of the upcoming processes?







share|improve this question






















  • duplicate? stackoverflow.com/questions/18122592/…
    – Michael D.
    Jan 5 at 11:42










  • No. Not a duplicate. the process is dead and removed from server. But is there any possibility that the same PID kernel can assign to future processes.
    – Athiri
    Jan 5 at 11:44










  • I can only guess - if cat /proc/sys/kernel/pid_max is reached, it might start from 2 looking for the next available, unused pid. I'd say yes.
    – Michael D.
    Jan 5 at 11:50










  • Ok. So in my case PID-2252 will be reused. Thanks Mike.
    – Athiri
    Jan 5 at 11:58







  • 1




    relating: unix.stackexchange.com/q/16883/117549
    – Jeff Schaller
    Jan 5 at 12:06












up vote
2
down vote

favorite









up vote
2
down vote

favorite











In a Linux operating system, is there any chance that a PID can be reused?



For example, A PID is named 2252. This PID is dead and erased from kernel process table. Is there any chance that the process table can re-use a same PID for a new process, or it will not be used in any of the upcoming processes?







share|improve this question














In a Linux operating system, is there any chance that a PID can be reused?



For example, A PID is named 2252. This PID is dead and erased from kernel process table. Is there any chance that the process table can re-use a same PID for a new process, or it will not be used in any of the upcoming processes?









share|improve this question













share|improve this question




share|improve this question








edited Jan 5 at 12:01









Jeff Schaller

31.8k848109




31.8k848109










asked Jan 5 at 11:35









Athiri

258




258











  • duplicate? stackoverflow.com/questions/18122592/…
    – Michael D.
    Jan 5 at 11:42










  • No. Not a duplicate. the process is dead and removed from server. But is there any possibility that the same PID kernel can assign to future processes.
    – Athiri
    Jan 5 at 11:44










  • I can only guess - if cat /proc/sys/kernel/pid_max is reached, it might start from 2 looking for the next available, unused pid. I'd say yes.
    – Michael D.
    Jan 5 at 11:50










  • Ok. So in my case PID-2252 will be reused. Thanks Mike.
    – Athiri
    Jan 5 at 11:58







  • 1




    relating: unix.stackexchange.com/q/16883/117549
    – Jeff Schaller
    Jan 5 at 12:06
















  • duplicate? stackoverflow.com/questions/18122592/…
    – Michael D.
    Jan 5 at 11:42










  • No. Not a duplicate. the process is dead and removed from server. But is there any possibility that the same PID kernel can assign to future processes.
    – Athiri
    Jan 5 at 11:44










  • I can only guess - if cat /proc/sys/kernel/pid_max is reached, it might start from 2 looking for the next available, unused pid. I'd say yes.
    – Michael D.
    Jan 5 at 11:50










  • Ok. So in my case PID-2252 will be reused. Thanks Mike.
    – Athiri
    Jan 5 at 11:58







  • 1




    relating: unix.stackexchange.com/q/16883/117549
    – Jeff Schaller
    Jan 5 at 12:06















duplicate? stackoverflow.com/questions/18122592/…
– Michael D.
Jan 5 at 11:42




duplicate? stackoverflow.com/questions/18122592/…
– Michael D.
Jan 5 at 11:42












No. Not a duplicate. the process is dead and removed from server. But is there any possibility that the same PID kernel can assign to future processes.
– Athiri
Jan 5 at 11:44




No. Not a duplicate. the process is dead and removed from server. But is there any possibility that the same PID kernel can assign to future processes.
– Athiri
Jan 5 at 11:44












I can only guess - if cat /proc/sys/kernel/pid_max is reached, it might start from 2 looking for the next available, unused pid. I'd say yes.
– Michael D.
Jan 5 at 11:50




I can only guess - if cat /proc/sys/kernel/pid_max is reached, it might start from 2 looking for the next available, unused pid. I'd say yes.
– Michael D.
Jan 5 at 11:50












Ok. So in my case PID-2252 will be reused. Thanks Mike.
– Athiri
Jan 5 at 11:58





Ok. So in my case PID-2252 will be reused. Thanks Mike.
– Athiri
Jan 5 at 11:58





1




1




relating: unix.stackexchange.com/q/16883/117549
– Jeff Schaller
Jan 5 at 12:06




relating: unix.stackexchange.com/q/16883/117549
– Jeff Schaller
Jan 5 at 12:06










1 Answer
1






active

oldest

votes

















up vote
7
down vote













Of course. Otherwise a system could only run 32768 processes (or whatever the maximum pid number is on the system) per boot.



As long as a process is dead and has been waited for (by its parent, or the sub child reaper or init if the parent is dead), its pid can be reused.



You see scripts doing things like:



cmd1 & pid1=$!
something else
cmd2 & pid2=$!
more things
kill "$pid1" "$pid2"


Those are approximations as the shell (most shells) language doesn't give you any better API to handle child processes.



There's no guarantee that $pid1 and/or $pid2 will still refer to the processes that were started earlier if those processes may have died. $pid1 and $pid2 could also be the same (if cmd1 has died by the time cmd2 is started). So kill could kill the wrong processes.



In practice, it's rarely a problem especially on systems where pids are assigned in sequence as it takes quite some time for pid numbers to wrap. But it can become so when the pid table gets full (like when it's filled with zombie processes) and some attackers can get advantage of that.






share|improve this answer






















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f414971%2fpid-reuse-possibility-in-linux%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    7
    down vote













    Of course. Otherwise a system could only run 32768 processes (or whatever the maximum pid number is on the system) per boot.



    As long as a process is dead and has been waited for (by its parent, or the sub child reaper or init if the parent is dead), its pid can be reused.



    You see scripts doing things like:



    cmd1 & pid1=$!
    something else
    cmd2 & pid2=$!
    more things
    kill "$pid1" "$pid2"


    Those are approximations as the shell (most shells) language doesn't give you any better API to handle child processes.



    There's no guarantee that $pid1 and/or $pid2 will still refer to the processes that were started earlier if those processes may have died. $pid1 and $pid2 could also be the same (if cmd1 has died by the time cmd2 is started). So kill could kill the wrong processes.



    In practice, it's rarely a problem especially on systems where pids are assigned in sequence as it takes quite some time for pid numbers to wrap. But it can become so when the pid table gets full (like when it's filled with zombie processes) and some attackers can get advantage of that.






    share|improve this answer


























      up vote
      7
      down vote













      Of course. Otherwise a system could only run 32768 processes (or whatever the maximum pid number is on the system) per boot.



      As long as a process is dead and has been waited for (by its parent, or the sub child reaper or init if the parent is dead), its pid can be reused.



      You see scripts doing things like:



      cmd1 & pid1=$!
      something else
      cmd2 & pid2=$!
      more things
      kill "$pid1" "$pid2"


      Those are approximations as the shell (most shells) language doesn't give you any better API to handle child processes.



      There's no guarantee that $pid1 and/or $pid2 will still refer to the processes that were started earlier if those processes may have died. $pid1 and $pid2 could also be the same (if cmd1 has died by the time cmd2 is started). So kill could kill the wrong processes.



      In practice, it's rarely a problem especially on systems where pids are assigned in sequence as it takes quite some time for pid numbers to wrap. But it can become so when the pid table gets full (like when it's filled with zombie processes) and some attackers can get advantage of that.






      share|improve this answer
























        up vote
        7
        down vote










        up vote
        7
        down vote









        Of course. Otherwise a system could only run 32768 processes (or whatever the maximum pid number is on the system) per boot.



        As long as a process is dead and has been waited for (by its parent, or the sub child reaper or init if the parent is dead), its pid can be reused.



        You see scripts doing things like:



        cmd1 & pid1=$!
        something else
        cmd2 & pid2=$!
        more things
        kill "$pid1" "$pid2"


        Those are approximations as the shell (most shells) language doesn't give you any better API to handle child processes.



        There's no guarantee that $pid1 and/or $pid2 will still refer to the processes that were started earlier if those processes may have died. $pid1 and $pid2 could also be the same (if cmd1 has died by the time cmd2 is started). So kill could kill the wrong processes.



        In practice, it's rarely a problem especially on systems where pids are assigned in sequence as it takes quite some time for pid numbers to wrap. But it can become so when the pid table gets full (like when it's filled with zombie processes) and some attackers can get advantage of that.






        share|improve this answer














        Of course. Otherwise a system could only run 32768 processes (or whatever the maximum pid number is on the system) per boot.



        As long as a process is dead and has been waited for (by its parent, or the sub child reaper or init if the parent is dead), its pid can be reused.



        You see scripts doing things like:



        cmd1 & pid1=$!
        something else
        cmd2 & pid2=$!
        more things
        kill "$pid1" "$pid2"


        Those are approximations as the shell (most shells) language doesn't give you any better API to handle child processes.



        There's no guarantee that $pid1 and/or $pid2 will still refer to the processes that were started earlier if those processes may have died. $pid1 and $pid2 could also be the same (if cmd1 has died by the time cmd2 is started). So kill could kill the wrong processes.



        In practice, it's rarely a problem especially on systems where pids are assigned in sequence as it takes quite some time for pid numbers to wrap. But it can become so when the pid table gets full (like when it's filled with zombie processes) and some attackers can get advantage of that.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 5 at 12:12

























        answered Jan 5 at 12:05









        Stéphane Chazelas

        281k53518849




        281k53518849






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f414971%2fpid-reuse-possibility-in-linux%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?