Is there any way to know how many sockets have been created by a C program?

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





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I have been running a C program which connects to a Redis database every once in a while. After running the program for a few hours it was displaying "Cannot create socket: too many files open". I'm pretty sure that I'm closing the connection every time after I'm done with the logic part at the database. So I wanted to know if there is any way to know how many sockets have been created by a C program, while it is running?
I'm using Ubuntu 16.04.










share|improve this question



















  • 1





    On linux: find /proc/PID/fd -lname 'socket:*' | wc -l. But unless your C program is the next big thing, it has no business having open more than a couple sockets at the same time; go fix it so it stops leaking fds -- every open file or socket should be closed when no longer in use.

    – mosvy
    Mar 14 at 8:04


















0















I have been running a C program which connects to a Redis database every once in a while. After running the program for a few hours it was displaying "Cannot create socket: too many files open". I'm pretty sure that I'm closing the connection every time after I'm done with the logic part at the database. So I wanted to know if there is any way to know how many sockets have been created by a C program, while it is running?
I'm using Ubuntu 16.04.










share|improve this question



















  • 1





    On linux: find /proc/PID/fd -lname 'socket:*' | wc -l. But unless your C program is the next big thing, it has no business having open more than a couple sockets at the same time; go fix it so it stops leaking fds -- every open file or socket should be closed when no longer in use.

    – mosvy
    Mar 14 at 8:04














0












0








0


0






I have been running a C program which connects to a Redis database every once in a while. After running the program for a few hours it was displaying "Cannot create socket: too many files open". I'm pretty sure that I'm closing the connection every time after I'm done with the logic part at the database. So I wanted to know if there is any way to know how many sockets have been created by a C program, while it is running?
I'm using Ubuntu 16.04.










share|improve this question
















I have been running a C program which connects to a Redis database every once in a while. After running the program for a few hours it was displaying "Cannot create socket: too many files open". I'm pretty sure that I'm closing the connection every time after I'm done with the logic part at the database. So I wanted to know if there is any way to know how many sockets have been created by a C program, while it is running?
I'm using Ubuntu 16.04.







shell networking c socket






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 14 at 10:01







yash

















asked Mar 14 at 7:03









yashyash

256




256







  • 1





    On linux: find /proc/PID/fd -lname 'socket:*' | wc -l. But unless your C program is the next big thing, it has no business having open more than a couple sockets at the same time; go fix it so it stops leaking fds -- every open file or socket should be closed when no longer in use.

    – mosvy
    Mar 14 at 8:04













  • 1





    On linux: find /proc/PID/fd -lname 'socket:*' | wc -l. But unless your C program is the next big thing, it has no business having open more than a couple sockets at the same time; go fix it so it stops leaking fds -- every open file or socket should be closed when no longer in use.

    – mosvy
    Mar 14 at 8:04








1




1





On linux: find /proc/PID/fd -lname 'socket:*' | wc -l. But unless your C program is the next big thing, it has no business having open more than a couple sockets at the same time; go fix it so it stops leaking fds -- every open file or socket should be closed when no longer in use.

– mosvy
Mar 14 at 8:04






On linux: find /proc/PID/fd -lname 'socket:*' | wc -l. But unless your C program is the next big thing, it has no business having open more than a couple sockets at the same time; go fix it so it stops leaking fds -- every open file or socket should be closed when no longer in use.

– mosvy
Mar 14 at 8:04











2 Answers
2






active

oldest

votes


















3














