Path: utzoo!attcan!uunet!steinmetz!sagittarius!dixon From: dixon@sagittarius.steinmetz (walt dixon) Newsgroups: comp.sys.ibm.pc Subject: Re: File handles in TSR's: A tip and a question Keywords: Aaaargh! MesS DOS Message-ID: <12881@steinmetz.ge.com> Date: 5 Jan 89 14:47:28 GMT References: <146@bms-at.UUCP> Sender: news@steinmetz.ge.com Reply-To: dixon@sagittarius.steinmetz.ge.com (walt dixon) Organization: General Electric CRD, Schenectady, NY Lines: 54 File handles are an index into a process data structure called the Job File Table (JFT for short). The address of the JFT and usually the JFT itself are located in the Program Segment Prefix (PSP). Each JFT entry contains either a System File Number (SFN) or 0xff indicating the entry is unused. The SFN is an index into a DOS maintained data structure called the System File Table (SFT). There is one SFT no matter how many programs/TSR's are resident. (Actually DOS maintains a separate SFT for FCB access). The SFT entry records the file name (no path), status information (EOF, raw, etc.), file position (characters processed), and a path to the device driver. Other information recorded here include the entry owner (a PSP address) and a reference count. When you perform a handle i/o operation DOS uses the handle as an index into the JFT of the current PSP. DOS maintains an internal global variable which records the PSP address of the current process. DOS int 21H AH=50h or AH=62H return the current PSP address; DOS int 21H AH=51h sets the current PSP to the value contained in BX. The main difference between the close call (int 21H ah=4ch) and the terminate and stay resident call is that the former closes all files and releases all Memory Control Blocks (MCBs) owned by the terminating process. When DOS processes the a close request, it decrements the reference count in the SFT. When this count goes to 0, the SFT entry is dealocated and the file/device is finally closed. The devices/files referenced by handles 0-4 are initially opened by command.com and are inherited by each process. So long as they are only closed once and you don't mess with the JFT, the SFT reference count will never go to 0 and the devices/files will never close. (Only the SFT owner can cause entry deallocation anyway). TSRs should set the current PSP to their own PSP before trying to reactivate. Before attempting any I/O the TSR must be sure that it is safe to do so. Significant portions of DOS are not reentrant. Making an int 21h request at the wrong time can be disasterous. You should also set up critical error and break handlers before initiating an int 21h request from a reactivating TSR. Never try to use another programs JFT to perform I/O; that is an invitation for trouble. TSRs are a very complicated subject. Check out chapter 4 of the MS DOS Developer's Guide (2nd edition) (H. Sams, 1988). This chapter covers TSRs in great detail. NB I am the author of that chapter. I get no royalties; I'm merely citing a good reference. Walt Dixon {ARPA: dixon@ge-crd.arpa } {US Mail: ge-crd } { po box 8 } { schenectady, ny 12345 } {VOICE: 518-387-5798 } Standard disclaimers apply