gtpc2mj9 | C/C++ Language Support User's Guide |
This section describes some considerations for special files.
In addition to the device driver functions that make up the special file interface, every device driver table entry contains a tpf_spif_flags field. The following flags are defined for this field:
The tpf_fork function permits a child process to inherit file descriptors from its parent process. When a file descriptor is inherited, its device driver functions can be called by multiple entry control blocks (ECBs) that are running concurrently on multiple I-streams using the same filedata object.
A file descriptor is inherited only if:
For example, the /dev/null device driver used in the TPF_FSDD_OPEN function can be inherited because:
A file descriptor is inherited only if the UDDTBL device driver entry referenced by the file descriptor has the TPF_SPIF_INHERITABLE bit in its tpf_spif_flags member set to ON. If this flag is set to OFF, file descriptors that refer to the device driver will not be inherited by child processes.
There are conditions when it is desirable for an application to control special file resources rather than being managed by the device driver (for example, not closing a socket when there is no ECB accessing the socket). The /dev/tpf.socket.file special file allows you to treat a socket in the TPF system as if it is a regular file. However, one of the characteristics of a regular file is that it is closed automatically when all of the processes that have access to its open file descriptor have exited. In the TPF system, a socket can remain open even if no ECB has access to it; for example, this allows an ECB to pass a socket to a created ECB.
If the TPF_SPIF_NODDCLOSE flag in the tpf_spif_flags field for the UDDTBL device driver entry referenced by the file descriptor is set ON, the application can bypass the TPF_FSDD_CLOSE -type device driver function by calling the control open file descriptors (fcntl) function to set the O_TPF_NODDCLOSE open file description flag to ON. When the TPF_SPIF_NODDCLOSE flag in tpf_spif_flags field is set to OFF, attempting to set the O_TPF_NODDCLOSE open file description flag has no effect.
The following example shows how to open a socket as a special file without causing the socket to be closed when its file descriptor is closed.
int socfiledes; pid_t childpid; /******************************************************************/ /* Open the socket and the socket special file. */ /******************************************************************/ char socfilename[80]; int socdes = socket( /* parameters specifying socket */); sprintf(socfilename, "/dev/tpf.socket.file/%X", socdes); socfiledes = open(socfilename, O_RDWR); /******************************************************************/ /* Take responsibility for the underlying socket from the file */ /* descriptor. */ /******************************************************************/ fcntl(socfiledes, F_SETFL, O_TPF_NODDCLOSE | fcntl(socfiledes, F_GETFL)); /******************************************************************/ /* Pass the socket file descriptor to a child process by */ /* inheritance through tpf_fork(). */ /******************************************************************/ childpid = tpf_fork( /* parameters specifying child process */ ); /******************************************************************/ /* Close the file descriptor but not the underlying socket. */ /******************************************************************/ close(socfiledes);