Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Socket Inheritance

CRodrig
Beginner
1,563 Views

Intel Forum,

I have a question pertaining to socket inheritance.

I am developing an application on Windows 7 which will spawn multiple processes on the same system (Laptop).

I am using Intel Parallel Studio 2015 to develop an application written in Fortran.

My scenario is as follows:

1). The main process will create a socket and connect to a socket (TCP/IP)
     which has already been established on a remote system acting as a server.

    NOTE:
    The server is a Linux-based system (Centos 6.4) which is running a java-based
    display application. I initiate the java-based display application on the server
    which waits for a client connection.

2). The main process will establish a TCP/IP connection to the server on a given port.
     Once the connection to the server is established, the main process will spawn a
     new process using the CreateProcess function.

     For arguments sake, I will call the newly spawned process "dio" (Display I/O).

3). The newly spawned process (DIO) is to send/receive data packets to/from the java-based
     server application over the established socket connection from the main task.

My limited understanding of socket inheritance is as follows:

Under Windows NT and Windows 2000, socket handles are inheritable by default.
This feature is often used by a process that wants to spawn a child process and
have the child process interact with the remote application on the other end of the connection.

The CreateProcess function supports the <bInheritHandles> parameter.
 
bInheritHandles [in]
  If this parameter is TRUE, each inheritable handle in the calling process is inherited by the new process.
  If this parameter is FALSE, the handles are not inherited. Note that inherited handles have the same value
  and access rights as the original handles.


Question:  If socket handles are inheritable by default, do I need to set <bInheritHandles> parameter to
                 TRUE in the call to CreateProcess when creating the DIO process?

                 The CreateProcess function is presently configured as follows when creating the DIO process:

                  returnVal = CreateProcess(NULL,
          &                        cmdLine,
          &                        NULL_SECURITY_ATTRIBUTES,
          &                        NULL_SECURITY_ATTRIBUTES,
          &                        FALSE,
          &                        0,
          &                        NULL,
          &                        NULL,
          &                        sinfo,
          &                        pinfo)

In doing research on socket inheritance, I found the following documentation:

In Winsock 2, WSADuplicateSocket/WSASocket is the recommended way for sharing sockets on Windows platforms.

Handle Inheritance

A child process can inherit handles from its parent process. An inherited handle is valid only in the context
of the child process.

To enable a child process to inherit open handles from its parent process, use the following steps.
1.Create the handle with the bInheritHandle member of the SECURITY_ATTRIBUTES structure set to TRUE.
2.Create the child process using the CreateProcess function, with the bInheritHandles parameter set to TRUE.

The DuplicateHandle function duplicates a handle to be used in the current process or in another process.
If an application duplicates one of its handles for another process, the duplicated handle is valid only
in the context of the other process.

A duplicated or inherited handle is a unique value, but it refers to the same object as the original handle.

All other objects are private to the process that created them; their object handles cannot be duplicated or inherited.

Question: In setting <bInheritHandles> parameter to TRUE, will the DIO process be able to use the established socket
                connection in the main process to send/receive data packets to/from the java-based server application?

If an example exists which addresses this issue, I would appreciate any feedback.

Thanks in advance.

0 Kudos
1 Reply
Paul_Curtis
Valued Contributor I
1,563 Views

My codes do lots of socket-based TCP/IP communications, and I have found that attempting to use persistent sockets does not work.  The communications model uses multiple repeats of:

  • create a socket, attach to remote server device
  •   send packet to query device
  •   receive response packet from device
  • close the socket

Since my program queries remote devices continually, I tried using persistent sockets, where the socket once opened is retained and re-used, thereby saving the overhead of CreateSocket() and so forth for each data exchange.  This approach basically does not work, as sockets left open get closed down pretty quickly by the network.  Every code example I have found, all in C, on Windows sockets communications uses the above create/close approach.  (Interestingly, my codes also do lots of multi-threaded serial communications on multiple ports, and serial ports can be opened and left open with fixed comm parameters, works just fine; that may simply reflect that serial port status is wholly under local control, not subject to an external network.)

I also do not understand why your code needs to create a separate process in order to do communications.  I use a multi-threaded model where each communications exercise is carried out in a (persistent set of) worker thread(s), which flag the main process when the exercise ends and makes the data available from thread-safe memory to main memory, whereupon it can be displayed, processed, etc.  The main program has a "heartbeat" routine driven by a timer which queries the status of all threads, gets the data from completed comm exercises and restarts the thread for its next comm episode.

0 Kudos
Reply