So guys, after having understanding the concept of select system call set data structure and the concept
of accept system call, now we are in a position to actually understand how the select and accept system
call actually works together in order to implement the concept of multiplexing.
So let us try to understand how select and accept system call collaboratively works together to implement
a multiplex server.
So multiplex server means a server which has the capability to handle multiple client connections at
the same time.
So in this diagram you can see that we have a client C1 and we have a client, C2 and we have a server
process which has started.
And as we know that as soon as the server process starts, the very first thing our server process does
is to create a master socket file descriptor and place that master socket file descriptor in a set data
structure.
And as our process has now invoked the select system call on set data structure.
So at this point of time, our server has done three things.
That is, our server process has booted up and created a master socket file descriptor.
Second thing, our server has placed that master socket file descriptor in a data structure.
And the third thing our server has invoked the select system call on set data structure.
Right now since the set data structure at this point of time contains only one file descriptor and that
is master socket file descriptor and the purpose of master socket file descriptor is to detect new connection
initiation requests from new client.
Since the select system call is a blocking system call.
Therefore, our server process is now waiting for new connection initiation requests from new client.
So this is exactly the state our server process is currently in.
So here you can see that this represent set data structure.
And at this point of time, this data structure has only one file descriptor as its member, and that
is master socket file descriptor.
Now suppose the client C1 wants to carry out communication with our server.
So the client C1 sends the connection initiation request to our server by invoking the connect system
call.
As soon as the server detects that it has received connect system call the select system call of the
server will get unblocked.
Now since the server is unblocked because it has received the connection initiation request from new
client.
The second step that the server will do is to invoke the accept system call.
And by invoking the accept system call the server will generate a communication file descriptor for
client C1 using this communication file descriptor.
Our server will carry out actual data exchange with the client C1.
For the rest of the lifetime of this connection.
In addition, when the server has generated this communication file descriptor for client one, the
server will save this communication file descriptor in set data structure so you can see that in set
data structure along with the master socket file descriptor.
A new communication file descriptor is also added.
So you can see in this diagram that when the server booted up, it created a master socket file descriptor
and it placed this master socket file descriptor in set data structure.
Now here the set data structure is denoted by read variable.
So Reid is nothing.
But it is set data structure and the server invoked the select system call.
Right.
And after that, when the server received the new connection initiation request, then the server will
get unblocked from the select system call.
And here the server will check that whether it is master socket file descriptor which is activated.
Yes.
If the master socket file descriptor is activated, it means that the server has received new connection
initiation request from new client.
So the next thing that the server does is to invoke the accept system, call and create a communication
file descriptor for that new client which has just connected to our server.
And the next thing our server does is to add this communication file descriptor to the set data structure.
Right.
And then the server goes back again and again.
It invokes the select system call on set data structure.
Right now at this point of time, our feed data structure contains two file descriptors that is master
socket file descriptor and the communication file descriptor which has which it has just created for
this new client.
So coming back to this diagram, after adding this communication file descriptor to the set data structure,
our server process again invokes the select system call on the updated set data structure.
Now, at this point of time, this set data structure contains two file descriptors.
One is master socket file descriptor and the other is communication file descriptor.
Now monitoring the two file descriptors at the same time means that if the new client sends new connection
initiation request to our server, our select system call will get unblocked because master socket file
descriptor will be activated.
In other case, if the client see one which is already connected to our server sends data request to
our server, then it will be communication file descriptor which will be activated.
So you can see because Select System Call is now monitoring two file descriptors.
Which file descriptor will be activated?
Depends whether client C one sends data request to our server or.
New client sends new connection initiation request to our server.
In the first case it is communication file descriptor which will be activated and in the second case
it will be master socket file descriptor which will be activated.
So you can see in this diagram that when the client C2 sends connection initiation requests to our server,
our server will invoke the accept system call because our server will get unblocked from the select
system call.
And the reason being that master socket file descriptor is activated.
So the return value of accept system call is again a communication file descriptor and this communication
file descriptor will be used by our server process to carry out future communication with the client.
C2.
And in addition to this, our server process will add this communication file descriptor to set data
structure.
And again, our server process goes back and invoke the select system, call on updated set data structure.
Now at this point of time, our server process is now monitoring three file descriptors, the master
socket file descriptor and the two communication file descriptors.
It means that our server process is in a state where it can process new connection initiation requests
from new client as well as it can also process data requests that can come from either client C1 or
client C2.
In any sequence.
So now you understand that select system call is actually monitor all the file descriptors which are
present in set data structure and depending on the type of event that takes place, that is whether
server receives new connection initiation request or whether server receives data request from already
connected client.
Appropriate file descriptors will be activated and the server gets unblocked from the select system
call to process that particular type of request.
So again, discussing the same thing with the help of a flowchart.
When the server receives the data request from the connected client, the server will get unblocked
from the select system call and now the server will check whether is it the master socket file descriptor
which is activated.
Of course it will be the communication file descriptor which will be activated.
So the next thing that our server needs to do is to actually identify the correct client, which has
actually sent data to our server process because there could be many clients to which our server process
is connected.
Right.
So out of those many clients, which client has actually sent the data to our server?
So the next step is to identify exactly that client which has sent data to our server.
And once our server finds that particular client, our server process then service the data request
to that client.
That is our server process, prepare the response and send the response back to that particular client.
And if the condition arises that our server process choose to close the connection with that particular
client, then in that case our server process has to remove that communication file descriptor from
the fdset data structure.
Because our server is not in a mood to carry out any future communication with that particular client.
So there is no need to maintain the communication file descriptor with that client in that data structure.
So again, the set data structure is updated and the server process goes back and again, invoke the
select system, call on an updated fdset data structure.
So the previous diagram and this flow chart together makes it very clear regarding how a multiplex server
actually works.
So we are in a position to discuss a code which implement a server process with multiplexing capabilities.
So let's have a code walkthrough.