You are here: SwitchKit Programmer’s Guide > 11 Programming Tips & Examples > Advanced Programming Technique in UNIX
Advanced Programming Technique in UNIX
Description
In UNIX, an application can split into two or more processes after opening a connection with the LLC. The processes, parent and child have a copy of the connection. All processes can write to the connection with no problem, but if they all try to read, a race condition occurs and only one process receives the message.
How to Solve the Race Condition
In case parent and child need to communicate with the LLC, the child should call sk_initializeConnection() or one of the sk_createConnection() functions after the split to get its own connection.
It is important to understand that in such a case, the processes have a completely different connection. An acknowledgment of a message sent by one process cannot be received by the other process. Use the message SK_InterAppMsg to pass on acknowledgments or other important messages.
Channels are also associated with connection names. Even after a split of processes, the channels stay with the parent. To delegate a channel assignment to a child, use the message SK_TransferChanMsg.
Example
The following example demonstrates the connection management calls in action. In this example, a UNIX process forks a child that connects to the LLC socket.
sk_broadcastLoad(1);// This makes the (parent) open its socket
child = fork(); // Spawn a child process
if (child==0)
{ // CHILD PROCESS CODE
SK_Connection *parentConnect;
SK_Connection *newConnect;
// save the parent's socket
parentConnect = sk_getCurrentConnection();
newConnect = sk_createConnection("Child",-1,0,NULL,
-1,NULL,-1);
// set the child's socket to the new one
sk_setCurrentConnection(newConnect);
// close the parent's socket in the child
sk_destroyConnection(parentConnect);
};