site_archiver@lists.apple.com Delivered-To: darwin-kernel@lists.apple.com Hi, Problem : Most of the time lsof gets hang (go in a sleep mode "S+") Steps what i am doing to parse the output of lsof for a list of pids. 1) Getting a list of pids with the use of sysctl(). 2) For each pid, storing the output of lsof in a file by calling popen("lsof -F n -p PID","r"); However... -- Terry _______________________________________________ Do not post admin requests to the list. They will be ignored. Darwin-kernel mailing list (Darwin-kernel@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/darwin-kernel/site_archiver%40lists.a... On Apr 18, 2008, at 12:36 AM, Harshal Choudhari wrote: I am new to mac programming, and i am facing a problem while parsing the output of lsof command for listing the information of pids. When I am executing these lines of code in multiple threads, then most of the time one or more than one thread gets hang i.e. it is not coming out of the thread(my application gets hang). If I am listing the state of processes, it has been observed that pid of some processes specially of lsof is in sleep mode (S+), and if we see its more details, then it is found that lsof processes are in sleep mode those are executed for a PID which is not existed. We know that lsof return with zero output, if we call it for a pid which is non existing. so it should not be a reason for lsof to go in sleep mode. So I want to know that, is there any limitation / bug inside the lsof for which it never come out from its call, or how lsof behaves if we call it for a pid which was existing at the time of call but gets killed/go out of scope/not existed while lsof is in processing (before come out with output). Whether this situation is handled by lsof? if so then in which case lsof can hangs/sleep and how to handle this situation. Or is their any problem with popen? lsof uses a lot of sysctl() calls which take the kernel funnel on entry and release it on exit; it's possible that you've just gotten two iterators into a lockstep, and the code is running really, really slow. Alternately, you could have some other blocking operation in progress holding the funnel (you actually didn't say anything about which version of Mac OS X you were running). Generally, you will have to use two machine kernel debugging to see where your problem happens. You are aware that lsof will order the outputs of a single invocation by pid, and that even if it didn't, you could sort by the pid field anyway, right? That's means the most efficient way to get the information you want is to popen() a single instance of lsof and simply switch on the pid field, and do the separation internally to your program, in a single thread instance. This email sent to site_archiver@lists.apple.com