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

  • Home
  • SEARCH
  • 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 7506517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:18:08+00:00 2026-05-29T22:18:08+00:00

I am working on some code which is the back-end for a tool. This

  • 0

I am working on some code which is the back-end for a tool. This tool creates all its output in a fixed directory. Now suppose at any time a client wants to kill all its applications, then all files should be deleted. So I use the system command (rm -rf) to remove the directory, but files which are open are not deleted, and hence the directory is also not deleted. How can I do this smartly?

One option seems to be to use a table maintaining all the open files and closing them all before firing rm -rf on the directory. However, this may slow down the whole application.

Another option I explored is using lsof to find a list of open files and close them all before rm -rf.

An additional constraint on my issue is that I have open several different files in a process, and one of them is in use by another process. Using the above methods, if I close all the file descriptors and try to delete the directory, logically it should not be deleted because there are still files opened by another process.

Any suggestions would be appreciated.

  • 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-29T22:18:10+00:00Added an answer on May 29, 2026 at 10:18 pm

    I think using a script is a good idea for your instance, and Coren’s answer is precise. That being said, I’ve seen programs just loop through a substantial range of file descriptors to close all of them. It has the potential for error if you have more than the range you loop through, but something like this shouldn’t hurt an application if you’re intending to close all your descriptors, provided they are within this limit:

    for (int i = 0; i < 4096; i++) {
        close(i);
    }
    

    In order to have this done before your application exits, you can set a signal handler, as mentioned and linked by Coren. Make sure if you use signals and a handler that you do as little as possible in the signal handler, i.e. just set a flag and have your program do its cleanup.

    Edit: Also, this answer from another post on StackOverflow gives both examples of how to do this (#3 is like Coren’s and is for Linux), but also has a couple of links to actual source code that handle this stuff. You might want to check it out!

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

Sidebar

Related Questions

I am working with some code which outputs a 3x3 rotation matrix and a
I am working on some code coverage for my applications. Now, I know that
I'm currently working on some MPI code for a graph theory problem in which
I'm working on our back-end encoder service which does an awful lot of video
While working on some graphics code a while back, I wrote Rect and Region
I'm working on some code to colorize an image in Java. Basically what I'd
I'm working on some code that uses the System.Diagnostics.Trace class and I'm wondering how
I'm working on some code for a loosely coupled cluster. To achieve optimal performance
I'm working with some code that is confusing me and I'm wondering if I'm
I was working on some code recently and came across a method that had

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.