You did not specify an operating system, and /proc/*/fd/ does not exist on all operating systems.



You actually need to list all of the currently open file descriptors for the process, from which you can determine what is actually being leaked. It is not necessarily a socket file descriptor that is being leaked.



On FreeBSD, NetBSD, OpenBSD …



… and their various derivatives (e.g. TrueOS, DragonFly BSD).



Use the fstat command with the -p option and the relevant process ID:


fstat -p 718



  • fstat. NetBSD General Commands Manual. 2013-12-15.

On AIX



Use the procfiles command similarly, with the -n option to print names:


procfiles -n 6679



  • procfiles. AIX 7.2. IBM Knowledge Centre.

On OpenSolaris …



… and thus on Illumos, Schillix, et al.



Use the pfiles command:


pfiles 13253



  • pfiles. User Commands. SunOS 10.5 Manual. 2008-12-10.

On Linux



Use the lsof command similarly:


lsof -p 41467


Furthermore



In my C++ programs, a simple class that owns a file descriptor and closes it in its destructor works wonders. Unfortunately, you'll have to resort to GNU-specific language extensions (i.e. __attribute__(__cleanup__)) to get that in C programs.



You can of course go further with other tools. With DTrace and a suitable script, you can monitor the process as it opens and closes file descriptors. (You can do this with truss, or ktrace or strace, too; although the their mechanisms for selecting subsets of the system calls made are either fairly basic or nonexistent. You can also do this with a debugger and some suitable breakpoints.)






share|improve this answer






























    0














    At a system level at does not better what language the program is written in.



    Looking in /proc/«pid»/fd will tell you total number of file descriptors, that the process has open (not just sockets), but will be a close approximation. Maybe some one else can tell you for sockets.



    There is also strace you can use this to trace system calls that your process makes. strace «my-program» «program args…»



    in the program



    Add some logging: printf when you open and close a socket, to a logfile.






    share|improve this answer























    • Does the count of files in /proc/«pid»/fd show the total number of sockets open at current time or does the count specify the total number of sockets created over time.

      – yash
      Mar 14 at 9:35











    • No. There is no count summary, there are just files. Each file represents an open file ( where file is a file, socket, pipe, … that is open at the time). Therefore there will be a few more than the number of open sockets. (3 plus one for every socket, file, pipe that the process opened).

      – ctrl-alt-delor
      Mar 14 at 16:45











    • @JdeBP`s answer is fuller, and shows how to get just the sockets.

      – ctrl-alt-delor
      Mar 14 at 16:47











    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',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    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%2f506222%2fis-there-any-way-to-know-how-many-sockets-have-been-created-by-a-c-program%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    You did not specify an operating system, and /proc/*/fd/ does not exist on all operating systems.



    You actually need to list all of the currently open file descriptors for the process, from which you can determine what is actually being leaked. It is not necessarily a socket file descriptor that is being leaked.



    On FreeBSD, NetBSD, OpenBSD …



    … and their various derivatives (e.g. TrueOS, DragonFly BSD).



    Use the fstat command with the -p option and the relevant process ID:


    fstat -p 718



    • fstat. NetBSD General Commands Manual. 2013-12-15.

    On AIX



    Use the procfiles command similarly, with the -n option to print names:


    procfiles -n 6679



    • procfiles. AIX 7.2. IBM Knowledge Centre.

    On OpenSolaris …



    … and thus on Illumos, Schillix, et al.



    Use the pfiles command:


    pfiles 13253



    • pfiles. User Commands. SunOS 10.5 Manual. 2008-12-10.

    On Linux



    Use the lsof command similarly:


    lsof -p 41467


    Furthermore



    In my C++ programs, a simple class that owns a file descriptor and closes it in its destructor works wonders. Unfortunately, you'll have to resort to GNU-specific language extensions (i.e. __attribute__(__cleanup__)) to get that in C programs.



    You can of course go further with other tools. With DTrace and a suitable script, you can monitor the process as it opens and closes file descriptors. (You can do this with truss, or ktrace or strace, too; although the their mechanisms for selecting subsets of the system calls made are either fairly basic or nonexistent. You can also do this with a debugger and some suitable breakpoints.)






    share|improve this answer



























      3














      You did not specify an operating system, and /proc/*/fd/ does not exist on all operating systems.



      You actually need to list all of the currently open file descriptors for the process, from which you can determine what is actually being leaked. It is not necessarily a socket file descriptor that is being leaked.



      On FreeBSD, NetBSD, OpenBSD …



      … and their various derivatives (e.g. TrueOS, DragonFly BSD).



      Use the fstat command with the -p option and the relevant process ID:


      fstat -p 718



      • fstat. NetBSD General Commands Manual. 2013-12-15.

      On AIX



      Use the procfiles command similarly, with the -n option to print names:


      procfiles -n 6679



      • procfiles. AIX 7.2. IBM Knowledge Centre.

      On OpenSolaris …



      … and thus on Illumos, Schillix, et al.



      Use the pfiles command:


      pfiles 13253



      • pfiles. User Commands. SunOS 10.5 Manual. 2008-12-10.

      On Linux



      Use the lsof command similarly:


      lsof -p 41467


      Furthermore



      In my C++ programs, a simple class that owns a file descriptor and closes it in its destructor works wonders. Unfortunately, you'll have to resort to GNU-specific language extensions (i.e. __attribute__(__cleanup__)) to get that in C programs.



      You can of course go further with other tools. With DTrace and a suitable script, you can monitor the process as it opens and closes file descriptors. (You can do this with truss, or ktrace or strace, too; although the their mechanisms for selecting subsets of the system calls made are either fairly basic or nonexistent. You can also do this with a debugger and some suitable breakpoints.)






      share|improve this answer

























        3












        3








        3







        You did not specify an operating system, and /proc/*/fd/ does not exist on all operating systems.



        You actually need to list all of the currently open file descriptors for the process, from which you can determine what is actually being leaked. It is not necessarily a socket file descriptor that is being leaked.



        On FreeBSD, NetBSD, OpenBSD …



        … and their various derivatives (e.g. TrueOS, DragonFly BSD).



        Use the fstat command with the -p option and the relevant process ID:


        fstat -p 718



        • fstat. NetBSD General Commands Manual. 2013-12-15.

        On AIX



        Use the procfiles command similarly, with the -n option to print names:


        procfiles -n 6679



        • procfiles. AIX 7.2. IBM Knowledge Centre.

        On OpenSolaris …



        … and thus on Illumos, Schillix, et al.



        Use the pfiles command:


        pfiles 13253



        • pfiles. User Commands. SunOS 10.5 Manual. 2008-12-10.

        On Linux



        Use the lsof command similarly:


        lsof -p 41467


        Furthermore



        In my C++ programs, a simple class that owns a file descriptor and closes it in its destructor works wonders. Unfortunately, you'll have to resort to GNU-specific language extensions (i.e. __attribute__(__cleanup__)) to get that in C programs.



        You can of course go further with other tools. With DTrace and a suitable script, you can monitor the process as it opens and closes file descriptors. (You can do this with truss, or ktrace or strace, too; although the their mechanisms for selecting subsets of the system calls made are either fairly basic or nonexistent. You can also do this with a debugger and some suitable breakpoints.)






        share|improve this answer













        You did not specify an operating system, and /proc/*/fd/ does not exist on all operating systems.



        You actually need to list all of the currently open file descriptors for the process, from which you can determine what is actually being leaked. It is not necessarily a socket file descriptor that is being leaked.



        On FreeBSD, NetBSD, OpenBSD …



        … and their various derivatives (e.g. TrueOS, DragonFly BSD).



        Use the fstat command with the -p option and the relevant process ID:


        fstat -p 718



        • fstat. NetBSD General Commands Manual. 2013-12-15.

        On AIX



        Use the procfiles command similarly, with the -n option to print names:


        procfiles -n 6679



        • procfiles. AIX 7.2. IBM Knowledge Centre.

        On OpenSolaris …



        … and thus on Illumos, Schillix, et al.



        Use the pfiles command:


        pfiles 13253



        • pfiles. User Commands. SunOS 10.5 Manual. 2008-12-10.

        On Linux



        Use the lsof command similarly:


        lsof -p 41467


        Furthermore



        In my C++ programs, a simple class that owns a file descriptor and closes it in its destructor works wonders. Unfortunately, you'll have to resort to GNU-specific language extensions (i.e. __attribute__(__cleanup__)) to get that in C programs.



        You can of course go further with other tools. With DTrace and a suitable script, you can monitor the process as it opens and closes file descriptors. (You can do this with truss, or ktrace or strace, too; although the their mechanisms for selecting subsets of the system calls made are either fairly basic or nonexistent. You can also do this with a debugger and some suitable breakpoints.)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 14 at 9:40









        JdeBPJdeBP

        38.1k478185




        38.1k478185























            0














            At a system level at does not better what language the program is written in.



            Looking in /proc/«pid»/fd will tell you total number of file descriptors, that the process has open (not just sockets), but will be a close approximation. Maybe some one else can tell you for sockets.



            There is also strace you can use this to trace system calls that your process makes. strace «my-program» «program args…»



            in the program



            Add some logging: printf when you open and close a socket, to a logfile.






            share|improve this answer























            • Does the count of files in /proc/«pid»/fd show the total number of sockets open at current time or does the count specify the total number of sockets created over time.

              – yash
              Mar 14 at 9:35











            • No. There is no count summary, there are just files. Each file represents an open file ( where file is a file, socket, pipe, … that is open at the time). Therefore there will be a few more than the number of open sockets. (3 plus one for every socket, file, pipe that the process opened).

              – ctrl-alt-delor
              Mar 14 at 16:45











            • @JdeBP`s answer is fuller, and shows how to get just the sockets.

              – ctrl-alt-delor
              Mar 14 at 16:47















            0














            At a system level at does not better what language the program is written in.



            Looking in /proc/«pid»/fd will tell you total number of file descriptors, that the process has open (not just sockets), but will be a close approximation. Maybe some one else can tell you for sockets.



            There is also strace you can use this to trace system calls that your process makes. strace «my-program» «program args…»



            in the program



            Add some logging: printf when you open and close a socket, to a logfile.






            share|improve this answer























            • Does the count of files in /proc/«pid»/fd show the total number of sockets open at current time or does the count specify the total number of sockets created over time.

              – yash
              Mar 14 at 9:35











            • No. There is no count summary, there are just files. Each file represents an open file ( where file is a file, socket, pipe, … that is open at the time). Therefore there will be a few more than the number of open sockets. (3 plus one for every socket, file, pipe that the process opened).

              – ctrl-alt-delor
              Mar 14 at 16:45











            • @JdeBP`s answer is fuller, and shows how to get just the sockets.

              – ctrl-alt-delor
              Mar 14 at 16:47













            0












            0








            0







            At a system level at does not better what language the program is written in.



            Looking in /proc/«pid»/fd will tell you total number of file descriptors, that the process has open (not just sockets), but will be a close approximation. Maybe some one else can tell you for sockets.



            There is also strace you can use this to trace system calls that your process makes. strace «my-program» «program args…»



            in the program



            Add some logging: printf when you open and close a socket, to a logfile.






            share|improve this answer













            At a system level at does not better what language the program is written in.



            Looking in /proc/«pid»/fd will tell you total number of file descriptors, that the process has open (not just sockets), but will be a close approximation. Maybe some one else can tell you for sockets.



            There is also strace you can use this to trace system calls that your process makes. strace «my-program» «program args…»



            in the program



            Add some logging: printf when you open and close a socket, to a logfile.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 14 at 7:27









            ctrl-alt-delorctrl-alt-delor

            12.4k52662




            12.4k52662












            • Does the count of files in /proc/«pid»/fd show the total number of sockets open at current time or does the count specify the total number of sockets created over time.

              – yash
              Mar 14 at 9:35











            • No. There is no count summary, there are just files. Each file represents an open file ( where file is a file, socket, pipe, … that is open at the time). Therefore there will be a few more than the number of open sockets. (3 plus one for every socket, file, pipe that the process opened).

              – ctrl-alt-delor
              Mar 14 at 16:45











            • @JdeBP`s answer is fuller, and shows how to get just the sockets.

              – ctrl-alt-delor
              Mar 14 at 16:47

















            • Does the count of files in /proc/«pid»/fd show the total number of sockets open at current time or does the count specify the total number of sockets created over time.

              – yash
              Mar 14 at 9:35











            • No. There is no count summary, there are just files. Each file represents an open file ( where file is a file, socket, pipe, … that is open at the time). Therefore there will be a few more than the number of open sockets. (3 plus one for every socket, file, pipe that the process opened).

              – ctrl-alt-delor
              Mar 14 at 16:45











            • @JdeBP`s answer is fuller, and shows how to get just the sockets.

              – ctrl-alt-delor
              Mar 14 at 16:47
















            Does the count of files in /proc/«pid»/fd show the total number of sockets open at current time or does the count specify the total number of sockets created over time.

            – yash
            Mar 14 at 9:35





            Does the count of files in /proc/«pid»/fd show the total number of sockets open at current time or does the count specify the total number of sockets created over time.

            – yash
            Mar 14 at 9:35













            No. There is no count summary, there are just files. Each file represents an open file ( where file is a file, socket, pipe, … that is open at the time). Therefore there will be a few more than the number of open sockets. (3 plus one for every socket, file, pipe that the process opened).

            – ctrl-alt-delor
            Mar 14 at 16:45





            No. There is no count summary, there are just files. Each file represents an open file ( where file is a file, socket, pipe, … that is open at the time). Therefore there will be a few more than the number of open sockets. (3 plus one for every socket, file, pipe that the process opened).

            – ctrl-alt-delor
            Mar 14 at 16:45













            @JdeBP`s answer is fuller, and shows how to get just the sockets.

            – ctrl-alt-delor
            Mar 14 at 16:47





            @JdeBP`s answer is fuller, and shows how to get just the sockets.

            – ctrl-alt-delor
            Mar 14 at 16:47

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f506222%2fis-there-any-way-to-know-how-many-sockets-have-been-created-by-a-c-program%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown






            Popular posts from this blog

            Peggy Mitchell

            The Forum (Inglewood, California)

            Palaiologos