Find the answer to your Linux question:
Results 1 to 2 of 2
Hi, Say I have a C program that does: While(1){system(“ls”);} The system() call creates a new PID at each call. Therefore, when the maximum number of PIDs is reached, the ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2011
    Posts
    10

    Question How to limit or reset PIDs


    Hi,

    Say I have a C program that does:

    While(1){system(“ls”);}

    The system() call creates a new PID at each call. Therefore, when the maximum number of PIDs is reached, the ls command becomes <defunct> and eventually the system crashes.

    I tried also popen() but it is the same result.

    Is there a way of avoiding this behavior?

    Thanks,
    S

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,596
    This is when you need to wait on death-of-child events with wait() or waitpid(), inside your loop. That will remove the defunct pid from the process tree, otherwise the observed behavior is expected. The system() command does a fork/exec and each fork generates a new pid, as does popen(). Alternatively, you can use one of the exec...() functions, but then your root process will no longer exist. IE, using exec() without a fork will transform your current process into a new one, but using the same PID.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •