Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!cs.utexas.edu!sun-barr!newstop!sun!imagen!qmsseq!pipkins From: pipkins@qmsseq.imagen.com (Jeff Pipkins) Newsgroups: comp.sys.ibm.pc Subject: Re: More than 15 files open Message-ID: <67@qmsseq.imagen.com> Date: 19 Dec 89 17:02:54 GMT References: <1989Dec17.150044.18175@axion.bt.co.uk> <5831@eos.UUCP> <1989Dec18.164450.3079@mks.com> Reply-To: pipkins@qmsseq.UUCP (Jeff Pipkins) Organization: QMS Inc., Mobile, Alabama Lines: 76 In article <1989Dec18.164450.3079@mks.com> andy@mks.com (Andy Toy) writes: >In article <5831@eos.UUCP> woody@eos.UUCP (Wayne Wood) writes: >>why not put >>FILES=20 >>in you config.sys file? > >Because all it does it change that total number of files that can be >opened and not the number of files that each programme can have open. >Consider that a programme opens ten files and then spawns another >programme that tries to open more files. The total number of files >that can be opened will be 20 and not 20 files for each programme. >The ``FILES='' cannot give more than 20 files for each programme. > >The limit is 20 files for each programme, but stdin, stdout, stderr, >stdaux, stdprn are automatically open so that leaves each programme >with 15 files maximum. You could close stdaux and stdprn for two >extra files. MS-DOS 3.30 and above allow FILES=20 up to FILES=255. That should be plenty. MS-DOS function 67h can be used to set this dynamically after boot time (i.e. in a program), but you should realize that this function has to allocate some memory for a table. Therefore, many programs must make a call to the DOS SETBLOCK function 4Ah to release some unused memory, since DOS usually allocates all available memory to a newly loaded program. If your program is written in MSC or Turdo C, the startup code will call SETBLOCK for you. If your program must work with all versions of DOS (all means >= 2.0) then you will have to play a little nastier than that. There is some undocumented info in a program's Program Segment Prefix (PSP) that you may want to be aware of. PSP:18h This is a 20-byte translation table for file handles. The length of this table is responsible for the FILES=20 limit. Redirection is handled by the mapping in this table. The handle number used internally by DOS is not necessarily the same as the one used by your program. Since the PSP of a spawned child process is created by first copying the PSP of the parent, the inheritance of open files is a simple operation. PSP:32h This 16-bit word tells the size of the table at PSP:18h. PSP:34h This is a long pointer that is initialized to point to PSP:18h, but it can be changed. If you want to write a program that opens many files at once, you can: 1) Allocate a file handle translation table of sufficient length and initialize the bytes to FFh. 2) Copy the old file table over the new one to preserve access to files already opened. 3) Set the far pointer at PSP:34h to point to the new table. 4) Set the word count at PSP:32h to the number of bytes in the new table. This raises a question about what you want to happen when this program spawns a child process. The usual method causes the table to be copied, not just the pointer to the table. You may want to consider duplicating the table for the child process. Someone asked about inheritance bits on this same newsgroup. One way to prevent a child process from inheriting certain open files is to replace the table entries that correspond to the private handle with an FFh byte, and then restore the byte after the child process terminates. (If you make a copy of the table or if you don't replace the one in the PSP, you can restore it after the child is spawned; if the child shares your table, you'll have to wait until it terminates to restore access to the file if you don't want it to use it.) Good luck. Jeff Pipkins pipkins@imagen.com