File descriptors open info

mike at bria.commodore.com mike at bria.commodore.com
Thu Feb 28 12:23:50 AEST 1991


In an article, theory.tn.cornell.edu!hurf (Hurf Sheldon) writes:
>is there a Unix system call (or some other method) for
>finding out how many file descriptors a process has
>open?

Well, the *real* way to do this is to go munging through the kernel's
process table. :-)

Actually, you could do something like this:

	#define MAXFD	100	/* or some reasonable number */

	getnumfd()
	{
	int i = 0, num = 0;
	struct stat buf;

		while ( i < MAXFD )
			if ( fstat(i++,&buf) == 0 )
				++num;

		return num;
	}

Kind of ugly, but it could get the job done.  The only drawback is that
the MAXFD value may be unrealistic after a few "relinkings of the kernel."

Cheers,
-- 
Michael Stefanik, MGI Inc., Los Angeles| Opinions stated are not even my own.
Title of the week: Systems Engineer    | UUCP: ...!uunet!bria!mike
-------------------------------------------------------------------------------
Remember folks: If you can't flame MS-DOS, then what _can_ you flame?



More information about the Comp.unix.questions mailing list