View Single Post
Old 10-26-2008, 10:42 AM   #2
Antartica
Evangelist
Antartica ought to be getting tired of karma fortunes by now.Antartica ought to be getting tired of karma fortunes by now.Antartica ought to be getting tired of karma fortunes by now.Antartica ought to be getting tired of karma fortunes by now.Antartica ought to be getting tired of karma fortunes by now.Antartica ought to be getting tired of karma fortunes by now.Antartica ought to be getting tired of karma fortunes by now.Antartica ought to be getting tired of karma fortunes by now.Antartica ought to be getting tired of karma fortunes by now.Antartica ought to be getting tired of karma fortunes by now.Antartica ought to be getting tired of karma fortunes by now.
 
Antartica's Avatar
 
Posts: 423
Karma: 1517132
Join Date: Jun 2006
Location: Madrid, Spain
Device: quaderno, remarkable2, yotaphone2, prs950, iliad, onhandpc, newton
Quote:
Originally Posted by ericshliao View Post
I am working on a cbz/cbr viewer. I used popen function to call unzip/unrar and get their output. Basically, this medthod works, but it seems to occupy more and more system resource even after unzip/unrar finished their jobs. This problem caused app crash.
I tried pipe/fork, still same problem.

I wonder if there are ways to re-collect system resource.

If not possible, then the only way left to me is finding a proper wrapped zip/rar lib and call it in my app.
I suppose that you're exhausting the available memory.

POPEN

The popen() doesn't need anything apart of the close() call to the file descriptor (the OS should recollect the child afterwards).

FORK

If you use fork, the you have to recollect (use the waitpid() function) the finished children or the OS will leave them using memory until you recollect them, just for the sake of being notified of the exit value of the child program.

For a code example on how to do it, look at xepdmgr, as it manages the spawning/recollection of the launched app.

What you have to do:
1. Install a signal handler for sigint, so that you are informed when a child has exited. Remember that in signal handlers not all library functions are allowed; to be in the safe side, limit yourself to only use read()/write() to a pipe so that your main loop can detect it; alternatively you can set a flag variable and cross your fingers that your main loop sees it soon.
2. When a child has exited, you have to call waitpid() ro recollect the status of the exited application. Once you recollect it, the OS will free the resources used by that child program.
Antartica is offline   Reply With Quote